Revert "fix(jobspec): Delete ipsec nfv density tests"
[csit.git] / GPL / traffic_profiles / trex / profile_trex_stateless_scale_class.py
1 # Copyright (c) 2023 Cisco and/or its affiliates.
2 #
3 # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
4 #
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:
9 #
10 #     http://www.apache.org/licenses/LICENSE-2.0
11 #     https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 #
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
16 # Apache 2.
17 #
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.
23
24 """Base class for stream profiles for T-rex traffic generator.
25 """
26
27 import socket
28 import struct
29
30 from random import choice
31 from string import ascii_letters
32
33 from trex.stl.api import *
34
35
36 class TrafficStreamsScaleClass:
37     """Base class for stream profiles for T-rex traffic generator."""
38
39     STREAM_TABLE = {
40         "IMIX_v4": [
41             {"size": 60, "pps": 28, "isg": 0},
42             {"size": 590, "pps": 20, "isg": 0.1},
43             {"size": 1514, "pps": 4, "isg": 0.2}
44         ],
45         "IMIX_v4_1": [
46             {"size": 64, "pps": 28, "isg": 0},
47             {"size": 570, "pps": 16, "isg": 0.1},
48             {"size": 1518, "pps": 4, "isg": 0.2}
49         ]
50     }
51
52     def __init__(self):
53         # Default value of frame size, it will be overwritten by the value of
54         # "framesize" parameter of "get_streams" method.
55         self.framesize = 64
56
57         # If needed, add your own parameters.
58
59     def _gen_payload(self, length):
60         """Generate payload.
61
62         If needed, implement your own algorithm.
63
64         :param length: Length of generated payload.
65         :type length: int
66         :returns: The generated payload.
67         :rtype: str
68         """
69         payload = ""
70         for _ in range(length):
71             payload += choice(ascii_letters)
72
73         return payload
74
75     def _get_start_end_ipv6(self, start_ip, end_ip):
76         """Get start host and number of hosts from IPv6 as integer.
77
78         :param start_ip: Start IPv6.
79         :param end_ip: End IPv6.
80         :type start_ip: string
81         :type end_ip: string
82         :return: Start host, number of hosts.
83         :rtype tuple of int
84         :raises: ValueError if start_ip is greater then end_ip.
85         :raises: socket.error if the IP addresses are not valid IPv6 addresses.
86         """
87         try:
88             ip1 = socket.inet_pton(socket.AF_INET6, start_ip)
89             ip2 = socket.inet_pton(socket.AF_INET6, end_ip)
90
91             hi1, lo1 = struct.unpack("!QQ", ip1)
92             hi2, lo2 = struct.unpack("!QQ", ip2)
93
94             if ((hi1 << 64) | lo1) > ((hi2 << 64) | lo2):
95                 raise ValueError("IPv6: start_ip is greater then end_ip")
96
97             return lo1, abs(int(lo1) - int(lo2))
98
99         except socket.error as err:
100             print(err)
101             raise
102
103     def define_packets(self):
104         """Define the packets to be sent from the traffic generator.
105
106         This method MUST return:
107
108             return base_pkt_a, base_pkt_b, vm1, vm2
109
110             vm1 and vm2 CAN be None.
111
112         :returns: Packets to be sent from the traffic generator.
113         :rtype: tuple
114         """
115         raise NotImplementedError
116
117     def create_streams(self):
118         """Create traffic streams.
119
120         Implement your own traffic streams.
121
122         :returns: Traffic streams.
123         :rtype: list
124         """
125         pkts, vms = self.define_packets()
126
127         # Frame size is defined as an integer, e.g. 64, 1518:
128         if isinstance(self.framesize, int):
129             pkt_streams = list()
130             lat_streams = list()
131             for i in range(len(pkts)):
132                 payload_len = max(0, self.framesize - len(pkts[i]) - 4)
133
134                 pkt = STLPktBuilder(
135                     pkt=pkts[i] / self._gen_payload(payload_len),
136                     vm=vms[i]
137                 )
138                 pkt_lat = STLPktBuilder(
139                     pkt=pkts[i] / self._gen_payload(payload_len),
140                     vm=vms[i]
141                 )
142                 pkt_streams.append(
143                     STLStream(
144                         packet=pkt,
145                         isg=10.0 * (i // (len(pkts) // 2)),
146                         mode=STLTXCont(pps=9000)
147                     )
148                 )
149                 lat_streams.append(
150                     STLStream(
151                         packet=pkt_lat,
152                         isg=10.0 * (i // (len(pkts) // 2)),
153                         flow_stats=STLFlowLatencyStats(pg_id=i),
154                         mode=STLTXCont(pps=9000)
155                     )
156                 )
157
158             streams = list()
159             streams.extend(pkt_streams)
160             streams.extend(lat_streams)
161             return streams
162
163         # Frame size is defined as a string, e.g.IMIX_v4_1:
164         elif isinstance(self.framesize, str):
165             pkt_streams = list()
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)
169
170                     pkt = STLPktBuilder(
171                         pkt=pkts[i] / self._gen_payload(payload_len),
172                         vm=vms[i]
173                     )
174                     pkt_streams.append(
175                         STLStream(
176                             packet=pkt,
177                             isg=stream["isg"],
178                             mode=STLTXCont(pps=stream["pps"])
179                         )
180                     )
181             return pkt_streams
182
183     def get_streams(self, **kwargs):
184         """Get traffic streams created by "create_streams" method.
185
186         If needed, add your own parameters.
187
188         :param kwargs: Key-value pairs used by "create_streams" method while
189         creating streams.
190         :returns: Traffic streams.
191         :rtype: list
192         """
193         self.framesize = kwargs["framesize"]
194         self.rate = kwargs["rate"]
195
196         return self.create_streams()