summaryrefslogtreecommitdiffstats
path: root/scripts/api/stl
diff options
context:
space:
mode:
authorHanoh Haim <hhaim@cisco.com>2016-01-27 18:43:12 +0200
committerHanoh Haim <hhaim@cisco.com>2016-01-27 18:43:12 +0200
commitb06653f4fc66e6cdafaa2697f9553ae12b417e93 (patch)
tree46ea1b3470cf619972e4dcef09d877c9a25ee076 /scripts/api/stl
parent7351ddb52a2162fc2ac303c687a2ed50cbb0843e (diff)
move api under scripts
Diffstat (limited to 'scripts/api/stl')
-rw-r--r--scripts/api/stl/examples/stl_bi_dir_flows.py122
-rw-r--r--scripts/api/stl/examples/stl_simple_burst.py90
-rw-r--r--scripts/api/stl/examples/udp_64B.pcapbin0 -> 104 bytes
-rw-r--r--scripts/api/stl/profiles/burst.yaml39
-rw-r--r--scripts/api/stl/trex_stl_api.py25
5 files changed, 276 insertions, 0 deletions
diff --git a/scripts/api/stl/examples/stl_bi_dir_flows.py b/scripts/api/stl/examples/stl_bi_dir_flows.py
new file mode 100644
index 00000000..429445e6
--- /dev/null
+++ b/scripts/api/stl/examples/stl_bi_dir_flows.py
@@ -0,0 +1,122 @@
+# include the path of trex_stl_api.py
+import sys
+sys.path.insert(0, "../")
+
+from trex_stl_api import *
+
+import dpkt
+import time
+import json
+
+
+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")
+
+
+ # create client
+ c = STLClient()
+ passed = True
+
+ try:
+ # turn this on for some information
+ #c.set_verbose("high")
+
+ # create two streams
+ s1 = STLStream(packet = pkt_a,
+ mode = STLTXCont(pps = 100))
+
+ # second stream with a phase of 1ms (inter stream gap)
+ s2 = STLStream(packet = pkt_b,
+ isg = 1000,
+ mode = STLTXCont(pps = 100))
+
+
+ # connect to server
+ c.connect()
+
+ # prepare our ports (my machine has 0 <--> 1 with static route)
+ c.reset(ports = [0, 1])
+
+ # add both streams to ports
+ c.add_streams(s1, ports = [0])
+ c.add_streams(s2, ports = [1])
+
+ # clear the stats before injecting
+ c.clear_stats()
+
+ # choose rate and start traffic for 10 seconds on 5 mpps
+ print "Running 5 Mpps on ports 0, 1 for 10 seconds..."
+ c.start(ports = [0, 1], mult = "5mpps", duration = 10)
+
+ # block until done
+ c.wait_on_traffic(ports = [0, 1])
+
+ # read the stats after the test
+ stats = c.get_stats()
+
+ print json.dumps(stats["port 0"], indent = 4, separators=(',', ': '), sort_keys = True)
+ print json.dumps(stats["port 1"], indent = 4, separators=(',', ': '), sort_keys = True)
+
+ lost_a = stats["port 0"]["opackets"] - stats["port 1"]["ipackets"]
+ lost_b = stats["port 1"]["opackets"] - stats["port 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:
+ passed = False
+ print e
+
+ finally:
+ c.disconnect()
+
+ if passed:
+ print "\nTest has passed :-)\n"
+ else:
+ print "\nTest has failed :-(\n"
+
+
+# run the tests
+simple_burst()
+
diff --git a/scripts/api/stl/examples/stl_simple_burst.py b/scripts/api/stl/examples/stl_simple_burst.py
new file mode 100644
index 00000000..3b394d10
--- /dev/null
+++ b/scripts/api/stl/examples/stl_simple_burst.py
@@ -0,0 +1,90 @@
+import sys
+sys.path.insert(0, "../")
+
+from trex_stl_api import *
+import dpkt
+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.logger.set_verbose(c.logger.VERBOSE_NORMAL)
+
+ # create two bursts and link them
+ s1 = STLStream(packet = pkt_bld,
+ mode = STLTXSingleBurst(total_pkts = 5000)
+ )
+
+ s2 = STLStream(packet = pkt_bld,
+ mode = STLTXSingleBurst(total_pkts = 3000),
+ next_stream_id = s1.get_id())
+
+
+ # connect to server
+ c.connect()
+
+ # prepare our ports
+ c.reset(ports = [0, 1])
+
+ # add both streams to ports
+ c.add_streams([s1, s2], ports = [0, 1])
+
+ # 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])
+
+ stats = c.get_stats()
+ ipackets = stats['total']['ipackets']
+
+ print "Test iteration {0} - Packets Received: {1} ".format(i, ipackets)
+ # (5000 + 3000) * 2 ports = 16,000
+ if (ipackets != (16000)):
+ 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
+simple_burst()
+
diff --git a/scripts/api/stl/examples/udp_64B.pcap b/scripts/api/stl/examples/udp_64B.pcap
new file mode 100644
index 00000000..699b9c80
--- /dev/null
+++ b/scripts/api/stl/examples/udp_64B.pcap
Binary files differ
diff --git a/scripts/api/stl/profiles/burst.yaml b/scripts/api/stl/profiles/burst.yaml
new file mode 100644
index 00000000..dbd348c7
--- /dev/null
+++ b/scripts/api/stl/profiles/burst.yaml
@@ -0,0 +1,39 @@
+### Single stream UDP packet, 64B ###
+#####################################
+- name: stream0
+ stream:
+ self_start: True
+ next_stream_id: stream1
+ packet:
+ binary: udp_64B.pcap
+ mode:
+ type: single_burst
+ pps: 100
+ total_pkts : 100
+ rx_stats: []
+ vm: []
+
+- name: stream1
+ stream:
+ self_start: False
+ next_stream_id: stream2
+ packet:
+ binary: udp_64B.pcap
+ mode:
+ type: single_burst
+ pps: 100
+ total_pkts : 200
+ rx_stats: []
+ vm: []
+
+- name: stream2
+ stream:
+ self_start: False
+ packet:
+ binary: udp_64B.pcap
+ mode:
+ type: single_burst
+ pps: 100
+ total_pkts : 300
+ rx_stats: []
+ vm: []
diff --git a/scripts/api/stl/trex_stl_api.py b/scripts/api/stl/trex_stl_api.py
new file mode 100644
index 00000000..09a0af18
--- /dev/null
+++ b/scripts/api/stl/trex_stl_api.py
@@ -0,0 +1,25 @@
+import os
+import sys
+import time
+
+
+# update the import path to include the stateless client
+root_path = os.path.dirname(os.path.abspath(__file__))
+
+sys.path.insert(0, os.path.join(root_path, '../../automation/trex_control_plane/'))
+
+# aliasing
+import common.trex_streams
+from client_utils.packet_builder import CTRexPktBuilder
+import common.trex_stl_exceptions
+import client.trex_stateless_client
+
+STLClient = client.trex_stateless_client.STLClient
+STLError = common.trex_stl_exceptions.STLError
+STLStream = common.trex_streams.STLStream
+
+STLTXCont = common.trex_streams.STLTXCont
+STLTXSingleBurst = common.trex_streams.STLTXSingleBurst
+STLTXMultiBurst = common.trex_streams.STLTXMultiBurst
+
+STLPktBuilder = CTRexPktBuilder