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