1 # Copyright (c) 2023 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 TrafficStreamsScaleClass:
37 """Base class for stream profiles for T-rex traffic generator."""
41 {"size": 60, "pps": 28, "isg": 0},
42 {"size": 590, "pps": 20, "isg": 0.1},
43 {"size": 1514, "pps": 4, "isg": 0.2}
46 {"size": 64, "pps": 28, "isg": 0},
47 {"size": 570, "pps": 16, "isg": 0.1},
48 {"size": 1518, "pps": 4, "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("!QQ", ip1)
92 hi2, lo2 = struct.unpack("!QQ", ip2)
94 if ((hi1 << 64) | lo1) > ((hi2 << 64) | lo2):
95 raise ValueError("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 pkts, vms = self.define_packets()
127 # Frame size is defined as an integer, e.g. 64, 1518:
128 if isinstance(self.framesize, int):
131 for i in range(len(pkts)):
132 payload_len = max(0, self.framesize - len(pkts[i]) - 4)
135 pkt=pkts[i] / self._gen_payload(payload_len),
138 pkt_lat = STLPktBuilder(
139 pkt=pkts[i] / self._gen_payload(payload_len),
145 isg=10.0 * (i // (len(pkts) // 2)),
146 mode=STLTXCont(pps=9000)
152 isg=10.0 * (i // (len(pkts) // 2)),
153 flow_stats=STLFlowLatencyStats(pg_id=i),
154 mode=STLTXCont(pps=9000)
159 streams.extend(pkt_streams)
160 streams.extend(lat_streams)
163 # Frame size is defined as a string, e.g.IMIX_v4_1:
164 elif isinstance(self.framesize, str):
166 for i in range(len(pkts)):
167 for stream in self.STREAM_TABLE[self.framesize]:
168 payload_len = max(0, stream["size"] - len(pkts[i]) - 4)
171 pkt=pkts[i] / self._gen_payload(payload_len),
178 mode=STLTXCont(pps=stream["pps"])
183 def get_streams(self, **kwargs):
184 """Get traffic streams created by "create_streams" method.
186 If needed, add your own parameters.
188 :param kwargs: Key-value pairs used by "create_streams" method while
190 :returns: Traffic streams.
193 self.framesize = kwargs["framesize"]
194 self.rate = kwargs["rate"]
196 return self.create_streams()