Perf: Bump T-Rex to 2.88
[csit.git] / GPL / traffic_profiles / trex / trex-stl-2n-ethip4-geneve-1024t.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 """Stream profile for T-rex traffic generator.
25
26 Stream profile:
27  - Two streams sent in directions 0 --> 1 and 1 --> 0 at the same time.
28  - Direction 0 --> 1:
29    - Packet: ETH / IP
30    - Source IP address range:            10.128.1.0 - 10.132.0.255
31    - Destination IP address range:       10.0.1.0 - 10.4.0.255
32  - Direction 1 --> 0:
33    - Packet: ETH / IP / UDP / GENEVE / ETH / IP
34    - Outer Source IP address range:      1.1.1.1
35    - Outer Destination IP address range: 1.1.1.2
36    - Inner Source IP address range:      10.0.1.0 - 10.4.0.255
37    - Inner Destination IP address range: 10.128.1.0 - 10.132.0.255
38    - Source UDP port range:              1024 - 65535
39    - Destination UDP port range:         6081
40 """
41
42 from ctypes import c_int
43 from ipaddress import IPv4Address
44
45 from trex.stl.api import *
46 from scapy.contrib.geneve import GENEVE
47
48 from profile_trex_stateless_base_class import TrafficStreamsBaseClass
49
50
51 class TrafficStreams(TrafficStreamsBaseClass):
52     """Stream profile."""
53
54     def __init__(self):
55         """Initialization and setting of streams' parameters."""
56
57         super(TrafficStreamsBaseClass, self).__init__()
58
59         # Nr. of GENEVE tunnels
60         self.n_tunnels = 1024
61         # VNIs used in GENEVE headers of Direction 1 --> 0.
62         self.p2_geneve_start_vni = 1
63
64         # IPs used in packet headers of Direction 0 --> 1.
65         self.p1_src_start_ip = u"10.128.1.0"
66         self.p1_dst_start_ip = u"10.0.1.0"
67
68         # IPs used in packet headers of Direction 1 --> 0.
69         self.p2_outer_src_ip = u"1.1.1.1"
70         self.p2_outer_dst_ip = u"1.1.1.2"
71
72         self.p2_inner_src_start_ip = u"10.0.1.0"
73         self.p2_inner_dst_start_ip = u"10.128.1.0"
74
75         # MACs used in inner ethernet header of Direction 1 --> 0.
76         self.p2_inner_dst_mac = u"d0:0b:ee:d0:00:00"
77
78         # UDP ports used in packet headers of Direction 1 --> 0.
79         self.p2_udp_sport_start = 1024
80         self.p2_udp_dport = 6081
81
82     def define_packets(self):
83         """Defines the packets to be sent from the traffic generator.
84
85         Packet definition: | ETH | IP | UDP |
86
87         :returns: Packets to be sent from the traffic generator.
88         :rtype: tuple
89         """
90         p1_src_start_ip_int = int(IPv4Address(self.p1_src_start_ip))
91         p1_dst_start_ip_int = int(IPv4Address(self.p1_dst_start_ip))
92         p2_inner_src_start_ip_int = int(IPv4Address(self.p2_inner_src_start_ip))
93         p2_inner_dst_start_ip_int = int(IPv4Address(self.p2_inner_dst_start_ip))
94
95         # Direction 0 --> 1
96         base_pkt_a = (
97             Ether() /
98             IP(
99                 src=self.p1_src_start_ip,
100                 dst=self.p1_dst_start_ip,
101                 proto=61
102             )
103         )
104         # Direction 1 --> 0
105         base_pkt_b = (
106             Ether() /
107             IP(
108                 src=self.p2_outer_src_ip,
109                 dst=self.p2_outer_dst_ip,
110                 proto=17
111             ) /
112             UDP(
113                 sport=self.p2_udp_sport_start,
114                 dport=self.p2_udp_dport
115             ) /
116             GENEVE(vni=self.p2_geneve_start_vni) /
117             Ether(dst=self.p2_inner_dst_mac) /
118             IP(
119                 src=self.p2_inner_src_start_ip,
120                 dst=self.p2_inner_dst_start_ip,
121                 proto=61
122             )
123         )
124         base_pkt_b /= Raw(load=self._gen_payload(110 - len(base_pkt_b)))
125         # Direction 0 --> 1
126         vm1 = STLScVmRaw(
127             [
128                 STLVmFlowVar(
129                     name=u"ip_src",
130                     min_value=p1_src_start_ip_int,
131                     max_value=p1_src_start_ip_int + self.n_tunnels * 256 - 1,
132                     size=4,
133                     op=u"inc"
134                 ),
135                 STLVmWrFlowVar(
136                     fv_name=u"ip_src",
137                     pkt_offset=u"IP.src"
138                 ),
139                 STLVmFlowVar(
140                     name=u"ip_dst",
141                     min_value=p1_dst_start_ip_int,
142                     max_value=p1_dst_start_ip_int + self.n_tunnels * 256 - 1,
143                     size=4,
144                     op=u"inc"
145                 ),
146                 STLVmWrFlowVar(
147                     fv_name=u"ip_dst",
148                     pkt_offset=u"IP.dst"
149                 ),
150                 STLVmFixIpv4(
151                     offset=u"IP"
152                 )
153             ]
154         )
155         # Direction 1 --> 0
156         vm2 = STLScVmRaw(
157             [
158                 STLVmFlowVar(
159                     name=u"ip",
160                     min_value=0,
161                     max_value=self.n_tunnels * 256 - 1,
162                     size=4,
163                     op=u"inc"
164                 ),
165                 STLVmWrMaskFlowVar(
166                     fv_name=u"ip",
167                     pkt_cast_size=4,
168                     mask=0xffffffff,
169                     add_value=p2_inner_src_start_ip_int,
170                     pkt_offset=u"IP:1.src"
171                 ),
172                 STLVmWrMaskFlowVar(
173                     fv_name=u"ip",
174                     pkt_cast_size=4,
175                     mask=0xffffffff,
176                     add_value=p2_inner_dst_start_ip_int,
177                     pkt_offset=u"IP:1.dst"
178                 ),
179                 STLVmWrMaskFlowVar(
180                     fv_name=u"ip",
181                     pkt_cast_size=2,
182                     mask=0xffff,
183                     add_value=self.p2_udp_sport_start,
184                     pkt_offset=u"UDP.sport"
185                 ),
186                 STLVmWrMaskFlowVar(
187                     fv_name=u"ip",
188                     pkt_cast_size=4,
189                     mask=0xffffff00,
190                     add_value=(self.p2_geneve_start_vni << 8),
191                     pkt_offset=u"GENEVE.vni"
192                 ),
193                 STLVmWrMaskFlowVar(
194                     fv_name=u"ip",
195                     pkt_cast_size=4,
196                     mask=0xffffff,
197                     shift=-8,
198                     offset_fixup=2,
199                     add_value=c_int(
200                         int(
201                             self.p2_inner_dst_mac.replace(u":", u"")[6:12], 16
202                         ) << 8
203                     ).value,
204                     pkt_offset=u"Ether:1.dst"
205                 ),
206                 STLVmFixIpv4(
207                     offset=u"IP:1"
208                 ),
209                 STLVmFixIpv4(
210                     offset=u"IP"
211                 ),
212                 STLVmFixChecksumHw(
213                     l3_offset=u"IP",
214                     l4_offset=u"UDP",
215                     l4_type=CTRexVmInsFixHwCs.L4_TYPE_UDP
216                 )
217             ]
218         )
219
220         return base_pkt_a, base_pkt_b, vm1, vm2
221
222
223 def register():
224     """Register this traffic profile to T-rex.
225
226     Do not change this function.
227
228     :return: Traffic streams.
229     :rtype: Object
230     """
231     return TrafficStreams()