1d4b2ae7dSYaroslav Brustinov#!/router/bin/python
2d4b2ae7dSYaroslav Brustinov
3d4b2ae7dSYaroslav Brustinovimport outer_packages
4ee916906SYaroslav Brustinov#from trex_stl_lib.trex_stl_hltapi import CTRexHltApi, CStreamsPerPort
5ee916906SYaroslav Brustinovfrom trex_stl_lib.trex_stl_hltapi import *
6d4b2ae7dSYaroslav Brustinovimport traceback
7d4b2ae7dSYaroslav Brustinovimport sys, time
8d4b2ae7dSYaroslav Brustinovfrom pprint import pprint
99f143525SYaroslav Brustinovimport argparse
10d4b2ae7dSYaroslav Brustinov
110f03cc46SYaroslav Brustinovdef error(err = None):
120f03cc46SYaroslav Brustinov    if not err:
130f03cc46SYaroslav Brustinov        raise Exception('Unknown exception, look traceback')
140f03cc46SYaroslav Brustinov    if type(err) is str and not err.startswith('[ERR]'):
150f03cc46SYaroslav Brustinov        err = '[ERR] ' + err
160f03cc46SYaroslav Brustinov    print err
170f03cc46SYaroslav Brustinov    sys.exit(1)
180f03cc46SYaroslav Brustinov
199f143525SYaroslav Brustinovdef check_res(res):
20d4b2ae7dSYaroslav Brustinov    if res['status'] == 0:
210f03cc46SYaroslav Brustinov        error('Encountered error:\n%s' % res['log'])
229f143525SYaroslav Brustinov    return res
239f143525SYaroslav Brustinov
249f143525SYaroslav Brustinovdef print_brief_stats(res):
259f143525SYaroslav Brustinov    title_str = ' '*3
269f143525SYaroslav Brustinov    tx_str = 'TX:'
279f143525SYaroslav Brustinov    rx_str = 'RX:'
289f143525SYaroslav Brustinov    for port_id, stat in res.iteritems():
299f143525SYaroslav Brustinov        if type(port_id) is not int:
309f143525SYaroslav Brustinov            continue
319f143525SYaroslav Brustinov        title_str += ' '*10 + 'Port%s' % port_id
329f143525SYaroslav Brustinov        tx_str    += '%15s' % res[port_id]['aggregate']['tx']['total_pkts']
339f143525SYaroslav Brustinov        rx_str    += '%15s' % res[port_id]['aggregate']['rx']['total_pkts']
349f143525SYaroslav Brustinov    print(title_str)
359f143525SYaroslav Brustinov    print(tx_str)
369f143525SYaroslav Brustinov    print(rx_str)
379f143525SYaroslav Brustinov
389f143525SYaroslav Brustinovdef wait_with_progress(seconds):
399f143525SYaroslav Brustinov    for i in range(0, seconds):
409f143525SYaroslav Brustinov        time.sleep(1)
419f143525SYaroslav Brustinov        sys.stdout.write('.')
429f143525SYaroslav Brustinov        sys.stdout.flush()
439f143525SYaroslav Brustinov    print('')
44d4b2ae7dSYaroslav Brustinov
45d4b2ae7dSYaroslav Brustinovif __name__ == "__main__":
46d4b2ae7dSYaroslav Brustinov    try:
479f143525SYaroslav Brustinov        parser = argparse.ArgumentParser(description='Example of using stateless TRex via HLT API.', formatter_class=argparse.RawTextHelpFormatter)
489f143525SYaroslav Brustinov        parser.add_argument('-v', dest = 'verbose', default = 0, help='Stateless API verbosity:\n0: No prints\n1: Commands and their status\n2: Same as 1 + ZMQ in&out')
499f143525SYaroslav Brustinov        parser.add_argument('--device', dest = 'device', default = 'localhost', help='Address of TRex server')
509f143525SYaroslav Brustinov        args = parser.parse_args()
519f143525SYaroslav Brustinov        hlt_client = CTRexHltApi(verbose = int(args.verbose))
520f03cc46SYaroslav Brustinov
539f143525SYaroslav Brustinov        print('Connecting to %s...' % args.device)
549f143525SYaroslav Brustinov        res = check_res(hlt_client.connect(device = args.device, port_list = [0, 1], username = 'danklei', break_locks = True, reset = True))
55d4b2ae7dSYaroslav Brustinov        port_handle = res['port_handle']
560f03cc46SYaroslav Brustinov        print('Connected, got port handles %s' % port_handle)
57ee916906SYaroslav Brustinov        ports_streams_dict = CStreamsPerPort()
58e07322a7SYaroslav Brustinov        print hlt_client.traffic_control(action = 'poll')
590f03cc46SYaroslav Brustinov
60e07322a7SYaroslav Brustinov        print hlt_client.traffic_config(mode = 'create', l2_encap = 'ethernet_ii_vlan', rate_pps = 1,
61e07322a7SYaroslav Brustinov                                        l3_protocol = 'ipv4',
62e07322a7SYaroslav Brustinov                                        #length_mode = 'imix', l3_length = 200,
63e07322a7SYaroslav Brustinov                                        ipv6_dst_mode = 'decrement', ipv6_dst_count = 300, ipv6_dst_addr = 'fe80:0:0:0:0:0:0:000f',
64e07322a7SYaroslav Brustinov                                        port_handle = port_handle, port_handle2 = port_handle[1],
65e07322a7SYaroslav Brustinov                                        #save_to_yaml = '/tmp/d1.yaml',
66e07322a7SYaroslav Brustinov                                        #stream_id = 1,
67e07322a7SYaroslav Brustinov                                        )
68e07322a7SYaroslav Brustinov        print hlt_client.traffic_control(action = 'poll')
69e07322a7SYaroslav Brustinov        print hlt_client.traffic_control(action = 'run')
70e07322a7SYaroslav Brustinov        print hlt_client.traffic_control(action = 'poll')
71e07322a7SYaroslav Brustinov        wait_with_progress(2)
72e07322a7SYaroslav Brustinov        print hlt_client.traffic_control(action = 'poll')
73e07322a7SYaroslav Brustinov        print hlt_client.traffic_control(action = 'stop')
74e07322a7SYaroslav Brustinov        print hlt_client.traffic_control(action = 'poll')
75e07322a7SYaroslav Brustinov        print hlt_client.traffic_stats(mode = 'aggregate')
76e07322a7SYaroslav Brustinov        print hlt_client.traffic_control(action = 'clear_stats')
77e07322a7SYaroslav Brustinov        wait_with_progress(1)
78e07322a7SYaroslav Brustinov        print hlt_client.traffic_stats(mode = 'aggregate')
79e07322a7SYaroslav Brustinov
80e07322a7SYaroslav Brustinov        wait_with_progress(1)
81e07322a7SYaroslav Brustinov        print hlt_client.traffic_stats(mode = 'aggregate')
82e07322a7SYaroslav Brustinov        wait_with_progress(1)
83e07322a7SYaroslav Brustinov        print hlt_client.traffic_stats(mode = 'aggregate')
84e07322a7SYaroslav Brustinov        wait_with_progress(1)
85e07322a7SYaroslav Brustinov        print hlt_client.traffic_stats(mode = 'aggregate')
86ee916906SYaroslav Brustinov        #print res
87ee916906SYaroslav Brustinov        #print hlt_client._streams_history
88ee916906SYaroslav Brustinov        #print hlt_client.trex_client._STLClient__get_all_streams(port_id = port_handle[0])
89ee916906SYaroslav Brustinov        #print hlt_client.trex_client._STLClient__get_all_streams(port_id = port_handle[1])
90ee916906SYaroslav Brustinov        #ports_streams_dict.add_streams_from_res(res)
91ee916906SYaroslav Brustinov        sys.exit(0)
92ee916906SYaroslav Brustinov        res = check_res(hlt_client.traffic_config(mode = 'create', l2_encap = 'ethernet_ii_vlan', rate_pps = 1,
93ee916906SYaroslav Brustinov                                                  port_handle = port_handle[0], port_handle2 = port_handle[1], save_to_yaml = '/tmp/d1.yaml',
94ee916906SYaroslav Brustinov                                                  l4_protocol = 'udp',
95ee916906SYaroslav Brustinov                                                  #udp_src_port_mode = 'decrement',
96ee916906SYaroslav Brustinov                                                  #udp_src_port_count = 10, udp_src_port = 5,
97ee916906SYaroslav Brustinov                                                  ))
980f03cc46SYaroslav Brustinov        ports_streams_dict.add_streams_from_res(res)
99ee916906SYaroslav Brustinov        sys.exit(0)
1000f03cc46SYaroslav Brustinov        #print ports_streams_dict
1010f03cc46SYaroslav Brustinov        #print hlt_client.trex_client._STLClient__get_all_streams(port_id = port_handle[0])
1020f03cc46SYaroslav Brustinov        res = check_res(hlt_client.traffic_config(mode = 'modify', port_handle = port_handle[0], stream_id = ports_streams_dict[0][0],
1030f03cc46SYaroslav Brustinov                                                  mac_src = '1-2-3:4:5:6', l4_protocol = 'udp', save_to_yaml = '/tmp/d2.yaml'))
1040f03cc46SYaroslav Brustinov        #print hlt_client.trex_client._STLClient__get_all_streams(port_id = port_handle[0])
1050f03cc46SYaroslav Brustinov        #print hlt_client._streams_history
1060f03cc46SYaroslav Brustinov        res = check_res(hlt_client.traffic_config(mode = 'modify', port_handle = port_handle[0], stream_id = ports_streams_dict[0][0],
1070f03cc46SYaroslav Brustinov                                                  mac_dst = '{ 7 7 7-7:7:7}', save_to_yaml = '/tmp/d3.yaml'))
1080f03cc46SYaroslav Brustinov        #print hlt_client.trex_client._STLClient__get_all_streams(port_id = port_handle[0])
1090f03cc46SYaroslav Brustinov        check_res(hlt_client.traffic_config(mode = 'reset', port_handle = port_handle))
1100f03cc46SYaroslav Brustinov
111ee916906SYaroslav Brustinov        res = check_res(hlt_client.traffic_config(mode = 'create', bidirectional = True, length_mode = 'fixed',
112ee916906SYaroslav Brustinov                                                  port_handle = port_handle[0], port_handle2 = port_handle[1],
1130f03cc46SYaroslav Brustinov                                                  transmit_mode = 'single_burst', pkts_per_burst = 100, rate_pps = 100,
1140f03cc46SYaroslav Brustinov                                                  mac_src = '1-2-3-4-5-6',
1150f03cc46SYaroslav Brustinov                                                  mac_dst = '6:5:4:4:5:6',
1160f03cc46SYaroslav Brustinov                                                  save_to_yaml = '/tmp/imix.yaml'))
1170f03cc46SYaroslav Brustinov        ports_streams_dict.add_streams_from_res(res)
1189f143525SYaroslav Brustinov
1199f143525SYaroslav Brustinov        print('Create single_burst 100 packets rate_pps=100 on port 0')
1200f03cc46SYaroslav Brustinov        res = check_res(hlt_client.traffic_config(mode = 'create', port_handle = port_handle[0], transmit_mode = 'single_burst',
1210f03cc46SYaroslav Brustinov                                                  pkts_per_burst = 100, rate_pps = 100))
1220f03cc46SYaroslav Brustinov        ports_streams_dict.add_streams_from_res(res)
12369e5a5c6SYaroslav Brustinov
12469e5a5c6SYaroslav Brustinov        # playground - creating various streams on port 1
12569e5a5c6SYaroslav Brustinov        res = check_res(hlt_client.traffic_config(mode = 'create', port_handle = port_handle[1], save_to_yaml = '/tmp/hlt2.yaml',
12669e5a5c6SYaroslav Brustinov                        tcp_src_port_mode = 'decrement',
12769e5a5c6SYaroslav Brustinov                        tcp_src_port_count = 10, tcp_dst_port_count = 10, tcp_dst_port_mode = 'random'))
1280f03cc46SYaroslav Brustinov        ports_streams_dict.add_streams_from_res(res)
12969e5a5c6SYaroslav Brustinov
13069e5a5c6SYaroslav Brustinov        res = check_res(hlt_client.traffic_config(mode = 'create', port_handle = port_handle[1], save_to_yaml = '/tmp/hlt3.yaml',
13169e5a5c6SYaroslav Brustinov                        l4_protocol = 'udp',
13269e5a5c6SYaroslav Brustinov                        udp_src_port_mode = 'decrement',
13369e5a5c6SYaroslav Brustinov                        udp_src_port_count = 10, udp_dst_port_count = 10, udp_dst_port_mode = 'random'))
1340f03cc46SYaroslav Brustinov        ports_streams_dict.add_streams_from_res(res)
13569e5a5c6SYaroslav Brustinov
13669e5a5c6SYaroslav Brustinov        res = check_res(hlt_client.traffic_config(mode = 'create', port_handle = port_handle[1], save_to_yaml = '/tmp/hlt4.yaml',
13769e5a5c6SYaroslav Brustinov                        length_mode = 'increment',
13869e5a5c6SYaroslav Brustinov                        #ip_src_addr = '192.168.1.1', ip_src_mode = 'increment', ip_src_count = 5,
13969e5a5c6SYaroslav Brustinov                        ip_dst_addr = '5.5.5.5', ip_dst_mode = 'random', ip_dst_count = 2))
1400f03cc46SYaroslav Brustinov        ports_streams_dict.add_streams_from_res(res)
14169e5a5c6SYaroslav Brustinov
14269e5a5c6SYaroslav Brustinov        res = check_res(hlt_client.traffic_config(mode = 'create', port_handle = port_handle[1], save_to_yaml = '/tmp/hlt5.yaml',
14369e5a5c6SYaroslav Brustinov                        length_mode = 'decrement', frame_size_min = 100, frame_size_max = 3000,
14469e5a5c6SYaroslav Brustinov                        #ip_src_addr = '192.168.1.1', ip_src_mode = 'increment', ip_src_count = 5,
14569e5a5c6SYaroslav Brustinov                        #ip_dst_addr = '5.5.5.5', ip_dst_mode = 'random', ip_dst_count = 2
14669e5a5c6SYaroslav Brustinov                        ))
1470f03cc46SYaroslav Brustinov        ports_streams_dict.add_streams_from_res(res)
1480f03cc46SYaroslav Brustinov
14969e5a5c6SYaroslav Brustinov        # remove the playground
15069e5a5c6SYaroslav Brustinov        check_res(hlt_client.traffic_config(mode = 'reset', port_handle = port_handle[1]))
1519f143525SYaroslav Brustinov
1529f143525SYaroslav Brustinov        print('Create continuous stream for port 1, rate_pps = 1')
15369e5a5c6SYaroslav Brustinov        res = check_res(hlt_client.traffic_config(mode = 'create', port_handle = port_handle[1], save_to_yaml = '/tmp/hlt1.yaml',
15469e5a5c6SYaroslav Brustinov                        #length_mode = 'increment', l3_length_min = 200,
15569e5a5c6SYaroslav Brustinov                        ip_src_addr = '192.168.1.1', ip_src_mode = 'increment', ip_src_count = 5,
15669e5a5c6SYaroslav Brustinov                        ip_dst_addr = '5.5.5.5', ip_dst_mode = 'random', ip_dst_count = 2))
1579f143525SYaroslav Brustinov
15869e5a5c6SYaroslav Brustinov        check_res(hlt_client.traffic_control(action = 'run', port_handle = port_handle))
15969e5a5c6SYaroslav Brustinov        wait_with_progress(1)
16069e5a5c6SYaroslav Brustinov        print('Sample after 1 seconds (only packets count)')
1619f143525SYaroslav Brustinov        res = check_res(hlt_client.traffic_stats(mode = 'all', port_handle = port_handle))
16269e5a5c6SYaroslav Brustinov        print_brief_stats(res)
16369e5a5c6SYaroslav Brustinov        print ''
1649f143525SYaroslav Brustinov
16569e5a5c6SYaroslav Brustinov        print('Port 0 has finished the burst, put continuous instead with rate 1000. No stopping of other ports.')
16669e5a5c6SYaroslav Brustinov        check_res(hlt_client.traffic_control(action = 'stop', port_handle = port_handle[0]))
16769e5a5c6SYaroslav Brustinov        check_res(hlt_client.traffic_config(mode = 'reset', port_handle = port_handle[0]))
16869e5a5c6SYaroslav Brustinov        res = check_res(hlt_client.traffic_config(mode = 'create', port_handle = port_handle[0], rate_pps = 1000))
1690f03cc46SYaroslav Brustinov        ports_streams_dict.add_streams_from_res(res)
17069e5a5c6SYaroslav Brustinov        check_res(hlt_client.traffic_control(action = 'run', port_handle = port_handle[0]))
1719f143525SYaroslav Brustinov        wait_with_progress(5)
17269e5a5c6SYaroslav Brustinov        print('Sample after another 5 seconds (only packets count)')
1739f143525SYaroslav Brustinov        res = check_res(hlt_client.traffic_stats(mode = 'aggregate', port_handle = port_handle))
1749f143525SYaroslav Brustinov        print_brief_stats(res)
17569e5a5c6SYaroslav Brustinov        print ''
1769f143525SYaroslav Brustinov
1779f143525SYaroslav Brustinov        print('Stop traffic at port 1')
17869e5a5c6SYaroslav Brustinov        res = check_res(hlt_client.traffic_control(action = 'stop', port_handle = port_handle[1]))
1799f143525SYaroslav Brustinov        wait_with_progress(5)
1809f143525SYaroslav Brustinov        print('Sample after another %s seconds (only packets count)' % 5)
1819f143525SYaroslav Brustinov        res = check_res(hlt_client.traffic_stats(mode = 'aggregate', port_handle = port_handle))
1829f143525SYaroslav Brustinov        print_brief_stats(res)
18369e5a5c6SYaroslav Brustinov        print ''
18469e5a5c6SYaroslav Brustinov        print('Full HLT stats:')
18569e5a5c6SYaroslav Brustinov        pprint(res)
1869f143525SYaroslav Brustinov
1879f143525SYaroslav Brustinov        check_res(hlt_client.cleanup_session())
188d4b2ae7dSYaroslav Brustinov    except Exception as e:
1899f143525SYaroslav Brustinov        print(traceback.print_exc())
1909f143525SYaroslav Brustinov        print(e)
191d4b2ae7dSYaroslav Brustinov        raise
192d4b2ae7dSYaroslav Brustinov    finally:
1939f143525SYaroslav Brustinov        print('Done.')
194