e286a1e8d539ed7c0c1ec3cceed09203e6468884
[csit.git] / resources / traffic_profiles / trex / trex-sl-dot1qip4vxlan-ip4src10udpsrcrnd.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..9].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..9]
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..9].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..9]
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 = 10
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, op=u"inc",
221                     min_value=0,
222                     max_value=255
223                 ),
224                 STLVmFlowVar(
225                     name=u"in_ip",
226                     size=1,
227                     op=u"inc",
228                     min_value=0,
229                     max_value=255
230                 ),
231                 STLVmFlowVar(
232                     name=u"src_port",
233                     size=2,
234                     op=u"random",
235                     min_value=1024,
236                     max_value=65535
237                 ),
238                 STLVmWrFlowVar(
239                     fv_name=u"nf_id",
240                     pkt_offset=32
241                 ),
242                 STLVmWrFlowVar(
243                     fv_name=u"src_port",
244                     pkt_offset=u"UDP.sport"
245                 ),
246                 STLVmWrFlowVar(
247                     fv_name=u"nf_id",
248                     pkt_offset=52
249                 ),
250                 STLVmWrFlowVar(
251                     fv_name=u"in_mac",
252                     pkt_offset=58
253                 ),
254                 STLVmWrFlowVar(
255                     fv_name=u"in_mac",
256                     pkt_offset=64
257                 ),
258                 STLVmWrFlowVar(
259                     fv_name=u"in_ip",
260                     pkt_offset=82
261                 ),
262                 STLVmWrFlowVar(
263                     fv_name=u"in_ip",
264                     pkt_offset=86
265                 ),
266                 STLVmFixIpv4(
267                     offset=u"IP"
268                 )
269             ]
270         )
271
272         return base_pkt_a, base_pkt_b, vm1, vm2
273
274 def register():
275     """Register this traffic profile to T-rex.
276
277     Do not change this function.
278
279     :return: Traffic streams.
280     :rtype: Object
281     """
282     return TrafficStreams()
283