License: Wrap GPL block to 80 characters
[csit.git] / GPL / traffic_profiles / trex / trex-stl-dot1qip4vxlan-ip4src1udpsrcrnd.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  - Packet: ETH / DOT1Q / IP / VXLAN / ETH / IP
29  - Direction 0 --> 1:
30    - VLAN range:                       100
31    - Source IP address:                172.17.0.2
32    - Destination IP address:           172.16.0.1
33    - Source UDP port:                  random([1024..65535])
34    - Destination UDP port:             4789
35    - VXLAN VNI:                        0
36    - Payload source MAC address:       00:aa:aa:00:00:[00..ff]
37    - Payload source IP address:        10.0.[0..255].2
38    - Payload destination MAC address:  00:bb:bb:00:00:[00..ff]
39    - Payload destination IP address:   10.0.[0..255].1
40  - Direction 1 --> 0:
41    - VLAN range:                       200
42    - Source IP address:                172.27.0.2
43    - Destination IP address:           172.26.0.1
44    - Source UDP port:                  random([1024..65535])
45    - Destination UDP port:             4789
46    - VXLAN VNI:                        0
47    - Payload source MAC address:       00:bb:bb:00:00:[00..ff]
48    - Payload source IP address:        10.0.[0..255].1
49    - Payload destination MAC address:  00:aa:aa:00:00:[00..ff]
50    - Payload destination IP address:   10.0.[0..255].2
51 """
52
53 from trex.stl.api import *
54 from profile_trex_stateless_base_class import TrafficStreamsBaseClass
55
56 # RFC 7348 - Virtual eXtensible Local Area Network (VXLAN):
57 # A Framework for Overlaying Virtualized Layer 2 Networks over Layer 3 Networks.
58 # http://tools.ietf.org/html/rfc7348
59 _VXLAN_FLAGS = list(u"R"*24 + u"RRRIRRRRR")
60
61
62 class VXLAN(Packet):
63     name=u"VXLAN"
64     fields_desc = [
65         FlagsField(u"flags", 0x08000000, 32, _VXLAN_FLAGS),
66         ThreeBytesField(u"vni", 0),
67         XByteField(u"reserved", 0x00)
68     ]
69
70     def mysummary(self):
71         return self.sprintf(u"VXLAN (vni=%VXLAN.vni%)")
72
73
74 bind_layers(UDP, VXLAN, dport=4789)
75 bind_layers(VXLAN, Ether)
76
77
78 class TrafficStreams(TrafficStreamsBaseClass):
79     """Stream profile."""
80
81     def __init__(self):
82         """Initialization and setting of streams' parameters."""
83
84         super(TrafficStreamsBaseClass, self).__init__()
85
86         self.nf_chains = 1
87
88     def define_packets(self):
89         """Defines the packets to be sent from the traffic generator.
90
91         Packet definition: | ETH | DOT1Q | IP | VXLAN | ETH | IP
92
93         :returns: Packets to be sent from the traffic generator.
94         :rtype: tuple
95         """
96
97         # Direction 0 --> 1
98         base_pkt_a = (
99             Ether()/
100             Dot1Q(
101                 vlan=100
102             ) /
103             IP(
104                 src=u"172.17.0.2",
105                 dst=u"172.16.0.1"
106             )/
107             UDP(
108                 sport=1024,
109                 dport=4789
110             )/
111             VXLAN(
112                 vni=0
113             )/
114             Ether(
115                 src=u"00:aa:aa:00:00:00",
116                 dst=u"00:bb:bb:00:00:00"
117             )/
118             IP(
119                 src=u"10.0.0.2",
120                 dst=u"10.0.0.1",
121                 proto=61
122             )
123         )
124
125         # Direction 1 --> 0
126         base_pkt_b = (
127             Ether()/
128             Dot1Q(
129                 vlan=200
130             ) /
131             IP(
132                 src=u"172.27.0.2",
133                 dst=u"172.26.0.1"
134             )/
135             UDP(
136                 sport=1024,
137                 dport=4789
138             )/
139             VXLAN(
140                 vni=0
141             )/
142             Ether(
143                 src=u"00:bb:bb:00:00:00",
144                 dst=u"00:aa:aa:00:00:00"
145             )/
146             IP(
147                 src=u"10.0.0.1",
148                 dst=u"10.0.0.2",
149                 proto=61
150             )
151         )
152
153         # Direction 0 --> 1
154         vm1 = STLScVmRaw(
155             [
156                 STLVmFlowVar(
157                     name=u"nf_id",
158                     size=1,
159                     op=u"inc",
160                     min_value=0,
161                     max_value=self.nf_chains - 1
162                 ),
163                 STLVmFlowVar(
164                     name=u"in_mac",
165                     size=2,
166                     op=u"inc",
167                     min_value=0,
168                     max_value=255
169                 ),
170                 STLVmFlowVar(
171                     name=u"in_ip",
172                     size=1,
173                     op=u"inc",
174                     min_value=0,
175                     max_value=255
176                 ),
177                 STLVmFlowVar(
178                     name=u"src_port",
179                     size=2,
180                     op=u"random",
181                     min_value=1024,
182                     max_value=65535
183                 ),
184                 STLVmWrFlowVar(
185                     fv_name=u"nf_id",
186                     pkt_offset=32
187                 ),
188                 STLVmWrFlowVar(
189                     fv_name=u"src_port",
190                     pkt_offset=u"UDP.sport"
191                 ),
192                 STLVmWrFlowVar(
193                     fv_name=u"nf_id",
194                     pkt_offset=52
195                 ),
196                 STLVmWrFlowVar(
197                     fv_name=u"in_mac",
198                     pkt_offset=58
199                 ),
200                 STLVmWrFlowVar(
201                     fv_name=u"in_mac",
202                     pkt_offset=64
203                 ),
204                 STLVmWrFlowVar(
205                     fv_name=u"in_ip",
206                     pkt_offset=82
207                 ),
208                 STLVmWrFlowVar(
209                     fv_name=u"in_ip",
210                     pkt_offset=86
211                 ),
212                 STLVmFixChecksumHw(
213                     l3_offset="IP:{}".format(0),
214                     l4_offset="UDP:{}".format(0),
215                     l4_type=CTRexVmInsFixHwCs.L4_TYPE_UDP
216                 )
217             ]
218         )
219
220         # Direction 1 --> 0
221         vm2 = STLScVmRaw(
222             [
223                 STLVmFlowVar(
224                     name=u"nf_id",
225                     size=1,
226                     op=u"inc",
227                     min_value=0,
228                     max_value=self.nf_chains - 1
229                 ),
230                 STLVmFlowVar(
231                     name=u"in_mac",
232                     size=2,
233                     op=u"inc",
234                     min_value=0,
235                     max_value=255
236                 ),
237                 STLVmFlowVar(
238                     name=u"in_ip",
239                     size=1,
240                     op=u"inc",
241                     min_value=0,
242                     max_value=255
243                 ),
244                 STLVmFlowVar(
245                     name=u"src_port",
246                     size=2,
247                     op=u"random",
248                     min_value=1024,
249                     max_value=65535
250                 ),
251                 STLVmWrFlowVar(
252                     fv_name=u"nf_id",
253                     pkt_offset=32
254                 ),
255                 STLVmWrFlowVar(
256                     fv_name=u"src_port",
257                     pkt_offset=u"UDP.sport"
258                 ),
259                 STLVmWrFlowVar(
260                     fv_name=u"nf_id",
261                     pkt_offset=52
262                 ),
263                 STLVmWrFlowVar(
264                     fv_name=u"in_mac",
265                     pkt_offset=58
266                 ),
267                 STLVmWrFlowVar(
268                     fv_name=u"in_mac",
269                     pkt_offset=64
270                 ),
271                 STLVmWrFlowVar(
272                     fv_name=u"in_ip",
273                     pkt_offset=82
274                 ),
275                 STLVmWrFlowVar(
276                     fv_name=u"in_ip",
277                     pkt_offset=86
278                 ),
279                 STLVmFixChecksumHw(
280                     l3_offset="IP:{}".format(0),
281                     l4_offset="UDP:{}".format(0),
282                     l4_type=CTRexVmInsFixHwCs.L4_TYPE_UDP
283                 )
284             ]
285         )
286
287         return base_pkt_a, base_pkt_b, vm1, vm2
288
289 def register():
290     """Register this traffic profile to T-rex.
291
292     Do not change this function.
293
294     :return: Traffic streams.
295     :rtype: Object
296     """
297     return TrafficStreams()
298