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