bc4484c8339125f2ae8ef23ea49b60c14801e196
[csit.git] / GPL / traffic_profiles / trex / trex-stl-ethip4vxlan-ip4src1udpsrcrnd.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 / IP / VXLAN / ETH / IP
28  - Direction 0 --> 1:
29    - Source IP address:                172.17.0.2
30    - Destination IP address:           172.16.0.1
31    - Source UDP port:                  random([1024-65535])
32    - Destination UDP port:             4789
33    - VXLAN VNI:                        0
34    - Payload source MAC address:       00:aa:aa:00:00:[00..ff]
35    - Payload source IP address:        10.0.0.2
36    - Payload destination MAC address:  00:bb:bb:00:00:[00..ff]
37    - Payload destination IP address:   10.0.0.1
38  - Direction 1 --> 0:
39    - Source IP address:                172.27.0.2
40    - Destination IP address:           172.26.0.1
41    - Source UDP port:                  random([1024-65535])
42    - Destination UDP port:             4789
43    - VXLAN VNI:                        0
44    - Payload source MAC address:       00:bb:bb:00:00:[00..ff]
45    - Payload source IP address:        10.0.0.1
46    - Payload destination MAC address:  00:aa:aa:00:00:[00..ff]
47    - Payload destination IP address:   10.0.0.2
48 """
49
50 from trex.stl.api import *
51 from profile_trex_stateless_base_class import TrafficStreamsBaseClass
52
53 # RFC 7348 - Virtual eXtensible Local Area Network (VXLAN):
54 # A Framework for Overlaying Virtualized Layer 2 Networks over Layer 3 Networks
55 # http://tools.ietf.org/html/rfc7348
56 _VXLAN_FLAGS = list(u"R"*24 + u"RRRIRRRRR")
57
58
59 class VXLAN(Packet):
60     name=u"VXLAN"
61     fields_desc = [
62         FlagsField(u"flags", 0x08000000, 32, _VXLAN_FLAGS),
63         ThreeBytesField(u"vni", 0),
64         XByteField(u"reserved", 0x00)
65     ]
66
67     def mysummary(self):
68         return self.sprintf(u"VXLAN (vni=%VXLAN.vni%)")
69
70
71 bind_layers(UDP, VXLAN, dport=4789)
72 bind_layers(VXLAN, Ether)
73
74
75 class TrafficStreams(TrafficStreamsBaseClass):
76     """Stream profile."""
77
78     def __init__(self):
79         """Initialization and setting of streams' parameters."""
80
81         super(TrafficStreamsBaseClass, self).__init__()
82
83         self.nf_chains = 1
84
85     def define_packets(self):
86         """Defines the packets to be sent from the traffic generator.
87
88         Packet definition: | ETH | IP | VXLAN | ETH | IP
89
90         :returns: Packets to be sent from the traffic generator.
91         :rtype: tuple
92         """
93
94         # Direction 0 --> 1
95         base_pkt_a = (
96             Ether()/
97             IP(
98                 src=u"172.17.0.2",
99                 dst=u"172.16.0.1"
100             )/
101             UDP(
102                 sport=1024,
103                 dport=4789)/
104             VXLAN(
105                 vni=0
106             )/
107             Ether(
108                 src=u"00:aa:aa:00:00:00",
109                 dst=u"00:bb:bb:00:00:00"
110             )/
111             IP(
112                 src=u"10.0.0.2",
113                 dst=u"10.0.0.1",
114                 proto=61
115             )
116         )
117
118         # Direction 1 --> 0
119         base_pkt_b = (
120             Ether()/
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=28
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=48
185                 ),
186                 STLVmWrFlowVar(
187                     fv_name=u"in_mac",
188                     pkt_offset=54
189                 ),
190                 STLVmWrFlowVar(
191                     fv_name=u"in_mac",
192                     pkt_offset=60
193                 ),
194                 STLVmFixChecksumHw(
195                     l3_offset="IP:{}".format(0),
196                     l4_offset="UDP:{}".format(0),
197                     l4_type=CTRexVmInsFixHwCs.L4_TYPE_UDP
198                 )
199             ]
200         )
201
202         # Direction 1 --> 0
203         vm2 = STLScVmRaw(
204             [
205                 STLVmFlowVar(
206                     name=u"nf_id",
207                     size=1,
208                     op=u"inc",
209                     min_value=0,
210                     max_value=self.nf_chains - 1
211                 ),
212                 STLVmFlowVar(
213                     name=u"in_mac",
214                     size=2, op=u"inc",
215                     min_value=0,
216                     max_value=255
217                 ),
218                 STLVmFlowVar(
219                     name=u"in_ip",
220                     size=1, op=u"inc",
221                     min_value=0,
222                     max_value=255
223                 ),
224                 STLVmFlowVar(
225                     name=u"src_port",
226                     size=2,
227                     op=u"random",
228                     min_value=1024,
229                     max_value=65535
230                 ),
231                 STLVmWrFlowVar(
232                     fv_name=u"nf_id",
233                     pkt_offset=28
234                 ),
235                 STLVmWrFlowVar(
236                     fv_name=u"src_port",
237                     pkt_offset=u"UDP.sport"
238                 ),
239                 STLVmWrFlowVar(
240                     fv_name=u"nf_id",
241                     pkt_offset=48
242                 ),
243                 STLVmWrFlowVar(
244                     fv_name=u"in_mac",
245                     pkt_offset=54
246                 ),
247                 STLVmWrFlowVar(
248                     fv_name=u"in_mac",
249                     pkt_offset=60
250                 ),
251                 STLVmFixChecksumHw(
252                     l3_offset="IP:{}".format(0),
253                     l4_offset="UDP:{}".format(0),
254                     l4_type=CTRexVmInsFixHwCs.L4_TYPE_UDP
255                 )
256             ]
257         )
258
259         return base_pkt_a, base_pkt_b, vm1, vm2
260
261 def register():
262     """Register this traffic profile to T-rex.
263
264     Do not change this function.
265
266     :return: Traffic streams.
267     :rtype: Object
268     """
269     return TrafficStreams()
270