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