1 # Copyright (c) 2019 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """Base class for stream profiles for T-rex traffic generator.
21 from random import choice
22 from string import ascii_letters
24 from trex.stl.api import *
27 class TrafficStreamsBaseClass(object):
28 """Base class for stream profiles for T-rex traffic generator."""
31 {'size': 60, 'pps': 28, 'isg': 0},
32 {'size': 590, 'pps': 20, 'isg': 0.1},
33 {'size': 1514, 'pps': 4, 'isg': 0.2}
36 {'size': 64, 'pps': 28, 'isg': 0},
37 {'size': 570, 'pps': 16, 'isg': 0.1},
38 {'size': 1518, 'pps': 4, 'isg': 0.2}
43 # Default value of frame size, it will be overwritten by the value of
44 # "framesize" parameter of "get_streams" method.
47 # If needed, add your own parameters.
49 def _gen_payload(self, length):
52 If needed, implement your own algorithm.
54 :param length: Length of generated payload.
56 :returns: The generated payload.
60 for _ in range(length):
61 payload += choice(ascii_letters)
65 def _get_start_end_ipv6(self, start_ip, end_ip):
66 """Get start host and number of hosts from IPv6 as integer.
68 :param start_ip: Start IPv6.
69 :param end_ip: End IPv6.
70 :type start_ip: string
72 :return: Start host, number of hosts.
74 :raises: ValueError if start_ip is greater then end_ip.
75 :raises: socket.error if the IP addresses are not valid IPv6 addresses.
78 ip1 = socket.inet_pton(socket.AF_INET6, start_ip)
79 ip2 = socket.inet_pton(socket.AF_INET6, end_ip)
81 hi1, lo1 = struct.unpack('!QQ', ip1)
82 hi2, lo2 = struct.unpack('!QQ', ip2)
84 if ((hi1 << 64) | lo1) > ((hi2 << 64) | lo2):
85 raise ValueError("IPv6: start_ip is greater then end_ip")
87 return lo1, abs(int(lo1) - int(lo2))
89 except socket.error as err:
93 def define_packets(self):
94 """Define the packets to be sent from the traffic generator.
96 This method MUST return:
98 return base_pkt_a, base_pkt_b, vm1, vm2
100 vm1 and vm2 CAN be None.
102 :returns: Packets to be sent from the traffic generator.
105 raise NotImplementedError
107 def create_streams(self):
108 """Create traffic streams.
110 Implement your own traffic streams.
112 :returns: Traffic streams.
115 base_pkt_a, base_pkt_b, vm1, vm2 = self.define_packets()
117 # In most cases you will not have to change the code below:
119 # Frame size is defined as an integer, e.g. 64, 1518:
120 if isinstance(self.framesize, int):
122 # Create a base packet and pad it to size
123 payload_len = max(0, self.framesize - len(base_pkt_a) - 4) # No FCS
126 pkt_a = STLPktBuilder(
127 pkt=base_pkt_a / self._gen_payload(payload_len), vm=vm1)
129 pkt_b = STLPktBuilder(
130 pkt=base_pkt_b / self._gen_payload(payload_len), vm=vm2)
132 # Packets for latency measurement:
134 pkt_lat_a = STLPktBuilder(
135 pkt=base_pkt_a / self._gen_payload(payload_len), vm=vm1)
137 pkt_lat_b = STLPktBuilder(
138 pkt=base_pkt_b / self._gen_payload(payload_len), vm=vm2)
140 # Create the streams:
142 stream1 = STLStream(packet=pkt_a, mode=STLTXCont(pps=9000))
144 # second traffic stream with a phase of 10ns (inter-stream gap)
145 stream2 = STLStream(packet=pkt_b, isg=10.0,
146 mode=STLTXCont(pps=9000))
148 # Streams for latency measurement:
150 lat_stream1 = STLStream(packet=pkt_lat_a,
151 flow_stats=STLFlowLatencyStats(pg_id=0),
152 mode=STLTXCont(pps=9000))
154 # second traffic stream with a phase of 10ns (inter-stream gap)
155 lat_stream2 = STLStream(packet=pkt_lat_b, isg=10.0,
156 flow_stats=STLFlowLatencyStats(pg_id=1),
157 mode=STLTXCont(pps=9000))
159 return [stream1, stream2, lat_stream1, lat_stream2]
161 # Frame size is defined as a string, e.g.IMIX_v4_1:
162 elif isinstance(self.framesize, str):
167 for stream in self.STREAM_TABLE[self.framesize]:
168 payload_len_a = max(0, stream['size'] - len(base_pkt_a) - 4)
169 payload_len_b = max(0, stream['size'] - len(base_pkt_b) - 4)
170 # Create a base packet and pad it to size
171 pkt_a = STLPktBuilder(
172 pkt=base_pkt_a / self._gen_payload(payload_len_a),
174 pkt_b = STLPktBuilder(
175 pkt=base_pkt_b / self._gen_payload(payload_len_b),
178 # Create the streams:
179 stream1.append(STLStream(packet=pkt_a,
181 mode=STLTXCont(pps=stream['pps'])))
182 stream2.append(STLStream(packet=pkt_b,
184 mode=STLTXCont(pps=stream['pps'])))
186 streams.extend(stream1)
187 streams.extend(stream2)
191 def get_streams(self, **kwargs):
192 """Get traffic streams created by "create_streams" method.
194 If needed, add your own parameters.
196 :param kwargs: Key-value pairs used by "create_streams" method while
198 :returns: Traffic streams.
201 self.framesize = kwargs['framesize']
203 return self.create_streams()