1import stl_path
2from trex_stl_lib.api import *
3
4import time
5import json
6
7# simple packet creation
8def create_pkt (size, direction):
9
10    ip_range = {'src': {'start': "10.0.0.1", 'end': "10.0.0.254"},
11                'dst': {'start': "8.0.0.1",  'end': "8.0.0.254"}}
12
13    if (direction == 0):
14        src = ip_range['src']
15        dst = ip_range['dst']
16    else:
17        src = ip_range['dst']
18        dst = ip_range['src']
19
20    vm = [
21        # src
22        STLVmFlowVar(name="src",min_value=src['start'],max_value=src['end'],size=4,op="inc"),
23        STLVmWrFlowVar(fv_name="src",pkt_offset= "IP.src"),
24
25        # dst
26        STLVmFlowVar(name="dst",min_value=dst['start'],max_value=dst['end'],size=4,op="inc"),
27        STLVmWrFlowVar(fv_name="dst",pkt_offset= "IP.dst"),
28
29        # checksum
30        STLVmFixIpv4(offset = "IP")
31        ]
32
33
34    base = Ether()/IP()/UDP()
35    pad = max(0, size-len(base)) * 'x'
36
37    return STLPktBuilder(pkt = base/pad,
38                         vm  = vm)
39
40
41def simple_burst (port_a, port_b, pkt_size, rate):
42
43
44    # create client
45    c = STLClient()
46    passed = True
47
48    try:
49        # turn this on for some information
50        #c.set_verbose("high")
51
52        # create two streams
53        s1 = STLStream(packet = create_pkt(pkt_size, 0),
54                       mode = STLTXCont(pps = 100))
55
56        # second stream with a phase of 1ms (inter stream gap)
57        s2 = STLStream(packet = create_pkt(pkt_size, 1),
58                       isg = 1000,
59                       mode = STLTXCont(pps = 100))
60
61
62        # connect to server
63        c.connect()
64
65        # prepare our ports
66        c.reset(ports = [port_a, port_b])
67
68        # add both streams to ports
69        c.add_streams(s1, ports = [port_a])
70        c.add_streams(s2, ports = [port_b])
71
72        # clear the stats before injecting
73        c.clear_stats()
74
75        # here we multiply the traffic lineaer to whatever given in rate
76        print("Running {:} on ports {:}, {:} for 10 seconds...".format(rate, port_a, port_b))
77        c.start(ports = [port_a, port_b], mult = rate, duration = 10)
78
79        # block until done
80        c.wait_on_traffic(ports = [port_a, port_b])
81
82        # read the stats after the test
83        stats = c.get_stats()
84
85        print(json.dumps(stats[port_a], indent = 4, separators=(',', ': '), sort_keys = True))
86        print(json.dumps(stats[port_b], indent = 4, separators=(',', ': '), sort_keys = True))
87
88        lost_a = stats[port_a]["opackets"] - stats[port_b]["ipackets"]
89        lost_b = stats[port_b]["opackets"] - stats[port_a]["ipackets"]
90
91        print("\npackets lost from {0} --> {1}:   {2} pkts".format(port_a, port_b, lost_a))
92        print("packets lost from {0} --> {1}:   {2} pkts".format(port_b, port_a, lost_b))
93
94        if c.get_warnings():
95            print("\n\n*** test had warnings ****\n\n")
96            for w in c.get_warnings():
97                print(w)
98
99        if (lost_a == 0) and (lost_b == 0) and not c.get_warnings():
100            passed = True
101        else:
102            passed = False
103
104    except STLError as e:
105        passed = False
106        print(e)
107
108    finally:
109        c.disconnect()
110
111    if passed:
112        print("\nTest has passed :-)\n")
113    else:
114        print("\nTest has failed :-(\n")
115
116# run the tests
117simple_burst(0, 3, 64, "10gbps")
118
119