summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts')
-rw-r--r--scripts/api/stl/examples/stl_imix.py106
-rw-r--r--scripts/api/stl/trex_stl_lib.py71
-rw-r--r--scripts/stl/profiles/imix.py4
3 files changed, 179 insertions, 2 deletions
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/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
+
diff --git a/scripts/stl/profiles/imix.py b/scripts/stl/profiles/imix.py
index 834ab6b3..c453222d 100644
--- a/scripts/stl/profiles/imix.py
+++ b/scripts/stl/profiles/imix.py
@@ -38,9 +38,9 @@ class STLImix(object):
mode = STLTXCont())
- def get_streams (self, flip = False):
+ def get_streams (self, direction = 0):
- if not flip:
+ if direction == 0:
src = self.ip_range['src']
dst = self.ip_range['dst']
else: