cf97364da6e6604060e8f4a98997fcb1c1272d60
[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                 STLVmFixIpv4(
186                     offset=u"IP"
187                 )
188             ]
189         )
190
191         # Direction 1 --> 0
192         vm2 = STLScVmRaw(
193             [
194                 STLVmFlowVar(
195                     name=u"nf_id",
196                     size=1,
197                     op=u"inc",
198                     min_value=0,
199                     max_value=self.nf_chains - 1
200                 ),
201                 STLVmFlowVar(
202                     name=u"in_mac",
203                     size=2, op=u"inc",
204                     min_value=0,
205                     max_value=255
206                 ),
207                 STLVmFlowVar(
208                     name=u"in_ip",
209                     size=1, op=u"inc",
210                     min_value=0,
211                     max_value=255
212                 ),
213                 STLVmFlowVar(
214                     name=u"src_port",
215                     size=2,
216                     op=u"random",
217                     min_value=1024,
218                     max_value=65535
219                 ),
220                 STLVmWrFlowVar(
221                     fv_name=u"nf_id",
222                     pkt_offset=28
223                 ),
224                 STLVmWrFlowVar(
225                     fv_name=u"src_port",
226                     pkt_offset=u"UDP.sport"
227                 ),
228                 STLVmWrFlowVar(
229                     fv_name=u"nf_id",
230                     pkt_offset=48
231                 ),
232                 STLVmWrFlowVar(
233                     fv_name=u"in_mac",
234                     pkt_offset=54
235                 ),
236                 STLVmWrFlowVar(
237                     fv_name=u"in_mac",
238                     pkt_offset=60
239                 ),
240                 STLVmFixIpv4(
241                     offset=u"IP"
242                 )
243             ]
244         )
245
246         return base_pkt_a, base_pkt_b, vm1, vm2
247
248 def register():
249     """Register this traffic profile to T-rex.
250
251     Do not change this function.
252
253     :return: Traffic streams.
254     :rtype: Object
255     """
256     return TrafficStreams()
257