to run concurrent flows in trex using python script

122 views
Skip to first unread message

Fanish Chandra

unread,
May 29, 2023, 9:05:40 AM5/29/23
to TRex Traffic Generator
hi 
I want that if one flow is on running , then next flow should be added such a way that previous flow is still running and next flow is added to previous flow , so both flows are running , now if another flow come into existence , then this flow should be added to previous two flow and now three flows are running
I want to fulfill above condition and right now using v2.88 version of trex
def create_flow (frame_size=9000, direction=0 ,src_ip=None, dst_ip=None):
     if direction == 0:
        src = {'start': src_ip['start'], 'end': src_ip['end']}
        dst = {'start': dst_ip['start'], 'end': dst_ip['end']}
     else:
        src = {'start': dst_ip['start'], 'end': dst_ip['end']}
        dst = {'start': src_ip['start'], 'end': src_ip['end']}

     pkt_base  = Ether()/IP(src=dst_ip['start'], dst=src_ip['start'], tos=0xe0)/UDP(dport=102,sport=20,chksum=0)
     pyld_size = frame_size - len(pkt_base)
     pkt_pyld  = generate_payload(pyld_size)
     #STLPktBuilder(pkt = pkt_base/pkt_pyld)
     stream = STLStream(packet= STLPktBuilder(pkt = pkt_base/pkt_pyld),mode=STLTXCont(pps=1))
     #stream = STLStream(packet= STLPktBuilder(pkt = pkt_base/pkt_pyld),mode=STLTXSingleBurst(pps=100))
     return stream
    #STLPktBuilder(pkt = pkt_base/pkt_pyld)



def run_simulation(duration=100,frame_size=200, speed='50mbps', ip_range={'src': {'start': "32.32.10.1", 'end': "32.32.10.2"}, 'dst': {'start': "192.18.0.26", 'end': "192.18.0.26"}}):
    speed='10mbps'

    integer_value = int(''.join(re.findall(r'\d+', speed)))
   
    if (frame_size < 60):
        frame_size = 60
   
    passed = True
    # create client
    c = STLClient(server = t_global.args.ip)
    # Calculate the number of flows based on the IP range
    start_ip = ipaddress.ip_address(ip_range['src']['start'])
    end_ip = ipaddress.ip_address(ip_range['src']['end'])
    num_flows = int(end_ip) - int(start_ip) + 1

    # Calculate the total pps value for all flows based on the desired speed
    total_pps = integer_value* 1000  # Convert Mbps to kbps
    pps_per_flow = total_pps / num_flows
    active_streams = []  # List to store the active streams
    active_ports = [1]
    UL_streams =[]

    try:
       
        # Create multiple flows
        flows = []
        for i in range(num_flows):
            #src_ip = str(ipaddress.ip_address(int(start_ip) + i))
            src_ip = { 'start': str(start_ip + i), 'end': str(start_ip + i) }
            #flow = {'src_ip': src_ip, 'dst_ip': ip_range['dst']['start'], 'src_port': 1234, 'dst_port': 80, 'packet_size': 64, 'pps': pps_per_flow}
            #flow = {'src_ip': src_ip, 'dst_ip': ip_range['dst'], 'packet_size': frame_size}
            flow = {'src_ip': src_ip, 'dst_ip': ip_range['dst'], 'packet_size': frame_size}
            flows.append(flow)

         # Create and configure streams for each flow
       
       
        c.connect()
        #Prepare the ports for traffic generation
        c.reset(ports=[1])

       

        streams = []
        for flow in flows:
            new_stream = create_flow(flow['packet_size'], 1 ,flow['src_ip'], flow['dst_ip'] )
            UL_streams.append(new_stream)
            print("\n Error Occured 4 \n ")

            #if active_ports:
            #    for port in active_ports:
            #        c.add_streams(new_stream, ports=[port])
            #else:
            #    active_ports.append(1)

            #if active_streams:
            #    # If there are active streams, update them by combining the new stream
            #    for stream in active_streams:
            #     print("\n Error Occured 5 \n ")

            #     stream.packets += new_stream.packets

            #else:
            #    print("\n Error Occured 6 \n ")
            #    # If there are no active streams, add the new stream to the active streams list
            #    active_streams.append(new_stream)    


            # Add the current stream to the list of active streams
            #active_streams.append(stream)

            # Add the active streams to the traffic generator
            #c.add_streams(active_streams, ports=[1])
            #streams.append(stream)
            #c.add_streams([stream], ports=[1])
            #c.add_streams(stream, ports=[1])
            # Update the active streams in the traffic generator
            print("\n Error Occured 7 \n ")
            #c.update_streams()
            print("\n Error Occured 8 \n ")
            c.clear_stats()
            print("Running {0} on ports 0, 1 for 10 seconds, UDP {1}...".format(speed,frame_size+4))
            c.start(ports = 1, mult = speed, duration = 60)
            #c.wait_on_traffic(ports = 1)
            stats = c.get_stats()
            print(json.dumps(stats[0], indent = 4, separators=(',', ': '), sort_keys = True))
            print(json.dumps(stats[1], indent = 4, separators=(',', ': '), sort_keys = True))

            lost_a = stats[0]["opackets"] - stats[1]["ipackets"]
            lost_b = stats[1]["opackets"] - stats[0]["ipackets"]

            print("\npackets lost from 0 --> 1:   {0} pkts".format(lost_a))
            print("packets lost from   1 --> 0:   {0} pkts".format(lost_b))
            if (lost_a == 0) and (lost_b == 0):
                passed = True
            else:
                passed = False


           
            except STLError as e:    
        print(e)
        print("\n Error Occured \n ")
        passed = False
    finally:
        c.disconnect()

    if passed:
        print("\nPASSED\n")
    else:
        print("\nFAILED\n")    
Using above code we can't satsify the requirement Right now I am usinf scapy server 2.5.0 Is it possible to achieve the above condition by any means in trex ?
Reply all
Reply to author
Forward
0 new messages