diff options
author | 2016-02-04 10:31:04 +0200 | |
---|---|---|
committer | 2016-02-04 10:31:04 +0200 | |
commit | eaa1928aebec0d8069a1466e2c421d3dfb5b16f0 (patch) | |
tree | 7ba4f23c5308aad0102b1d6e106f2390bc1a19f8 /scripts/api/stl | |
parent | d95c37b1d2b3c9d7c2e1942b4b8ee18f22464f72 (diff) | |
parent | 51f3f97dfcd156b79b10331312d238775443e23c (diff) |
Merge simulation fix
Diffstat (limited to 'scripts/api/stl')
-rw-r--r-- | scripts/api/stl/examples/stl_bi_dir_flows.py | 77 | ||||
-rw-r--r-- | scripts/api/stl/examples/stl_imix.py | 106 | ||||
-rw-r--r-- | scripts/api/stl/examples/stl_simple_burst.py | 52 | ||||
-rw-r--r-- | scripts/api/stl/trex_stl_api.py | 12 | ||||
-rw-r--r-- | scripts/api/stl/trex_stl_lib.py | 71 |
5 files changed, 236 insertions, 82 deletions
diff --git a/scripts/api/stl/examples/stl_bi_dir_flows.py b/scripts/api/stl/examples/stl_bi_dir_flows.py index 38cb36dd..7d090345 100644 --- a/scripts/api/stl/examples/stl_bi_dir_flows.py +++ b/scripts/api/stl/examples/stl_bi_dir_flows.py @@ -4,51 +4,46 @@ sys.path.insert(0, "../") from trex_stl_api import * -import dpkt import time import json +# simple packet creation +def create_pkt (size, direction): -def simple_burst (): - - # build A side packet - pkt_a = STLPktBuilder() - - pkt_a.add_pkt_layer("l2", dpkt.ethernet.Ethernet()) - pkt_a.add_pkt_layer("l3_ip", dpkt.ip.IP()) - pkt_a.add_pkt_layer("l4_udp", dpkt.udp.UDP()) - pkt_a.set_pkt_payload("somepayload") - pkt_a.set_layer_attr("l3_ip", "len", len(pkt_a.get_layer('l3_ip'))) - - # build B side packet - pkt_b = pkt_a.clone() - - # set IP range for pkt and split it by multiple cores - pkt_a.set_vm_ip_range(ip_layer_name = "l3_ip", - ip_field = "src", - ip_start="10.0.0.1", ip_end="10.0.0.254", - operation = "inc", - split = True) - - pkt_a.set_vm_ip_range(ip_layer_name = "l3_ip", - ip_field = "dst", - ip_start="8.0.0.1", ip_end="8.0.0.254", - operation = "inc") - - - # build B side packet - pkt_b.set_vm_ip_range(ip_layer_name = "l3_ip", - ip_field = "src", - ip_start="8.0.0.1", ip_end="8.0.0.254", - operation = "inc", - split = True) - - pkt_b.set_vm_ip_range(ip_layer_name = "l3_ip", - ip_field = "dst", - ip_start="10.0.0.1", ip_end="10.0.0.254", - operation = "inc") + ip_range = {'src': {'start': "10.0.0.1", 'end': "10.0.0.254"}, + 'dst': {'start': "8.0.0.1", 'end': "8.0.0.254"}} + + if (direction == 0): + src = ip_range['src'] + dst = ip_range['dst'] + else: + src = ip_range['dst'] + dst = ip_range['src'] + + vm = [ + # src + STLVmFlowVar(name="src",min_value=src['start'],max_value=src['end'],size=4,op="inc"), + STLVmWriteFlowVar(fv_name="src",pkt_offset= "IP.src"), + + # dst + STLVmFlowVar(name="dst",min_value=dst['start'],max_value=dst['end'],size=4,op="inc"), + STLVmWriteFlowVar(fv_name="dst",pkt_offset= "IP.dst"), + + # checksum + STLVmFixIpv4(offset = "IP") + ] + + base = Ether()/IP()/UDP() + pad = max(0, len(base)) * 'x' + + return STLPktBuilder(pkt = base/pad, + vm = vm) + + +def simple_burst (): + # create client c = STLClient() passed = True @@ -58,11 +53,11 @@ def simple_burst (): #c.set_verbose("high") # create two streams - s1 = STLStream(packet = pkt_a, + s1 = STLStream(packet = create_pkt(200, 0), mode = STLTXCont(pps = 100)) # second stream with a phase of 1ms (inter stream gap) - s2 = STLStream(packet = pkt_b, + s2 = STLStream(packet = create_pkt(200, 1), isg = 1000, mode = STLTXCont(pps = 100)) diff --git a/scripts/api/stl/examples/stl_imix.py b/scripts/api/stl/examples/stl_imix.py new file mode 100644 index 00000000..01eec9b4 --- /dev/null +++ b/scripts/api/stl/examples/stl_imix.py @@ -0,0 +1,106 @@ +# include the path of trex_stl_api.py +import sys +sys.path.insert(0, "../") + +from trex_stl_api import * +from trex_stl_lib import * +from profiles.imix import STLImix + +import time +import json +from pprint import pprint + +# IMIX test +# it maps the ports to sides +# then it load a predefind profile 'IMIX' +# and attach it to both sides and inject +# at a certain rate for some time +# finally it checks that all packets arrived +def imix_test (): + + + # create client + c = STLClient() + passed = True + + + try: + + # base profile - imix + profile = STLImix() + + # connect to server + c.connect() + + # take all the ports + c.reset() + + # map ports - identify the routes + table = stl_map_ports(c) + + print "Mapped ports to sides {0} <--> {1}".format(table['dir'][0], table['dir'][1]) + dir_0 = table['dir'][0] + dir_1 = table['dir'][1] + + # add both streams to ports + c.add_streams(profile.get_streams(direction = 0), ports = dir_0) + c.add_streams(profile.get_streams(direction = 1), ports = dir_1) + + # clear the stats before injecting + c.clear_stats() + + # choose rate and start traffic for 10 seconds on 5 mpps + duration = 10 + mult = "5mpps" + print "Injecting {0} <--> {1} on total rate of '{2}' for {3} seconds".format(dir_0, dir_1, mult, duration) + + c.start(ports = (dir_0 + dir_1), mult = mult, duration = duration, total = True) + + # block until done + c.wait_on_traffic(ports = (dir_0 + dir_1)) + + # read the stats after the test + stats = c.get_stats() + + # use this for debug info on all the stats + #pprint(stats) + + # sum dir 0 + dir_0_opackets = sum([stats[i]["opackets"] for i in dir_0]) + dir_0_ipackets = sum([stats[i]["ipackets"] for i in dir_0]) + + # sum dir 1 + dir_1_opackets = sum([stats[i]["opackets"] for i in dir_1]) + dir_1_ipackets = sum([stats[i]["ipackets"] for i in dir_1]) + + + lost_0 = dir_0_opackets - dir_1_ipackets + lost_1 = dir_1_opackets - dir_0_ipackets + + print "\nPackets injected from {0}: {1:,}".format(dir_0, dir_0_opackets) + print "Packets injected from {0}: {1:,}".format(dir_1, dir_1_opackets) + + print "\npackets lost from {0} --> {1}: {2:,} pkts".format(dir_0, dir_0, lost_0) + print "packets lost from {0} --> {1}: {2:,} pkts".format(dir_0, dir_0, lost_0) + + if (lost_0 == 0) and (lost_0 == 0): + passed = True + else: + passed = False + + except STLError as e: + passed = False + print e + + finally: + c.disconnect() + + if passed: + print "\nTest has passed :-)\n" + else: + print "\nTest has failed :-(\n" + + +# run the tests +imix_test() + diff --git a/scripts/api/stl/examples/stl_simple_burst.py b/scripts/api/stl/examples/stl_simple_burst.py index 2ca71b44..0de4df89 100644 --- a/scripts/api/stl/examples/stl_simple_burst.py +++ b/scripts/api/stl/examples/stl_simple_burst.py @@ -2,49 +2,25 @@ import sys sys.path.insert(0, "../") from trex_stl_api import * -import dpkt +from scapy.all import * import time def simple_burst (): - - # build a simple packet - - pkt_bld = STLPktBuilder() - pkt_bld.add_pkt_layer("l2", dpkt.ethernet.Ethernet()) - # set Ethernet layer attributes - pkt_bld.set_eth_layer_addr("l2", "src", "00:15:17:a7:75:a3") - pkt_bld.set_eth_layer_addr("l2", "dst", "e0:5f:b9:69:e9:22") - pkt_bld.set_layer_attr("l2", "type", dpkt.ethernet.ETH_TYPE_IP) - # set IP layer attributes - pkt_bld.add_pkt_layer("l3_ip", dpkt.ip.IP()) - pkt_bld.set_ip_layer_addr("l3_ip", "src", "21.0.0.2") - pkt_bld.set_ip_layer_addr("l3_ip", "dst", "22.0.0.12") - pkt_bld.set_layer_attr("l3_ip", "p", dpkt.ip.IP_PROTO_TCP) - # set TCP layer attributes - pkt_bld.add_pkt_layer("l4_tcp", dpkt.tcp.TCP()) - pkt_bld.set_layer_attr("l4_tcp", "sport", 13311) - pkt_bld.set_layer_attr("l4_tcp", "dport", 80) - pkt_bld.set_layer_attr("l4_tcp", "flags", 0) - pkt_bld.set_layer_attr("l4_tcp", "win", 32768) - pkt_bld.set_layer_attr("l4_tcp", "seq", 0) - #pkt_bld.set_pkt_payload("abcdefgh") - pkt_bld.set_layer_attr("l3_ip", "len", len(pkt_bld.get_layer('l3_ip'))) - - + + # create client c = STLClient() passed = True - + try: - - #c.set_verbose("high") - # create two bursts and link them - s1 = STLStream(packet = pkt_bld, - mode = STLTXSingleBurst(total_pkts = 5000) - ) + pkt = STLPktBuilder(pkt = Ether()/IP(src="16.0.0.1",dst="48.0.0.1")/UDP(dport=12,sport=1025)/IP()/'a_payload_example') - s2 = STLStream(packet = pkt_bld, + # create two bursts and link them + s1 = STLStream(packet = pkt, + mode = STLTXSingleBurst(total_pkts = 5000)) + + s2 = STLStream(packet = pkt, mode = STLTXSingleBurst(total_pkts = 3000), next_stream_id = s1.get_id()) @@ -53,16 +29,16 @@ def simple_burst (): c.connect() # prepare our ports - c.reset(ports = [0, 1]) + c.reset(ports = [0, 3]) # add both streams to ports - stream_ids = c.add_streams([s1, s2], ports = [0, 1]) + stream_ids = c.add_streams([s1, s2], ports = [0, 3]) # run 5 times for i in xrange(1, 6): c.clear_stats() - c.start(ports = [0, 1], mult = "1gbps") - c.wait_on_traffic(ports = [0, 1]) + c.start(ports = [0, 3], mult = "1gbps") + c.wait_on_traffic(ports = [0, 3]) stats = c.get_stats() ipackets = stats['total']['ipackets'] diff --git a/scripts/api/stl/trex_stl_api.py b/scripts/api/stl/trex_stl_api.py index 0bfd1181..3ef7eaaa 100644 --- a/scripts/api/stl/trex_stl_api.py +++ b/scripts/api/stl/trex_stl_api.py @@ -1,7 +1,8 @@ import os import sys import time - +import json +import math # update the import path to include the stateless client root_path = os.path.dirname(os.path.abspath(__file__)) @@ -11,7 +12,7 @@ sys.path.insert(0, os.path.join(root_path, '../../stl/')) # aliasing import common.trex_streams -from client_utils.packet_builder import CTRexPktBuilder +from client_utils.scapy_packet_builder import * import common.trex_stl_exceptions import client.trex_stateless_client import client.trex_stateless_sim @@ -27,7 +28,12 @@ STLTXSingleBurst = common.trex_streams.STLTXSingleBurst STLTXMultiBurst = common.trex_streams.STLTXMultiBurst # packet builder -STLPktBuilder = CTRexPktBuilder +STLPktBuilder = CScapyTRexPktBuilder + +# VM +STLVmFlowVar = CTRexVmDescFlowVar +STLVmWriteFlowVar = CTRexVmDescWrFlowVar +STLVmFixIpv4 = CTRexVmDescFixIpv4 # simulator STLSim = client.trex_stateless_sim.STLSim diff --git a/scripts/api/stl/trex_stl_lib.py b/scripts/api/stl/trex_stl_lib.py new file mode 100644 index 00000000..a8574e82 --- /dev/null +++ b/scripts/api/stl/trex_stl_lib.py @@ -0,0 +1,71 @@ + +from trex_stl_api import * +from scapy.all import * + +# stl library for various utilities + + +# map ports +# will destroy all streams/data on the ports +def stl_map_ports (client, ports = None): + + # by default use all ports + if ports == None: + ports = client.get_all_ports() + + # reset the ports + client.reset(ports) + + # generate streams + base_pkt = STLPktBuilder(pkt = Ether()/IP()) + + pkts = 1 + for port in ports: + stream = STLStream(packet = base_pkt, + mode = STLTXSingleBurst(pps = 100000, total_pkts = pkts)) + + client.add_streams(stream, [port]) + pkts = pkts * 2 + + # inject + client.clear_stats() + client.start(ports, mult = "1mpps") + client.wait_on_traffic(ports) + + stats = client.get_stats() + + # cleanup + client.reset(ports = ports) + + table = {} + for port in ports: + table[port] = None + + for port in ports: + ipackets = stats[port]["ipackets"] + + exp = 1 + while ipackets >= exp: + if ((ipackets & exp) == (exp)): + source = int(math.log(exp, 2)) + table[source] = port + + exp *= 2 + + if not all(x != None for x in table.values()): + raise STLError('unable to map ports') + + dir_a = set() + dir_b = set() + for src, dst in table.iteritems(): + # src is not in + if src not in (dir_a, dir_b): + if dst in dir_a: + dir_b.add(src) + else: + dir_a.add(src) + + table['dir'] = [list(dir_a), list(dir_b)] + + return table + |