perf: GENEVE tunnel test, l3 mode
[csit.git] / GPL / traffic_profiles / trex / profile_trex_stateless_base_class.py
1 # Copyright (c) 2021 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 TrafficStreamsBaseClass:
37     """Base class for stream profiles for T-rex traffic generator."""
38
39     STREAM_TABLE = {
40         u"IMIX_v4": [
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}
44         ],
45         'IMIX_v4_1': [
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}
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 = u""
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(u"!QQ", ip1)
92             hi2, lo2 = struct.unpack(u"!QQ", ip2)
93
94             if ((hi1 << 64) | lo1) > ((hi2 << 64) | lo2):
95                 raise ValueError(u"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         base_pkt_a, base_pkt_b, vm1, vm2 = self.define_packets()
126
127         # In most cases you will not have to change the code below:
128
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)
134
135             # Direction 0 --> 1
136             pkt_a = STLPktBuilder(
137                 pkt=base_pkt_a / self._gen_payload(payload_len_a), vm=vm1
138             )
139             # Direction 1 --> 0
140             pkt_b = STLPktBuilder(
141                 pkt=base_pkt_b / self._gen_payload(payload_len_b), vm=vm2
142             )
143
144             # Packets for latency measurement:
145             # Direction 0 --> 1
146             pkt_lat_a = STLPktBuilder(
147                 pkt=base_pkt_a / self._gen_payload(payload_len_a), vm=vm1
148             )
149             # Direction 1 --> 0
150             pkt_lat_b = STLPktBuilder(
151                 pkt=base_pkt_b / self._gen_payload(payload_len_b), vm=vm2
152             )
153
154             # Create the streams:
155             # Direction 0 --> 1
156             stream1 = STLStream(
157                 packet=pkt_a, mode=STLTXCont(pps=9000)
158             )
159             # Direction 1 --> 0
160             # second traffic stream with a phase of 10ns (inter-stream gap)
161             stream2 = STLStream(
162                 packet=pkt_b, isg=10.0, mode=STLTXCont(pps=9000)
163             )
164
165             # Streams for latency measurement:
166             # Direction 0 --> 1
167             lat_stream1 = STLStream(
168                 packet=pkt_lat_a,
169                 flow_stats=STLFlowLatencyStats(pg_id=0),
170                 mode=STLTXCont(pps=9000)
171             )
172             # Direction 1 --> 0
173             # second traffic stream with a phase of 10ns (inter-stream gap)
174             lat_stream2 = STLStream(
175                 packet=pkt_lat_b,
176                 isg=10.0,
177                 flow_stats=STLFlowLatencyStats(pg_id=1),
178                 mode=STLTXCont(pps=9000)
179             )
180
181             return [stream1, stream2, lat_stream1, lat_stream2]
182
183         # Frame size is defined as a string, e.g.IMIX_v4_1:
184         elif isinstance(self.framesize, str):
185
186             stream1 = list()
187             stream2 = list()
188
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),
195                     vm=vm1
196                 )
197                 pkt_b = STLPktBuilder(
198                     pkt=base_pkt_b / self._gen_payload(payload_len_b),
199                     vm=vm2
200                 )
201
202                 # Create the streams:
203                 stream1.append(STLStream(
204                     packet=pkt_a,
205                     isg=stream[u"isg"],
206                     mode=STLTXCont(pps=stream[u"pps"]))
207                 )
208                 stream2.append(STLStream(
209                     packet=pkt_b,
210                     isg=stream[u"isg"],
211                     mode=STLTXCont(pps=stream[u"pps"]))
212                 )
213             streams = list()
214             streams.extend(stream1)
215             streams.extend(stream2)
216
217             return streams
218
219     def get_streams(self, **kwargs):
220         """Get traffic streams created by "create_streams" method.
221
222         If needed, add your own parameters.
223
224         :param kwargs: Key-value pairs used by "create_streams" method while
225         creating streams.
226         :returns: Traffic streams.
227         :rtype: list
228         """
229         self.framesize = kwargs[u"framesize"]
230         self.rate = kwargs[u"rate"]
231
232         return self.create_streams()