3 # Copyright (c) 2016 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
18 sys.path.insert(0, "/opt/trex-core-1.88/scripts/api/stl/")
20 from trex_stl_api import *
26 def generate_payload(length):
28 alphabet_size = len(string.letters)
29 for i in range(length):
30 word += string.letters[(i % alphabet_size)]
33 def create_packets(traffic_options, frame_size=64):
36 print "Packet min. size is 64B"
40 pkt_a = STLPktBuilder()
42 pkt_a.add_pkt_layer("l2", dpkt.ethernet.Ethernet())
43 pkt_a.add_pkt_layer("l3_ip", dpkt.ip.IP())
44 pkt_a.add_pkt_layer("l4_udp", dpkt.udp.UDP())
45 pkt_a.set_pkt_payload(generate_payload(frame_size -
46 pkt_a.get_packet_length()))
47 pkt_a.set_layer_attr("l3_ip", "len", len(pkt_a.get_layer('l3_ip')))
52 p1_src_mac = traffic_options['p1_src_mac']
53 p1_dst_mac = traffic_options['p1_dst_mac']
54 p1_src_start_ip = traffic_options['p1_src_start_ip']
55 p1_src_end_ip = traffic_options['p1_src_end_ip']
56 p1_dst_start_ip = traffic_options['p1_dst_start_ip']
57 p1_dst_end_ip = traffic_options['p1_dst_end_ip']
58 p2_src_mac = traffic_options['p2_src_mac']
59 p2_dst_mac = traffic_options['p2_dst_mac']
60 p2_src_start_ip = traffic_options['p2_src_start_ip']
61 p2_src_end_ip = traffic_options['p2_src_end_ip']
62 p2_dst_start_ip = traffic_options['p2_dst_start_ip']
63 p2_dst_end_ip = traffic_options['p2_dst_end_ip']
65 pkt_a.set_eth_layer_addr(layer_name="l2",
68 pkt_a.set_eth_layer_addr(layer_name="l2",
71 pkt_b.set_eth_layer_addr(layer_name="l2",
74 pkt_b.set_eth_layer_addr(layer_name="l2",
78 # set IP range for pkt and split it by multiple cores
79 pkt_a.set_vm_ip_range(ip_layer_name="l3_ip",
81 ip_start=p1_src_start_ip, ip_end=p1_src_end_ip,
85 pkt_a.set_vm_ip_range(ip_layer_name="l3_ip",
87 ip_start=p1_dst_start_ip, ip_end=p1_dst_end_ip,
92 pkt_b.set_vm_ip_range(ip_layer_name="l3_ip",
94 ip_start=p2_src_start_ip, ip_end=p2_src_end_ip,
98 pkt_b.set_vm_ip_range(ip_layer_name="l3_ip",
100 ip_start=p2_dst_start_ip, ip_end=p2_dst_end_ip,
105 def simple_burst(pkt_a, pkt_b, duration=10, rate="1mpps",
106 warmup=True, warmup_time=5):
113 # turn this on for some information
114 #c.set_verbose("high")
117 s1 = STLStream(packet=pkt_a,
118 mode=STLTXCont(pps=100))
120 # second stream with a phase of 1ms (inter stream gap)
121 s2 = STLStream(packet=pkt_b,
123 mode=STLTXCont(pps=100))
129 # prepare our ports (my machine has 0 <--> 1 with static route)
130 c.reset(ports=[0, 1])
132 # add both streams to ports
133 c.add_streams(s1, ports=[0])
134 c.add_streams(s2, ports=[1])
139 c.start(ports=[0, 1], mult=rate, duration=warmup_time)
140 c.wait_on_traffic(ports=[0, 1])
141 stats = c.get_stats()
142 print "#####warmup statistics#####"
143 print json.dumps(stats["port 0"], indent=4,
144 separators=(',', ': '), sort_keys=True)
145 print json.dumps(stats["port 1"], indent=4,
146 separators=(',', ': '), sort_keys=True)
147 lost_a = stats["port 0"]["opackets"] - stats["port 1"]["ipackets"]
148 lost_b = stats["port 1"]["opackets"] - stats["port 0"]["ipackets"]
150 print "\npackets lost from 0 --> 1: {0} pkts".format(lost_a)
151 print "packets lost from 1 --> 0: {0} pkts".format(lost_b)
154 # clear the stats before injecting
157 # choose rate and start traffic
158 c.start(ports=[0, 1], mult=rate, duration=duration)
161 c.wait_on_traffic(ports=[0, 1])
163 # read the stats after the test
164 stats = c.get_stats()
166 print "#####statistics#####"
167 print json.dumps(stats["port 0"], indent=4,
168 separators=(',', ': '), sort_keys=True)
169 print json.dumps(stats["port 1"], indent=4,
170 separators=(',', ': '), sort_keys=True)
172 lost_a = stats["port 0"]["opackets"] - stats["port 1"]["ipackets"]
173 lost_b = stats["port 1"]["opackets"] - stats["port 0"]["ipackets"]
175 total_sent = stats["port 0"]["opackets"] + stats["port 1"]["opackets"]
176 total_rcvd = stats["port 0"]["ipackets"] + stats["port 1"]["ipackets"]
178 print "\npackets lost from 0 --> 1: {0} pkts".format(lost_a)
179 print "packets lost from 1 --> 0: {0} pkts".format(lost_b)
180 print "rate={0}, totalReceived={1}, totalSent={2}, frameLoss={3}"\
181 .format(rate, total_rcvd, total_sent, lost_a+lost_b)
183 if (lost_a == 0) and (lost_b == 0):
188 except STLError as e:
197 print "args: [-h] -d <duration> -s <size of frame in bytes>"+\
198 " [-r] <traffic rate with unit: %, mpps> "+\
199 "--p1_src_mac <port1_src_mac> "+\
200 "--p1_dst_mac <port1_dst_mac> "+\
201 "--p1_src_start_ip <port1_src_start_ip> "+\
202 "--p1_src_end_ip <port1_src_end_ip> "+\
203 "--p1_dst_start_ip <port1_dst_start_ip> "+\
204 "--p1_dst_end_ip <port1_dst_end_ip> "+\
205 "--p2_src_mac <port2_src_mac> "+\
206 "--p2_dst_mac <port2_dst_mac> "+\
207 "--p2_src_start_ip <port2_src_start_ip> "+\
208 "--p2_src_end_ip <port2_src_end_ip> "+\
209 "--p2_dst_start_ip <port2_dst_start_ip> "+\
210 "--p2_dst_end_ip <port2_dst_end_ip>"
218 _traffic_options = {}
221 opts, args = getopt.getopt(argv, "hd:s:r:o:",
235 except getopt.GetoptError:
238 for opt, arg in opts:
239 if opt in ('-h', "--help"):
245 _frame_size = int(arg)
248 elif opt.startswith( "--p" ):
249 _traffic_options[opt[2:]] = arg
251 print _traffic_options
252 if len(_traffic_options) != 12:
253 print "Expected all 12 traffic options"
257 pkt_a, pkt_b = create_packets(_traffic_options,
258 frame_size=_frame_size)
260 simple_burst(pkt_a, pkt_b, duration=_duration, rate=_rate)
262 if __name__ == "__main__":