CSIT-342 Update T-rex version to v2.07
[csit.git] / resources / tools / t-rex / t-rex-stateless.py
index 7ff4c37..455ecef 100755 (executable)
@@ -48,10 +48,14 @@ import string
 import struct
 import sys
 
-sys.path.insert(0, "/opt/trex-core-2.03/scripts/automation/"+\
+sys.path.insert(0, "/opt/trex-core-2.07/scripts/automation/"+\
                    "trex_control_plane/stl/")
 from trex_stl_lib.api import *
 
+stream_table = {'IMIX_v4_1': [{'size': 64, 'pps': 28, 'isg':0},
+                              {'size': 570, 'pps': 16, 'isg':0.1},
+                              {'size': 1518, 'pps': 4, 'isg':0.2}]
+               }
 
 def generate_payload(length):
     """Generate payload.
@@ -92,7 +96,7 @@ def get_start_end_ipv6(start_ip, end_ip):
             print "IPv6: start_ip is greater then end_ip"
             sys.exit(2)
 
-        max_p1 = abs(int(lo1) - int(lo2)) + 1
+        max_p1 = abs(int(lo1) - int(lo2))
         base_p1 = lo1
     except AddressValueError as ex_error:
         print ex_error
@@ -100,8 +104,75 @@ def get_start_end_ipv6(start_ip, end_ip):
 
     return base_p1, max_p1
 
+def create_streams_v46(base_pkt_a, base_pkt_b, vm1, vm2, frame_size):
+    """Create STLStream streams
+
+    :param base_pkt_a: Base packet a for stream_a
+    :param base_pkt_b: Base packet b for stream_b
+    :param vm1: vm for stream_a
+    :param vm2: vm for stream_b
+    :param frame_size: frame size or name of traffic profile
+    :type base_pkt_a: Eth (scapy)
+    :type base_pkt_b: Eth (scapy)
+    :type vm1: STLScVmRaw
+    :type vm2: STLScVmRaw
+    :frame_size: int or string
+    :return: stream_a, stream_b, stream_a_latency, stream_b_latency
+    :rtype: STLStream, STLStream, STLStream, STLStream
+    """
+
+    if type(frame_size) is int:
+
+        fsize_no_fcs = frame_size - 4 # no FCS
+        pkt_a = STLPktBuilder(pkt=base_pkt_a/generate_payload(
+            max(0, fsize_no_fcs-len(base_pkt_a))), vm=vm1)
+        pkt_b = STLPktBuilder(pkt=base_pkt_b/generate_payload(
+            max(0, fsize_no_fcs-len(base_pkt_b))), vm=vm2)
+        pkt_lat_a = STLPktBuilder(pkt=base_pkt_a/generate_payload(
+            max(0, fsize_no_fcs-len(base_pkt_a))))
+        pkt_lat_b = STLPktBuilder(pkt=base_pkt_b/generate_payload(
+            max(0, fsize_no_fcs-len(base_pkt_b))))
+        lat_stream1 = STLStream(packet=pkt_lat_a,
+                                flow_stats=STLFlowLatencyStats(pg_id=0),
+                                mode=STLTXCont(pps=1000))
+        # second traffic stream with a phase of 10ns (inter stream gap)
+        lat_stream2 = STLStream(packet=pkt_lat_b,
+                                isg=10.0,
+                                flow_stats=STLFlowLatencyStats(pg_id=1),
+                                mode=STLTXCont(pps=1000))
+
+        stream1 = STLStream(packet=pkt_a,
+                            mode=STLTXCont(pps=1000))
+        # second traffic stream with a phase of 10ns (inter stream gap)
+        stream2 = STLStream(packet=pkt_b,
+                            isg=10.0,
+                            mode=STLTXCont(pps=1000))
+    elif type(frame_size) is str:
+        lat_stream1 = []
+        lat_stream2 = []
+        stream1 = []
+        stream2 = []
+
+        for x in stream_table[frame_size]:
+            fsize_no_fcs = x['size'] - 4 # no FCS
+            pkt_a = STLPktBuilder(pkt=base_pkt_a/generate_payload(
+                max(0, fsize_no_fcs-len(base_pkt_a))), vm=vm1)
+            pkt_b = STLPktBuilder(pkt=base_pkt_b/generate_payload(
+                max(0, fsize_no_fcs-len(base_pkt_b))), vm=vm2)
+
+            stream1.append(STLStream(packet=pkt_a,
+                               isg=x['isg'],
+                               mode=STLTXCont(pps=x['pps'])))
+            stream2.append(STLStream(packet=pkt_b,
+                               isg=x['isg'],
+                               mode=STLTXCont(pps=x['pps'])))
+
+    else:
+        raise ValueError("Unknown frame_size type")
 
-def create_packets(traffic_options, frame_size=64):
+    return (stream1, stream2, lat_stream1, lat_stream2)
+
+def create_streams(traffic_options, frame_size=64):
     """Create two IP packets to be used in stream.
 
     :param traffic_options: Parameters for packets.
@@ -112,12 +183,10 @@ def create_packets(traffic_options, frame_size=64):
     :rtype: Tuple of STLPktBuilder
     """
 
-    if frame_size < 64:
-        print_error("Packet min. size is 64B")
+    if type(frame_size) is int and frame_size < 64:
+        print_error("Frame min. size is 64B")
         sys.exit(1)
 
-    fsize_no_fcs = frame_size - 4 # no FCS
-
     p1_src_start_ip = traffic_options['p1_src_start_ip']
     p1_src_end_ip = traffic_options['p1_src_end_ip']
     p1_dst_start_ip = traffic_options['p1_dst_start_ip']
@@ -125,43 +194,50 @@ def create_packets(traffic_options, frame_size=64):
     p2_src_end_ip = traffic_options['p2_src_end_ip']
     p2_dst_start_ip = traffic_options['p2_dst_start_ip']
 
+    p1_dst_end_ip = traffic_options['p1_dst_end_ip']
+    p2_dst_end_ip = traffic_options['p2_dst_end_ip']
+
     base_pkt_a = Ether()/IP(src=p1_src_start_ip, dst=p1_dst_start_ip, proto=61)
     base_pkt_b = Ether()/IP(src=p2_src_start_ip, dst=p2_dst_start_ip, proto=61)
 
-    # The following code applies raw instructions to packet (IP src increment).
-    # It splits the generated traffic by "ip_src" variable to cores and fix
-    # IPv4 header checksum.
-    vm1 = STLScVmRaw([STLVmFlowVar(name="src",
-                                   min_value=p1_src_start_ip,
-                                   max_value=p1_src_end_ip,
-                                   size=4, op="inc"),
-                      STLVmWrFlowVar(fv_name="src", pkt_offset="IP.src"),
-                      STLVmFixIpv4(offset="IP"),
-                     ], split_by_field="src")
-    # The following code applies raw instructions to packet (IP src increment).
-    # It splits the generated traffic by "ip_src" variable to cores and fix
-    # IPv4 header checksum.
-    vm2 = STLScVmRaw([STLVmFlowVar(name="src",
-                                   min_value=p2_src_start_ip,
-                                   max_value=p2_src_end_ip,
-                                   size=4, op="inc"),
-                      STLVmWrFlowVar(fv_name="src", pkt_offset="IP.src"),
-                      STLVmFixIpv4(offset="IP"),
-                     ], split_by_field="src")
-
-    pkt_a = STLPktBuilder(pkt=base_pkt_a/generate_payload(
-        max(0, fsize_no_fcs-len(base_pkt_a))), vm=vm1)
-    pkt_b = STLPktBuilder(pkt=base_pkt_b/generate_payload(
-        max(0, fsize_no_fcs-len(base_pkt_b))), vm=vm2)
-    lat_a = STLPktBuilder(pkt=base_pkt_a/generate_payload(
-        max(0, fsize_no_fcs-len(base_pkt_a))))
-    lat_b = STLPktBuilder(pkt=base_pkt_b/generate_payload(
-        max(0, fsize_no_fcs-len(base_pkt_b))))
-
-    return(pkt_a, pkt_b, lat_a, lat_b)
-
-
-def create_packets_v6(traffic_options, frame_size=78):
+    # The following code applies raw instructions to packet (IP src/dst
+    # increment). It splits the generated traffic by "ip_src"/"ip_dst" variable
+    # to cores and fix IPv4 header checksum.
+    if p1_dst_end_ip and p2_dst_end_ip:
+        vm1 = STLScVmRaw([STLVmFlowVar(name="dst",
+                                       min_value=p1_dst_start_ip,
+                                       max_value=p1_dst_end_ip,
+                                       size=4, op="inc"),
+                          STLVmWrFlowVar(fv_name="dst", pkt_offset="IP.dst"),
+                          STLVmFixIpv4(offset="IP"),
+                         ], split_by_field="dst")
+        vm2 = STLScVmRaw([STLVmFlowVar(name="dst",
+                                       min_value=p2_dst_start_ip,
+                                       max_value=p2_dst_end_ip,
+                                       size=4, op="inc"),
+                          STLVmWrFlowVar(fv_name="dst", pkt_offset="IP.dst"),
+                          STLVmFixIpv4(offset="IP"),
+                         ], split_by_field="dst")
+    else:
+        vm1 = STLScVmRaw([STLVmFlowVar(name="src",
+                                       min_value=p1_src_start_ip,
+                                       max_value=p1_src_end_ip,
+                                       size=4, op="inc"),
+                          STLVmWrFlowVar(fv_name="src", pkt_offset="IP.src"),
+                          STLVmFixIpv4(offset="IP"),
+                         ], split_by_field="src")
+        vm2 = STLScVmRaw([STLVmFlowVar(name="src",
+                                       min_value=p2_src_start_ip,
+                                       max_value=p2_src_end_ip,
+                                       size=4, op="inc"),
+                          STLVmWrFlowVar(fv_name="src", pkt_offset="IP.src"),
+                          STLVmFixIpv4(offset="IP"),
+                         ], split_by_field="src")
+
+    return create_streams_v46(base_pkt_a, base_pkt_b, vm1, vm2, frame_size)
+
+
+def create_streams_v6(traffic_options, frame_size=78):
     """Create two IPv6 packets to be used in stream.
 
     :param traffic_options: Parameters for packets.
@@ -172,12 +248,10 @@ def create_packets_v6(traffic_options, frame_size=78):
     :rtype: Tuple of STLPktBuilder
     """
 
-    if frame_size < 78:
-        print "Packet min. size is 78B"
+    if type(frame_size) is int and frame_size < 78:
+        print_error("Frame min. size is 78B")
         sys.exit(2)
 
-    fsize_no_fcs = frame_size - 4 # no FCS
-
     p1_src_start_ip = traffic_options['p1_src_start_ip']
     p1_src_end_ip = traffic_options['p1_src_end_ip']
     p1_dst_start_ip = traffic_options['p1_dst_start_ip']
@@ -185,47 +259,63 @@ def create_packets_v6(traffic_options, frame_size=78):
     p2_src_end_ip = traffic_options['p2_src_end_ip']
     p2_dst_start_ip = traffic_options['p2_dst_start_ip']
 
-    base_p1, max_p1 = get_start_end_ipv6(p1_src_start_ip, p1_src_end_ip)
-    base_p2, max_p2 = get_start_end_ipv6(p2_src_start_ip, p2_src_end_ip)
+    p1_dst_end_ip = traffic_options['p1_dst_end_ip']
+    p2_dst_end_ip = traffic_options['p2_dst_end_ip']
 
     base_pkt_a = Ether()/IPv6(src=p1_src_start_ip, dst=p1_dst_start_ip)
     base_pkt_b = Ether()/IPv6(src=p2_src_start_ip, dst=p2_dst_start_ip)
 
-    # The following code applies raw instructions to packet (IP src increment).
-    # It splits the generated traffic by "ip_src" variable to cores
-    vm1 = STLScVmRaw([STLVmFlowVar(name="ipv6_src",
-                                   min_value=base_p1,
-                                   max_value=max_p1+base_p1,
-                                   size=8, op="inc"),
-                      STLVmWrFlowVar(fv_name="ipv6_src", pkt_offset="IPv6.src",
-                                     offset_fixup=8)
-                     ]
-                     , split_by_field="ipv6_src")
-
-    # The following code applies raw instructions to packet (IP src increment).
-    # It splits the generated traffic by "ip_src" variable to cores
-    vm2 = STLScVmRaw([STLVmFlowVar(name="ipv6_src",
-                                   min_value=base_p2,
-                                   max_value=max_p2+base_p2,
-                                   size=8, op="inc"),
-                      STLVmWrFlowVar(fv_name="ipv6_src", pkt_offset="IPv6.src",
-                                     offset_fixup=8)
-                     ]
-                     , split_by_field="ipv6_src")
-
-    pkt_a = STLPktBuilder(pkt=base_pkt_a/generate_payload(
-        max(0, fsize_no_fcs-len(base_pkt_a))), vm=vm1)
-    pkt_b = STLPktBuilder(pkt=base_pkt_b/generate_payload(
-        max(0, fsize_no_fcs-len(base_pkt_b))), vm=vm2)
-    lat_a = STLPktBuilder(pkt=base_pkt_a/generate_payload(
-        max(0, fsize_no_fcs-len(base_pkt_a))))
-    lat_b = STLPktBuilder(pkt=base_pkt_b/generate_payload(
-        max(0, fsize_no_fcs-len(base_pkt_b))))
-
-    return(pkt_a, pkt_b, lat_a, lat_b)
-
-
-def simple_burst(pkt_a, pkt_b, pkt_lat_a, pkt_lat_b, duration, rate,
+    # The following code applies raw instructions to packet (IP src/dst
+    # increment). It splits the generated traffic by "ip_src"/"ip_dst" variable
+    # to cores.
+    if p1_dst_end_ip and p2_dst_end_ip:
+        base_p1, max_p1 = get_start_end_ipv6(p1_dst_start_ip, p1_dst_end_ip)
+        base_p2, max_p2 = get_start_end_ipv6(p2_dst_start_ip, p2_dst_end_ip)
+
+        vm1 = STLScVmRaw([STLVmFlowVar(name="ipv6_dst",
+                                       min_value=base_p1,
+                                       max_value=max_p1+base_p1,
+                                       size=8, op="inc"),
+                          STLVmWrFlowVar(fv_name="ipv6_dst",
+                                         pkt_offset="IPv6.dst",
+                                         offset_fixup=8)
+                         ]
+                         , split_by_field="ipv6_dst")
+        vm2 = STLScVmRaw([STLVmFlowVar(name="ipv6_dst",
+                                       min_value=base_p2,
+                                       max_value=max_p2+base_p2,
+                                       size=8, op="inc"),
+                          STLVmWrFlowVar(fv_name="ipv6_dst",
+                                         pkt_offset="IPv6.dst",
+                                         offset_fixup=8)
+                         ]
+                         , split_by_field="ipv6_dst")
+    else:
+        base_p1, max_p1 = get_start_end_ipv6(p1_src_start_ip, p1_src_end_ip)
+        base_p2, max_p2 = get_start_end_ipv6(p2_src_start_ip, p2_src_end_ip)
+
+        vm1 = STLScVmRaw([STLVmFlowVar(name="ipv6_src",
+                                       min_value=base_p1,
+                                       max_value=max_p1+base_p1,
+                                       size=8, op="inc"),
+                          STLVmWrFlowVar(fv_name="ipv6_src",
+                                         pkt_offset="IPv6.src",
+                                         offset_fixup=8)
+                         ]
+                         , split_by_field="ipv6_src")
+        vm2 = STLScVmRaw([STLVmFlowVar(name="ipv6_src",
+                                       min_value=base_p2,
+                                       max_value=max_p2+base_p2,
+                                       size=8, op="inc"),
+                          STLVmWrFlowVar(fv_name="ipv6_src",
+                                         pkt_offset="IPv6.src",
+                                         offset_fixup=8)
+                         ]
+                         , split_by_field="ipv6_src")
+
+    return create_streams_v46(base_pkt_a, base_pkt_b, vm1, vm2, frame_size)
+
+def simple_burst(stream_a, stream_b, stream_lat_a, stream_lat_b, duration, rate,
                  warmup_time, async_start, latency):
     """Run the traffic with specific parameters.
 
@@ -270,28 +360,12 @@ def simple_burst(pkt_a, pkt_b, pkt_lat_a, pkt_lat_b, duration, rate,
         # prepare our ports (my machine has 0 <--> 1 with static route)
         client.reset(ports=[0, 1])
 
-        # create two traffic streams without latency stats
-        stream1 = STLStream(packet=pkt_a,
-                            mode=STLTXCont(pps=1000))
-        # second traffic stream with a phase of 10ns (inter stream gap)
-        stream2 = STLStream(packet=pkt_b,
-                            isg=10.0,
-                            mode=STLTXCont(pps=1000))
-        client.add_streams(stream1, ports=[0])
-        client.add_streams(stream2, ports=[1])
+        client.add_streams(stream_a, ports=[0])
+        client.add_streams(stream_b, ports=[1])
 
         if latency:
-            # create two traffic streams with latency stats
-            lat_stream1 = STLStream(packet=pkt_lat_a,
-                                    flow_stats=STLFlowLatencyStats(pg_id=0),
-                                    mode=STLTXCont(pps=1000))
-            # second traffic stream with a phase of 10ns (inter stream gap)
-            lat_stream2 = STLStream(packet=pkt_lat_b,
-                                    isg=10.0,
-                                    flow_stats=STLFlowLatencyStats(pg_id=1),
-                                    mode=STLTXCont(pps=1000))
-            client.add_streams(lat_stream1, ports=[0])
-            client.add_streams(lat_stream2, ports=[1])
+            client.add_streams(stream_lat_a, ports=[0])
+            client.add_streams(stream_lat_b, ports=[1])
 
         #warmup phase
         if warmup_time > 0:
@@ -397,7 +471,7 @@ def parse_args():
     parser = argparse.ArgumentParser()
     parser.add_argument("-d", "--duration", required=True, type=int,
                         help="Duration of traffic run")
-    parser.add_argument("-s", "--frame_size", required=True, type=int,
+    parser.add_argument("-s", "--frame_size", required=True,
                         help="Size of a Frame without padding and IPG")
     parser.add_argument("-r", "--rate", required=True,
                         help="Traffic rate with included units (%, pps)")
@@ -419,24 +493,28 @@ def parse_args():
 #                        help="Port 1 destination MAC address")
     parser.add_argument("--p1_src_start_ip", required=True,
                         help="Port 1 source start IP address")
-    parser.add_argument("--p1_src_end_ip", required=True,
+    parser.add_argument("--p1_src_end_ip",
+                        default=False,
                         help="Port 1 source end IP address")
     parser.add_argument("--p1_dst_start_ip", required=True,
                         help="Port 1 destination start IP address")
-#    parser.add_argument("--p1_dst_end_ip",
-#                        help="Port 1 destination end IP address")
+    parser.add_argument("--p1_dst_end_ip",
+                        default=False,
+                        help="Port 1 destination end IP address")
 #    parser.add_argument("--p2_src_mac",
 #                        help="Port 2 source MAC address")
 #    parser.add_argument("--p2_dst_mac",
 #                        help="Port 2 destination MAC address")
     parser.add_argument("--p2_src_start_ip", required=True,
                         help="Port 2 source start IP address")
-    parser.add_argument("--p2_src_end_ip", required=True,
+    parser.add_argument("--p2_src_end_ip",
+                        default=False,
                         help="Port 2 source end IP address")
     parser.add_argument("--p2_dst_start_ip", required=True,
                         help="Port 2 destination start IP address")
-#    parser.add_argument("--p2_dst_end_ip",
-#                        help="Port 2 destination end IP address")
+    parser.add_argument("--p2_dst_end_ip",
+                        default=False,
+                        help="Port 2 destination end IP address")
 
     return parser.parse_args()
 
@@ -447,11 +525,15 @@ def main():
     args = parse_args()
 
     _duration = args.duration
-    _frame_size = args.frame_size
+    _latency = args.latency
+    if args.frame_size.isdigit():
+        _frame_size = int(args.frame_size)
+    else:
+        _frame_size = args.frame_size
+        _latency = False
     _rate = args.rate
     _use_ipv6 = args.use_IPv6
     _async_call = args.async
-    _latency = args.latency
     _warmup_time = args.warmup_time
 
     _traffic_options = {}
@@ -464,14 +546,14 @@ def main():
         print_error('IPv6 latency is not supported yet. Running without lat.')
         _latency = False
 
-        pkt_a, pkt_b, lat_a, lat_b = create_packets_v6(_traffic_options,
-                                                       frame_size=_frame_size)
+        stream_a, stream_b, stream_lat_a, stream_lat_b = create_streams_v6(
+            _traffic_options, frame_size=_frame_size)
     else:
-        pkt_a, pkt_b, lat_a, lat_b = create_packets(_traffic_options,
-                                                    frame_size=_frame_size)
+        stream_a, stream_b, stream_lat_a, stream_lat_b = create_streams(
+            _traffic_options, frame_size=_frame_size)
 
-    simple_burst(pkt_a, pkt_b, lat_a, lat_b, _duration, _rate, _warmup_time,
-                 _async_call, _latency)
+    simple_burst(stream_a, stream_b, stream_lat_a, stream_lat_b,
+                 _duration, _rate, _warmup_time, _async_call, _latency)
 
 if __name__ == "__main__":
     sys.exit(main())