hlt_udp_simple.py revision a842b4cf
1#!/usr/bin/python
2
3"""
4Sample HLTAPI application (for loopback)
5Connect to TRex
6Send UDP packet in specific length
7Each direction has its own IP range
8"""
9
10import sys
11import argparse
12import stl_path
13from trex_stl_lib.api import *
14from trex_stl_lib.trex_stl_hltapi import *
15
16
17if __name__ == "__main__":
18    parser = argparse.ArgumentParser(usage="""
19    Connect to TRex and send bidirectional continuous traffic
20
21    examples:
22
23     hlt_udp_simple.py --server <hostname/ip>
24
25     hlt_udp_simple.py -s 300 -d 30 -rate_pps 5000000 --src <MAC> --dst <MAC>
26
27     then run the simulator on the output
28       ./stl-sim -f example.yaml -o a.pcap  ==> a.pcap include the packet
29
30    """,
31    description="Example for TRex HLTAPI",
32    epilog=" based on hhaim's stl_run_udp_simple example");
33
34    parser.add_argument("--server",
35                        dest="server",
36                        help='Remote trex address',
37                        default="127.0.0.1",
38                        type = str)
39
40    parser.add_argument("-s", "--frame-size",
41                        dest="frame_size",
42                        help='L2 frame size in bytes without FCS',
43                        default=60,
44                        type = int,)
45
46    parser.add_argument('-d','--duration',
47                        dest='duration',
48                        help='duration in second ',
49                        default=10,
50                        type = int,)
51
52    parser.add_argument('--rate-pps',
53                        dest='rate_pps',
54                        help='speed in pps',
55                        default="100")
56
57    parser.add_argument('--src',
58                        dest='src_mac',
59                        help='src MAC',
60                        default='00:50:56:b9:de:75')
61
62    parser.add_argument('--dst',
63                        dest='dst_mac',
64                        help='dst MAC',
65                        default='00:50:56:b9:34:f3')
66
67    args = parser.parse_args();
68
69    hltapi = CTRexHltApi()
70    print 'Connecting to TRex'
71    res = hltapi.connect(device = args.server, port_list = [0, 1], reset = True, break_locks = True)
72    check_res(res)
73    ports = res['port_handle']
74    if len(ports) < 2:
75        error('Should have at least 2 ports for this test')
76    print 'Connected, acquired ports: %s' % ports
77
78    print 'Creating traffic'
79
80    res = hltapi.traffic_config(mode = 'create', bidirectional = True,
81                                port_handle = ports[0], port_handle2 = ports[1],
82                                frame_size = args.frame_size,
83                                mac_src = args.src_mac, mac_dst = args.dst_mac,
84                                mac_src2 = args.dst_mac, mac_dst2 = args.src_mac,
85                                l3_protocol = 'ipv4',
86                                ip_src_addr = '10.0.0.1', ip_src_mode = 'increment', ip_src_count = 254,
87                                ip_dst_addr = '8.0.0.1', ip_dst_mode = 'increment', ip_dst_count = 254,
88                                l4_protocol = 'udp',
89                                udp_dst_port = 12, udp_src_port = 1025,
90                                stream_id = 1, # temporary workaround, add_stream does not return stream_id
91                                rate_pps = args.rate_pps,
92                                )
93    check_res(res)
94
95    print 'Starting traffic'
96    res = hltapi.traffic_control(action = 'run', port_handle = ports[:2])
97    check_res(res)
98    wait_with_progress(args.duration)
99
100    print 'Stopping traffic'
101    res = hltapi.traffic_control(action = 'stop', port_handle = ports[:2])
102    check_res(res)
103
104    res = hltapi.traffic_stats(mode = 'aggregate', port_handle = ports[:2])
105    check_res(res)
106    print_brief_stats(res)
107
108    res = hltapi.cleanup_session(port_handle = 'all')
109    check_res(res)
110
111    print 'Done'
112