1995267dbSimaromimport stl_path
27e2be1ddSimaromfrom trex_stl_lib.api import *
37351ddb5Simarom
47351ddb5Simaromimport time
57351ddb5Simaromimport json
67351ddb5Simarom
72887433cSimarom# simple packet creation
82887433cSimaromdef create_pkt (size, direction):
97351ddb5Simarom
102887433cSimarom    ip_range = {'src': {'start': "10.0.0.1", 'end': "10.0.0.254"},
112887433cSimarom                'dst': {'start': "8.0.0.1",  'end': "8.0.0.254"}}
122887433cSimarom
132887433cSimarom    if (direction == 0):
142887433cSimarom        src = ip_range['src']
152887433cSimarom        dst = ip_range['dst']
162887433cSimarom    else:
172887433cSimarom        src = ip_range['dst']
182887433cSimarom        dst = ip_range['src']
192887433cSimarom
202887433cSimarom    vm = [
212887433cSimarom        # src
22c8cd078fSimarom        STLVmFlowVar(name="src",min_value=src['start'],max_value=src['end'],size=4,op="inc"),
230e70a929Simarom        STLVmWrFlowVar(fv_name="src",pkt_offset= "IP.src"),
242887433cSimarom
252887433cSimarom        # dst
26c8cd078fSimarom        STLVmFlowVar(name="dst",min_value=dst['start'],max_value=dst['end'],size=4,op="inc"),
270e70a929Simarom        STLVmWrFlowVar(fv_name="dst",pkt_offset= "IP.dst"),
282887433cSimarom
292887433cSimarom        # checksum
302887433cSimarom        STLVmFixIpv4(offset = "IP")
312887433cSimarom        ]
327351ddb5Simarom
332887433cSimarom
342887433cSimarom    base = Ether()/IP()/UDP()
353c9c5ad0SHanoh Haim    pad = max(0, size-len(base)) * 'x'
362887433cSimarom
372887433cSimarom    return STLPktBuilder(pkt = base/pad,
382887433cSimarom                         vm  = vm)
392887433cSimarom
402887433cSimarom
4187bac1abSimaromdef simple_burst (port_a, port_b, pkt_size, rate):
427351ddb5Simarom
432887433cSimarom
447351ddb5Simarom    # create client
457351ddb5Simarom    c = STLClient()
467351ddb5Simarom    passed = True
477351ddb5Simarom
487351ddb5Simarom    try:
497351ddb5Simarom        # turn this on for some information
507351ddb5Simarom        #c.set_verbose("high")
517351ddb5Simarom
527351ddb5Simarom        # create two streams
5387bac1abSimarom        s1 = STLStream(packet = create_pkt(pkt_size, 0),
547351ddb5Simarom                       mode = STLTXCont(pps = 100))
557351ddb5Simarom
567351ddb5Simarom        # second stream with a phase of 1ms (inter stream gap)
5787bac1abSimarom        s2 = STLStream(packet = create_pkt(pkt_size, 1),
587351ddb5Simarom                       isg = 1000,
597351ddb5Simarom                       mode = STLTXCont(pps = 100))
607351ddb5Simarom
617351ddb5Simarom
627351ddb5Simarom        # connect to server
637351ddb5Simarom        c.connect()
647351ddb5Simarom
6587bac1abSimarom        # prepare our ports
6687bac1abSimarom        c.reset(ports = [port_a, port_b])
677351ddb5Simarom
687351ddb5Simarom        # add both streams to ports
6987bac1abSimarom        c.add_streams(s1, ports = [port_a])
7087bac1abSimarom        c.add_streams(s2, ports = [port_b])
717351ddb5Simarom
727351ddb5Simarom        # clear the stats before injecting
737351ddb5Simarom        c.clear_stats()
747351ddb5Simarom
7587bac1abSimarom        # here we multiply the traffic lineaer to whatever given in rate
7687bac1abSimarom        print("Running {:} on ports {:}, {:} for 10 seconds...".format(rate, port_a, port_b))
7787bac1abSimarom        c.start(ports = [port_a, port_b], mult = rate, duration = 10)
787351ddb5Simarom
797351ddb5Simarom        # block until done
8087bac1abSimarom        c.wait_on_traffic(ports = [port_a, port_b])
817351ddb5Simarom
827351ddb5Simarom        # read the stats after the test
837351ddb5Simarom        stats = c.get_stats()
847351ddb5Simarom
8587bac1abSimarom        print(json.dumps(stats[port_a], indent = 4, separators=(',', ': '), sort_keys = True))
8687bac1abSimarom        print(json.dumps(stats[port_b], indent = 4, separators=(',', ': '), sort_keys = True))
877351ddb5Simarom
8887bac1abSimarom        lost_a = stats[port_a]["opackets"] - stats[port_b]["ipackets"]
8987bac1abSimarom        lost_b = stats[port_b]["opackets"] - stats[port_a]["ipackets"]
907351ddb5Simarom
9187bac1abSimarom        print("\npackets lost from {0} --> {1}:   {2} pkts".format(port_a, port_b, lost_a))
9287bac1abSimarom        print("packets lost from {0} --> {1}:   {2} pkts".format(port_b, port_a, lost_b))
937351ddb5Simarom
9487bac1abSimarom        if c.get_warnings():
9587bac1abSimarom            print("\n\n*** test had warnings ****\n\n")
9687bac1abSimarom            for w in c.get_warnings():
9787bac1abSimarom                print(w)
9887bac1abSimarom
9987bac1abSimarom        if (lost_a == 0) and (lost_b == 0) and not c.get_warnings():
1007351ddb5Simarom            passed = True
1017351ddb5Simarom        else:
1027351ddb5Simarom            passed = False
1037351ddb5Simarom
1047351ddb5Simarom    except STLError as e:
1057351ddb5Simarom        passed = False
106738da2c0Simarom        print(e)
1077351ddb5Simarom
1087351ddb5Simarom    finally:
1097351ddb5Simarom        c.disconnect()
1107351ddb5Simarom
1117351ddb5Simarom    if passed:
112738da2c0Simarom        print("\nTest has passed :-)\n")
1137351ddb5Simarom    else:
114738da2c0Simarom        print("\nTest has failed :-(\n")
1157351ddb5Simarom
1167351ddb5Simarom# run the tests
11787bac1abSimaromsimple_burst(0, 3, 64, "10gbps")
1187351ddb5Simarom
119