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.
16 """This script uses T-REX stateless API to drive t-rex instance.
19 - T-REX: https://github.com/cisco-system-traffic-generator/trex-core
20 - compiled and running T-REX process (eg. ./t-rex-64 -i -c 4)
21 - trex_stl_lib.api library
22 - Script must be executed on a node with T-REX instance
23 - 2 interfaces must be configured in configuretion file /etc/trex_cfg.yaml
25 ##################### Example of /etc/trex_cfg.yaml ##########################
26 - port_limit : 2 # numbers of ports to use
28 interfaces : ["84:00.0","84:00.1"] # PCI address of interfaces
29 port_info : # set eth mac addr
30 - dest_mac : [0x90,0xe2,0xba,0x1f,0x97,0xd5] # port 0
31 src_mac : [0x90,0xe2,0xba,0x1f,0x97,0xd4]
32 - dest_mac : [0x90,0xe2,0xba,0x1f,0x97,0xd4] # port 1
33 src_mac : [0x90,0xe2,0xba,0x1f,0x97,0xd5]
34 ##############################################################################
37 1. Configure traffic on running T-REX instance
38 2. Clear statistics on all ports
39 3. Ctart traffic with specified duration
40 4. Print statistics to stdout
50 sys.path.insert(0, "/opt/trex-core-2.00/scripts/automation/"+\
51 "trex_control_plane/stl/")
52 from trex_stl_lib.api import *
55 def generate_payload(length):
58 :param length: Length of payload.
60 :return: Payload filled with chars.
65 alphabet_size = len(string.letters)
66 for i in range(length):
67 word += string.letters[(i % alphabet_size)]
72 def get_start_end_ipv6(start_ip, end_ip):
73 """Get start and end host from IPv6 as integer.
75 :param start_ip: Start IPv6.
76 :param end_ip: End IPv6.
77 :type start_ip: string
79 :return: Start host, end host.
84 ip1 = socket.inet_pton(socket.AF_INET6, start_ip)
85 ip2 = socket.inet_pton(socket.AF_INET6, end_ip)
87 hi1, lo1 = struct.unpack('!QQ', ip1)
88 hi2, lo2 = struct.unpack('!QQ', ip2)
90 if ((hi1 << 64) | lo1) > ((hi2 << 64) | lo2):
91 print "IPv6: start_ip is greater then end_ip"
94 max_p1 = abs(int(lo1) - int(lo2)) + 1
96 except AddressValueError as ex_error:
100 return base_p1, max_p1
103 def create_packets(traffic_options, frame_size=64):
104 """Create two IP packets to be used in stream.
106 :param traffic_options: Parameters for packets.
107 :param frame_size: Size of L2 frame.
108 :type traffic_options: list
109 :type frame_size: int
110 :return: Packet instances.
115 print_error("Packet min. size is 64B")
118 fsize_no_fcs = frame_size - 4 # no FCS
120 p1_src_start_ip = traffic_options['p1_src_start_ip']
121 p1_src_end_ip = traffic_options['p1_src_end_ip']
122 p1_dst_start_ip = traffic_options['p1_dst_start_ip']
123 p2_src_start_ip = traffic_options['p2_src_start_ip']
124 p2_src_end_ip = traffic_options['p2_src_end_ip']
125 p2_dst_start_ip = traffic_options['p2_dst_start_ip']
127 base_pkt_a = Ether()/IP(src=p1_src_start_ip, dst=p1_dst_start_ip, proto=61)
128 base_pkt_b = Ether()/IP(src=p2_src_start_ip, dst=p2_dst_start_ip, proto=61)
130 # The following code applies raw instructions to packet (IP src increment).
131 # It splits the generated traffic by "ip_src" variable to cores and fix
132 # IPv4 header checksum.
133 vm1 = STLScVmRaw([STLVmFlowVar(name="src",
134 min_value=p1_src_start_ip,
135 max_value=p1_src_end_ip,
137 STLVmWrFlowVar(fv_name="src", pkt_offset="IP.src"),
138 STLVmFixIpv4(offset="IP"),
139 ], split_by_field="src")
140 # The following code applies raw instructions to packet (IP src increment).
141 # It splits the generated traffic by "ip_src" variable to cores and fix
142 # IPv4 header checksum.
143 vm2 = STLScVmRaw([STLVmFlowVar(name="src",
144 min_value=p2_src_start_ip,
145 max_value=p2_src_end_ip,
147 STLVmWrFlowVar(fv_name="src", pkt_offset="IP.src"),
148 STLVmFixIpv4(offset="IP"),
149 ], split_by_field="src")
151 pkt_a = STLPktBuilder(pkt=base_pkt_a/generate_payload(
152 fsize_no_fcs-len(base_pkt_a)), vm=vm1)
153 pkt_b = STLPktBuilder(pkt=base_pkt_b/generate_payload(
154 fsize_no_fcs-len(base_pkt_b)), vm=vm2)
159 def create_packets_v6(traffic_options, frame_size=78):
160 """Create two IPv6 packets to be used in stream.
162 :param traffic_options: Parameters for packets.
163 :param frame_size: Size of L2 frame.
164 :type traffic_options: List
165 :type frame_size: int
166 :return: Packet instances.
171 print "Packet min. size is 78B"
174 fsize_no_fcs = frame_size - 4 # no FCS
176 p1_src_start_ip = traffic_options['p1_src_start_ip']
177 p1_src_end_ip = traffic_options['p1_src_end_ip']
178 p1_dst_start_ip = traffic_options['p1_dst_start_ip']
179 p2_src_start_ip = traffic_options['p2_src_start_ip']
180 p2_src_end_ip = traffic_options['p2_src_end_ip']
181 p2_dst_start_ip = traffic_options['p2_dst_start_ip']
183 base_p1, max_p1 = get_start_end_ipv6(p1_src_start_ip, p1_src_end_ip)
184 base_p2, max_p2 = get_start_end_ipv6(p2_src_start_ip, p2_src_end_ip)
186 base_pkt_a = Ether()/IPv6(src=p1_src_start_ip, dst=p1_dst_start_ip)
187 base_pkt_b = Ether()/IPv6(src=p2_src_start_ip, dst=p2_dst_start_ip)
189 # The following code applies raw instructions to packet (IP src increment).
190 # It splits the generated traffic by "ip_src" variable to cores
191 vm1 = STLScVmRaw([STLVmFlowVar(name="ipv6_src",
193 max_value=max_p1+base_p1,
195 STLVmWrFlowVar(fv_name="ipv6_src", pkt_offset="IPv6.src",
198 , split_by_field="ipv6_src")
200 # The following code applies raw instructions to packet (IP src increment).
201 # It splits the generated traffic by "ip_src" variable to cores
202 vm2 = STLScVmRaw([STLVmFlowVar(name="ipv6_src",
204 max_value=max_p2+base_p2,
206 STLVmWrFlowVar(fv_name="ipv6_src", pkt_offset="IPv6.src",
209 , split_by_field="ipv6_src")
211 pkt_a = STLPktBuilder(pkt=base_pkt_a/generate_payload(
212 max(0, fsize_no_fcs-len(base_pkt_a))), vm=vm1)
213 pkt_b = STLPktBuilder(pkt=base_pkt_b/generate_payload(
214 max(0, fsize_no_fcs-len(base_pkt_b))), vm=vm2)
219 def simple_burst(pkt_a, pkt_b, duration=10, rate="1mpps", warmup_time=5):
220 """Run the traffic with specific parameters.
222 :param pkt_a: Base packet for stream 1.
223 :param pkt_b: Base packet for stream 2.
224 :param duration: Duration of traffic run in seconds.
225 :param rate: Rate of traffic run [percentage, pps, bps].
226 :param warmup_time: Warm up duration.
227 :type pkt_a: STLPktBuilder
228 :type pkt_b: STLPktBuilder
231 :type warmup_time: int
244 # turn this off if too many logs
245 #client.set_verbose("high")
248 stream1 = STLStream(packet=pkt_a,
249 mode=STLTXCont(pps=100))
251 # second stream with a phase of 10ns (inter stream gap)
252 stream2 = STLStream(packet=pkt_b,
254 mode=STLTXCont(pps=100))
259 # prepare our ports (my machine has 0 <--> 1 with static route)
260 client.reset(ports=[0, 1])
262 # add both streams to ports
263 client.add_streams(stream1, ports=[0])
264 client.add_streams(stream2, ports=[1])
267 if warmup_time is not None:
269 client.start(ports=[0, 1], mult=rate, duration=warmup_time)
270 client.wait_on_traffic(ports=[0, 1], timeout=(warmup_time+30))
271 stats = client.get_stats()
273 print "#####warmup statistics#####"
274 print json.dumps(stats, indent=4,
275 separators=(',', ': '), sort_keys=True)
276 lost_a = stats[0]["opackets"] - stats[1]["ipackets"]
277 lost_b = stats[1]["opackets"] - stats[0]["ipackets"]
279 print "\npackets lost from 0 --> 1: {0} pkts".format(lost_a)
280 print "packets lost from 1 --> 0: {0} pkts".format(lost_b)
283 # clear the stats before injecting
290 # choose rate and start traffic
291 client.start(ports=[0, 1], mult=rate, duration=duration)
294 client.wait_on_traffic(ports=[0, 1], timeout=(duration+30))
296 # read the stats after the test
297 stats = client.get_stats()
299 print "#####statistics#####"
300 print json.dumps(stats, indent=4,
301 separators=(',', ': '), sort_keys=True)
303 lost_a = stats[0]["opackets"] - stats[1]["ipackets"]
304 lost_b = stats[1]["opackets"] - stats[0]["ipackets"]
306 total_sent = stats[0]["opackets"] + stats[1]["opackets"]
307 total_rcvd = stats[0]["ipackets"] + stats[1]["ipackets"]
309 print "\npackets lost from 0 --> 1: {0} pkts".format(lost_a)
310 print "packets lost from 1 --> 0: {0} pkts".format(lost_b)
312 except STLError as ex_error:
313 print_error(str(ex_error))
318 print "rate={0}, totalReceived={1}, totalSent={2}, frameLoss={3}"\
319 .format(rate, total_rcvd, total_sent, lost_a+lost_b)
323 """Print help on stdout."""
325 print "args: [-h] -d <duration> -s <size of frame in bytes>"+\
326 " [-r] <traffic rate with unit: %, mpps> "+\
327 " [-6] Use of ipv6 "+\
328 "--p1_src_mac <port1_src_mac> "+\
329 "--p1_dst_mac <port1_dst_mac> "+\
330 "--p1_src_start_ip <port1_src_start_ip> "+\
331 "--p1_src_end_ip <port1_src_end_ip> "+\
332 "--p1_dst_start_ip <port1_dst_start_ip> "+\
333 "--p1_dst_end_ip <port1_dst_end_ip> "+\
334 "--p2_src_mac <port2_src_mac> "+\
335 "--p2_dst_mac <port2_dst_mac> "+\
336 "--p2_src_start_ip <port2_src_start_ip> "+\
337 "--p2_src_end_ip <port2_src_end_ip> "+\
338 "--p2_dst_start_ip <port2_dst_start_ip> "+\
339 "--p2_dst_end_ip <port2_dst_end_ip>"
342 def print_error(msg):
343 """Print error message on stderr.
345 :param msg: Error message to print.
350 sys.stderr.write(msg+'\n')
359 _traffic_options = {}
363 opts, _ = getopt.getopt(argv, "hd:s:r:6o:",
377 except getopt.GetoptError:
380 for opt, arg in opts:
381 if opt in ('-h', "--help"):
387 _frame_size = int(arg)
392 elif opt.startswith("--p"):
393 _traffic_options[opt[2:]] = arg
395 print _traffic_options
396 if len(_traffic_options) != 6:
397 print_error("Supported only: src_start_ip, src_end_ip, dst_start_ip")
402 pkt_a, pkt_b = create_packets_v6(_traffic_options,
403 frame_size=_frame_size)
405 pkt_a, pkt_b = create_packets(_traffic_options,
406 frame_size=_frame_size)
408 simple_burst(pkt_a, pkt_b, duration=_duration, rate=_rate)
410 if __name__ == "__main__":