1 # Copyright (c) 2021 Cisco and/or its affiliates.
3 # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
5 # Licensed under the Apache License 2.0 or
6 # GNU General Public License v2.0 or later; you may not use this file
7 # except in compliance with one of these Licenses. You
8 # may obtain a copy of the Licenses at:
10 # http://www.apache.org/licenses/LICENSE-2.0
11 # https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
13 # Note: If this file is linked with Scapy, which is GPLv2+, your use of it
14 # must be under GPLv2+. If at any point in the future it is no longer linked
15 # with Scapy (or other GPLv2+ licensed software), you are free to choose
18 # Unless required by applicable law or agreed to in writing, software
19 # distributed under the License is distributed on an "AS IS" BASIS,
20 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 # See the License for the specific language governing permissions and
22 # limitations under the License.
24 """Base class for stream profiles for T-rex traffic generator.
30 from random import choice
31 from string import ascii_letters
33 from trex.stl.api import *
36 class TrafficStreamsBaseClass:
37 """Base class for stream profiles for T-rex traffic generator."""
41 {u"size": 60, u"pps": 28, u"isg": 0},
42 {u"size": 590, u"pps": 20, u"isg": 0.1},
43 {u"size": 1514, u"pps": 4, u"isg": 0.2}
46 {u"size": 64, u"pps": 28, u"isg": 0},
47 {u"size": 570, u"pps": 16, u"isg": 0.1},
48 {u"size": 1518, u"pps": 4, u"isg": 0.2}
53 # Default value of frame size, it will be overwritten by the value of
54 # "framesize" parameter of "get_streams" method.
57 # If needed, add your own parameters.
59 def _gen_payload(self, length):
62 If needed, implement your own algorithm.
64 :param length: Length of generated payload.
66 :returns: The generated payload.
70 for _ in range(length):
71 payload += choice(ascii_letters)
75 def _get_start_end_ipv6(self, start_ip, end_ip):
76 """Get start host and number of hosts from IPv6 as integer.
78 :param start_ip: Start IPv6.
79 :param end_ip: End IPv6.
80 :type start_ip: string
82 :return: Start host, number of hosts.
84 :raises: ValueError if start_ip is greater then end_ip.
85 :raises: socket.error if the IP addresses are not valid IPv6 addresses.
88 ip1 = socket.inet_pton(socket.AF_INET6, start_ip)
89 ip2 = socket.inet_pton(socket.AF_INET6, end_ip)
91 hi1, lo1 = struct.unpack(u"!QQ", ip1)
92 hi2, lo2 = struct.unpack(u"!QQ", ip2)
94 if ((hi1 << 64) | lo1) > ((hi2 << 64) | lo2):
95 raise ValueError(u"IPv6: start_ip is greater then end_ip")
97 return lo1, abs(int(lo1) - int(lo2))
99 except socket.error as err:
103 def define_packets(self):
104 """Define the packets to be sent from the traffic generator.
106 This method MUST return:
108 return base_pkt_a, base_pkt_b, vm1, vm2
110 vm1 and vm2 CAN be None.
112 :returns: Packets to be sent from the traffic generator.
115 raise NotImplementedError
117 def create_streams(self):
118 """Create traffic streams.
120 Implement your own traffic streams.
122 :returns: Traffic streams.
125 base_pkt_a, base_pkt_b, vm1, vm2 = self.define_packets()
127 # In most cases you will not have to change the code below:
129 # Frame size is defined as an integer, e.g. 64, 1518:
130 if isinstance(self.framesize, int):
131 # Create a base packet and pad it to size; deduct FCS
132 payload_len_a = max(0, self.framesize - len(base_pkt_a) - 4)
133 payload_len_b = max(0, self.framesize - len(base_pkt_b) - 4)
136 pkt_a = STLPktBuilder(
137 pkt=base_pkt_a / self._gen_payload(payload_len_a), vm=vm1
140 pkt_b = STLPktBuilder(
141 pkt=base_pkt_b / self._gen_payload(payload_len_b), vm=vm2
144 # Packets for latency measurement:
146 pkt_lat_a = STLPktBuilder(
147 pkt=base_pkt_a / self._gen_payload(payload_len_a), vm=vm1
150 pkt_lat_b = STLPktBuilder(
151 pkt=base_pkt_b / self._gen_payload(payload_len_b), vm=vm2
154 # Create the streams:
157 packet=pkt_a, mode=STLTXCont(pps=9000)
160 # second traffic stream with a phase of 10ns (inter-stream gap)
162 packet=pkt_b, isg=10.0, mode=STLTXCont(pps=9000)
165 # Streams for latency measurement:
167 lat_stream1 = STLStream(
169 flow_stats=STLFlowLatencyStats(pg_id=0),
170 mode=STLTXCont(pps=9000)
173 # second traffic stream with a phase of 10ns (inter-stream gap)
174 lat_stream2 = STLStream(
177 flow_stats=STLFlowLatencyStats(pg_id=1),
178 mode=STLTXCont(pps=9000)
181 return [stream1, stream2, lat_stream1, lat_stream2]
183 # Frame size is defined as a string, e.g.IMIX_v4_1:
184 elif isinstance(self.framesize, str):
189 for stream in self.STREAM_TABLE[self.framesize]:
190 payload_len_a = max(0, stream[u"size"] - len(base_pkt_a) - 4)
191 payload_len_b = max(0, stream[u"size"] - len(base_pkt_b) - 4)
192 # Create a base packet and pad it to size
193 pkt_a = STLPktBuilder(
194 pkt=base_pkt_a / self._gen_payload(payload_len_a),
197 pkt_b = STLPktBuilder(
198 pkt=base_pkt_b / self._gen_payload(payload_len_b),
202 # Create the streams:
203 stream1.append(STLStream(
206 mode=STLTXCont(pps=stream[u"pps"]))
208 stream2.append(STLStream(
211 mode=STLTXCont(pps=stream[u"pps"]))
214 streams.extend(stream1)
215 streams.extend(stream2)
219 def get_streams(self, **kwargs):
220 """Get traffic streams created by "create_streams" method.
222 If needed, add your own parameters.
224 :param kwargs: Key-value pairs used by "create_streams" method while
226 :returns: Traffic streams.
229 self.framesize = kwargs[u"framesize"]
230 self.rate = kwargs[u"rate"]
232 return self.create_streams()