def minimal_stateful_test(server): print('Connecting to %s' % server) trex_client = CTRexClient(server) print('Connected, start TRex') trex_client.start_trex( c=1, m=700, f='cap2/http_simple.yaml', d=30, l=1000, ) print('Sample until end') result = trex_client.sample_to_run_finish() print('Test results:') print(result) print('TX by ports:') tx_ptks_dict = result.get_last_value('trex-global.data', 'opackets-*') print(' | '.join([ '%s: %s' % (k.split('-')[-1], tx_ptks_dict[k]) for k in sorted(tx_ptks_dict.keys()) ])) print('RX by ports:') rx_ptks_dict = result.get_last_value('trex-global.data', 'ipackets-*') print(' | '.join([ '%s: %s' % (k.split('-')[-1], rx_ptks_dict[k]) for k in sorted(rx_ptks_dict.keys()) ])) print('CPU utilization:') print(result.get_value_list('trex-global.data.m_cpu_util'))
def minimal_stateful_test(server): print('Connecting to %s' % server) trex_client = CTRexClient(server) print('Connected, start TRex') trex_client.start_trex( c = 1, m = 700, f = 'cap2/http_simple.yaml', d = 30, l = 1000, ) print('Sample until end') result = trex_client.sample_to_run_finish() print('Test results:') print(result) print('TX by ports:') tx_ptks_dict = result.get_last_value('trex-global.data', 'opackets-*') print(' | '.join(['%s: %s' % (k.split('-')[-1], tx_ptks_dict[k]) for k in sorted(tx_ptks_dict.keys())])) print('RX by ports:') rx_ptks_dict = result.get_last_value('trex-global.data', 'ipackets-*') print(' | '.join(['%s: %s' % (k.split('-')[-1], rx_ptks_dict[k]) for k in sorted(rx_ptks_dict.keys())])) print('CPU utilization:') print(result.get_value_list('trex-global.data.m_cpu_util'))
def minimal_stateful_test(server,csv_file,a_active_flows): trex_client = CTRexClient(server) trex_client.start_trex( c = 7, m = 30000, # f = 'cap2/cur_flow_single.yaml', f = 'cap2/cur_flow.yaml', d = 30, l = 1000, p=True, cfg = "cfg/trex_08_5mflows.yaml", active_flows=a_active_flows, nc=True ) result = trex_client.sample_to_run_finish() active_flows = result.get_value_list('trex-global.data.m_active_flows') cpu_utl = result.get_value_list('trex-global.data.m_cpu_util') pps = result.get_value_list('trex-global.data.m_tx_pps') queue_full = result.get_value_list('trex-global.data.m_total_queue_full') if queue_full[-1]>10000: print("WARNING QUEU WAS FULL"); tuple=(active_flows[-5],cpu_utl[-5],pps[-5],queue_full[-1]) file_writer = csv.writer(test_file) file_writer.writerow(tuple);
def minimal_stateful_test(server,csv_file,a_active_flows): trex_client = CTRexClient(server) trex_client.start_trex( c = 7, m = 30000, f = 'cap2/cur_flow_single.yaml', # f = 'cap2/cur_flow.yaml', d = 30, l = 1000, p=True, cfg = "cfg/trex_08_5mflows.yaml", active_flows=a_active_flows, nc=True ) result = trex_client.sample_until_finish() active_flows = result.get_value_list('trex-global.data.m_active_flows') cpu_utl = result.get_value_list('trex-global.data.m_cpu_util') pps = result.get_value_list('trex-global.data.m_tx_pps') queue_full = result.get_value_list('trex-global.data.m_total_queue_full') if queue_full[-1]>10000: print("WARNING QUEU WAS FULL"); tuple=(active_flows[-5],cpu_utl[-5],pps[-5],queue_full[-1]) print(tuple) file_writer = csv.writer(test_file) file_writer.writerow(tuple);
def gen_stf(): trex = CTRexClient('127.0.0.1') print("Before Running, TRex status is: %s", trex.is_running()) print("Before Running, TRex status is: %s", trex.get_running_status()) ret = trex.start_trex(c=1, m=0.1, d=int(args.duration), f='mydns_traffic.yaml', nc=True) print("After Starting, TRex status is: %s %s", trex.is_running(), trex.get_running_status()) time.sleep(int(args.duration)) while trex.is_running(): time.sleep(5) print("Is TRex running? %s %s", trex.is_running(), trex.get_running_status())
def run_stateful_tcp_test(server): trex_client = CTRexClient(server) trex_client.start_trex( c=1, # limitation for now m=1000, f='cap2/http_simple.yaml', k=10, d=20, l=1000, astf=True, #enable ASTF mode nc=True) result = trex_client.sample_to_run_finish() c = result.get_latest_dump() pprint(c["tcp-v1"]["data"]) tcp_c = c["tcp-v1"]["data"]["client"] if not validate_tcp(tcp_c): return False tcp_s = c["tcp-v1"]["data"]["server"] if not validate_tcp(tcp_s): return False
def restart_stl_mode(ip): try: trex = CTRexClient(ip) if trex.get_running_status()['state'] == TRexStatus.Idle: # print('trex server process is not running') return False else: trex.force_kill(confirm=False) time.sleep(1) # 4 if trex.get_running_status()['state'] == TRexStatus.Idle: print("Success killed server, ip:" + ip) return True except Exception as e: print( 'Error occurred when automatically resetting up trex stateless server at: ' + ip) print(e) return False
def start_stl_mode(ip): try: trex = CTRexClient(ip) if trex.get_running_status()['state'] == TRexStatus.Running: # print('server has already running') print('ip: ' + ip + ' server has started!') else: trex.start_stateless() time.sleep(1) # 4 if trex.get_running_status()['state'] == TRexStatus.Running: print("Success start, ip:" + ip) return True except Exception as e: print( '\nError occurs when automatically starting up trex stateless server at: ' + ip + '\n') print(e) return False
def main() -> int: # Initialize the argument parser and subparsers # First we initialize general arguments. parser = argparse.ArgumentParser(description="Linerate test control plane") parser.add_argument( "--server-addr", type=str, help="The server address", default="127.0.0.1", required=False, ) parser.add_argument( "--trex-config", type=str, help="The Trex config to be placed on the server.", required=True, ) parser.add_argument( "--keep-running", action="store_true", default=False, help="Keep Trex running after the test.", ) parser.add_argument( "--force-restart", action="store_true", default=False, help="Force restart the Trex process " + "if there is one running.", ) # Second, we initialize subparsers from all test scripts subparsers = parser.add_subparsers( dest="test", help="The test profile, which is the " + "filename(without .py) in the test directory", required=True, ) test_py_list = glob.glob(join(dirname(__file__), "tests", "*.py")) test_list = [ basename(f)[:-3] for f in test_py_list if isfile(f) and not f.endswith("__init__.py") ] for test in test_list: test_class = get_test_class(test) if not test_class: continue test_parser = subparsers.add_parser(test) test_class.setup_subparser(test_parser) # Finally, we get the arguments args = parser.parse_args() # Set up the Trex server if not os.path.exists(args.trex_config): logging.error("Can not find Trex config file: %s", args.trex_config) return if not os.path.isfile(args.trex_config): logging.error("%s is not a file", args.trex_config) return 1 trex_config_file_on_server = TREX_FILES_DIR + os.path.basename( args.trex_config) trex_daemon_client = CTRexClient(args.server_addr) trex_started = False try: logging.info("Pushing Trex config %s to the server", args.trex_config) if not trex_daemon_client.push_files(args.trex_config): logging.error("Unable to push %s to Trex server", args.trex_config) return 1 if args.force_restart: logging.info("Killing all Trexes... with meteorite... Boom!") trex_daemon_client.kill_all_trexes() # Wait until Trex enter the Idle state start_time = time.time() success = False while time.time() - start_time < DEFAULT_KILL_TIMEOUT: if trex_daemon_client.is_idle(): success = True break time.sleep(1) if not success: logging.error("Unable to kill Trex process, please login " + "to the server and kill it manually.") return 1 if not trex_daemon_client.is_idle(): logging.info("The Trex server process is running") logging.warning("A Trex server process is still running, " + "use --force-restart to kill it if necessary.") return 1 test_class = get_test_class(args.test) if not test_class: logging.error("Unable to get test class for test %s", args.test) return 1 test_type = test_class.test_type() logging.info("Starting Trex with %s mode", test_class.test_type()) try: start_trex_function = getattr(trex_daemon_client, "start_{}".format(test_type)) except AttributeError: logging.error("Unkonwon test type %s", test_type) return 1 # Not checking the return value from this # call since it always return True start_trex_function(cfg=trex_config_file_on_server) trex_started = True # Start the test if test_type == "stateless": trex_client = STLClient(server=args.server_addr) elif test_type == "astf": trex_client = ASTFClient(server=args.server_addr) else: logging.error("Unknown test type %s", test_type) return 1 test = test_class(trex_client) try: logging.info("Connecting to Trex server...") trex_client.connect() logging.info("Acquaring ports...") trex_client.acquire() logging.info("Resetting and clearing port...") trex_client.reset() # Resets configs from all ports trex_client.clear_stats() # Clear status from all ports logging.info("Running the test: %s...", test) test.start(args) except STLError as e: logging.error("Got error from Trex server: %s", e) return 1 finally: logging.info("Cleaning up Trex client") trex_client.stop() trex_client.release() trex_client.disconnect() except ConnectionRefusedError: logging.error( "Unable to connect to server %s.\n" + "Did you start the Trex daemon?", args.server_addr, ) return 1 except ProtocolError as pe: logging.error("%s", pe) return 1 except TRexError as te: logging.error("TRex error: %s", te.msg) return 1 except TRexInUseError as tiue: logging.error("TRex is already taken: %s", tiue.msg) return 1 except TRexRequestDenied as trd: logging.error("Request denied: %s", trd.msg) return 1 finally: if trex_started and not args.keep_running: logging.info("Stopping Trex server") trex_daemon_client.stop_trex()
def main(): """ Read in all command line arguments. """ parser = argparse.ArgumentParser( description="Start TRex daemon client and run PTF command") parser.add_argument( "--ptf-dir", help="Directory containing PTF tests", type=str, required=True, ) parser.add_argument( "--trex-address", help="Address of the remote TRex daemon server", type=str, required=False, ) parser.add_argument( "--bess-address", help="Address of the remote BESS-UPF instance", type=str, required=False, ) parser.add_argument( "--trex-config", help= "Location of the TRex config file to be pushed to the remote server", type=str, required=False, ) parser.add_argument( "--trex-hw-mode", help="Enables NIC HW acceleration and disables TRex software mode", action="store_true", required=False, ) args, unknown_args = parser.parse_known_args() # ensure PTF command is available if not check_ptf(): error("Cannot find PTF executable") sys.exit(1) """ Run either linerate or unary test case, depending on arguments. """ # if line rate test, need to perform set up of TRex traffic generator if args.trex_address is not None: if args.trex_hw_mode: trex_args = None else: trex_args = "--software --no-hw-flow-stat" trex_daemon_client = CTRexClient(args.trex_address, trex_args=trex_args) info("Starting TRex daemon client...") success = set_up_trex_server( trex_daemon_client, args.trex_address, args.trex_config, ) if not success: error("Failed to set up TRex daemon client!") sys.exit(2) info("Running linerate test(s)...") success = run_test( bess_addr=args.bess_address, ptfdir=args.ptf_dir, trex_server_addr=args.trex_address, extra_args=unknown_args, ) if not success: error("Failed to run linerate tests!") trex_daemon_client.stop_trex() sys.exit(3) trex_daemon_client.stop_trex() # if unary test, can skip TRex set up and just run PTF command else: info("Running unary test(s)...") success = run_test( p4info_path=args.p4info, device_id=args.device_id, grpc_addr=args.grpc_addr, cpu_port=args.cpu_port, ptfdir=args.ptf_dir, port_map_path=args.port_map, platform=args.platform, generate_tv=args.generate_tv, loopback=args.loopback, profile=args.profile, extra_args=unknown_args, ) if not success: error("Failed running unary tests!") sys.exit(4)