Python3: Migration of files under traffic-profiles/trex
[csit.git] / resources / traffic_profiles / trex / trex-sl-3n-dot1qip4-vlan10ip4src254ip4dst254.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 /
19  - Direction 0 --> 1:
20    - VLAN range:                    1 - 10
21    - Source IP address range:       10.0.0.1 - 10.0.0.254
22    - Destination IP address range:  20.0.0.1 - 20.0.0.254
23  - Direction 1 --> 0:
24    - VLAN range:                    1 - 10
25    - Source IP address range:       20.0.0.1 - 20.0.0.254
26    - Destination IP address range:  10.0.0.1 - 10.0.0.254
27 """
28
29 from trex.stl.api import *
30 from profile_trex_stateless_base_class import TrafficStreamsBaseClass
31
32
33 class TrafficStreams(TrafficStreamsBaseClass):
34     """Stream profile."""
35
36     def __init__(self):
37         """Initialization and setting of streams' parameters."""
38
39         super(TrafficStreamsBaseClass, self).__init__()
40
41         # VLAN IDs
42         self.vlans = 10
43
44         self.p1_vlan_start = 1
45         self.p1_vlan_end = self.p1_vlan_start + self.vlans - 1
46
47         self.p2_vlan_start = 1
48         self.p2_vlan_end = self.p2_vlan_start + self.vlans - 1
49
50
51         # IPs used in packet headers.
52         self.p1_src_start_ip = u"10.0.0.1"
53         self.p1_src_end_ip = u"10.0.0.254"
54
55         self.p1_dst_start_ip = u"20.0.0.1"
56         self.p1_dst_end_ip = u"20.0.0.254"
57
58         self.p2_src_start_ip = u"20.0.0.1"
59         self.p2_src_end_ip = u"20.0.0.254"
60
61         self.p2_dst_start_ip = u"10.0.0.1"
62         self.p2_dst_end_ip = u"10.0.0.254"
63
64     def define_packets(self):
65         """Defines the packets to be sent from the traffic generator.
66
67         Packet definition: | ETH | DOT1Q | IP |
68
69         :returns: Packets to be sent from the traffic generator.
70         :rtype: tuple
71         """
72
73         # Direction 0 --> 1
74         base_pkt_a = (
75             Ether() /
76             Dot1Q(
77                 vlan=self.p1_vlan_start
78             ) /
79             IP(
80                 src=self.p1_src_start_ip,
81                 dst=self.p1_dst_start_ip,
82                 proto=61
83             )
84         )
85         # Direction 1 --> 0
86         base_pkt_b = (
87             Ether() /
88             Dot1Q(
89                 vlan=self.p2_vlan_start
90             ) /
91             IP(
92                 src=self.p2_src_start_ip,
93                 dst=self.p2_dst_start_ip,
94                 proto=61
95             )
96         )
97
98         # Direction 0 --> 1
99         vm1 = STLScVmRaw(
100             [
101                 STLVmFlowVar(
102                     name=u"vlan",
103                     min_value=self.p1_vlan_start,
104                     max_value=self.p1_vlan_end,
105                     size=2,
106                     op=u"inc"
107                 ),
108                 STLVmWrFlowVar(
109                     fv_name=u"vlan",
110                     pkt_offset=u"Dot1Q.vlan"
111                 ),
112                 STLVmFlowVar(
113                   name=u"ip_src",
114                     min_value=self.p1_src_start_ip,
115                     max_value=self.p1_src_end_ip,
116                     size=4,
117                     op=u"random"
118                 ),
119                 STLVmWrFlowVar(
120                     fv_name=u"ip_src",
121                     pkt_offset=u"IP.src"
122                 ),
123                 STLVmFlowVar(
124                     name=u"ip_dst",
125                     min_value=self.p1_dst_start_ip,
126                     max_value=self.p1_dst_end_ip,
127                     size=4,
128                     op=u"random"
129                 ),
130                 STLVmWrFlowVar(
131                     fv_name=u"ip_dst",
132                     pkt_offset=u"IP.dst"
133                 ),
134                 STLVmFixIpv4(
135                     offset=u"IP"
136                 )
137             ]
138         )
139         # Direction 1 --> 0
140         vm2 = STLScVmRaw(
141             [
142                 STLVmFlowVar(
143                     name=u"vlan",
144                     min_value=self.p2_vlan_start,
145                     max_value=self.p2_vlan_end,
146                     size=2,
147                     op=u"inc"
148                 ),
149                 STLVmWrFlowVar(
150                     fv_name=u"vlan",
151                     pkt_offset=u"Dot1Q.vlan"
152                 ),
153                 STLVmFlowVar(
154                     name=u"ip_src",
155                     min_value=self.p2_src_start_ip,
156                     max_value=self.p2_src_end_ip,
157                     size=4,
158                     op=u"random"
159                 ),
160                 STLVmWrFlowVar(
161                     fv_name=u"ip_src",
162                     pkt_offset=u"IP.src"
163                 ),
164                 STLVmFlowVar(name=u"ip_dst",
165                     min_value=self.p2_dst_start_ip,
166                     max_value=self.p2_dst_end_ip,
167                     size=4,
168                     op=u"random"
169                 ),
170                 STLVmWrFlowVar(
171                     fv_name=u"ip_dst",
172                     pkt_offset=u"IP.dst"
173                 ),
174                 STLVmFixIpv4(
175                     offset=u"IP"
176                 )
177             ]
178         )
179
180         return base_pkt_a, base_pkt_b, vm1, vm2
181
182
183 def register():
184     """Register this traffic profile to T-rex.
185
186     Do not change this function.
187
188     :returns: Traffic streams.
189     :rtype: Object
190     """
191     return TrafficStreams()