Separate files needing GPL license
[csit.git] / GPL / traffic_profiles / trex / trex-sl-3n-dot1qip4-vlan100ip4src254ip4dst254.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 - 100
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 - 100
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 = 100
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         # IPs used in packet headers.
51         self.p1_src_start_ip = u"10.0.0.1"
52         self.p1_src_end_ip = u"10.0.0.254"
53
54         self.p1_dst_start_ip = u"20.0.0.1"
55         self.p1_dst_end_ip = u"20.0.0.254"
56
57         self.p2_src_start_ip = u"20.0.0.1"
58         self.p2_src_end_ip = u"20.0.0.254"
59
60         self.p2_dst_start_ip = u"10.0.0.1"
61         self.p2_dst_end_ip = u"10.0.0.254"
62
63     def define_packets(self):
64         """Defines the packets to be sent from the traffic generator.
65
66         Packet definition: | ETH | DOT1Q | IP |
67
68         :returns: Packets to be sent from the traffic generator.
69         :rtype: tuple
70         """
71
72         # Direction 0 --> 1
73         base_pkt_a = (
74             Ether() /
75             Dot1Q(
76                 vlan=self.p1_vlan_start
77             ) /
78             IP(
79                 src=self.p1_src_start_ip,
80                 dst=self.p1_dst_start_ip,
81                 proto=61
82             )
83         )
84         # Direction 1 --> 0
85         base_pkt_b = (
86             Ether() /
87             Dot1Q(
88                 vlan=self.p2_vlan_start
89             ) /
90             IP(
91                 src=self.p2_src_start_ip,
92                 dst=self.p2_dst_start_ip,
93                 proto=61
94             )
95         )
96
97         # Direction 0 --> 1
98         vm1 = STLScVmRaw(
99             [
100                 STLVmFlowVar(
101                     name=u"vlan",
102                     min_value=self.p1_vlan_start,
103                     max_value=self.p1_vlan_end,
104                     size=2,
105                     op=u"inc"
106                 ),
107                 STLVmWrFlowVar(
108                     fv_name=u"vlan",
109                     pkt_offset=u"Dot1Q.vlan"
110                 ),
111                 STLVmFlowVar(
112                     name=u"ip_src",
113                     min_value=self.p1_src_start_ip,
114                     max_value=self.p1_src_end_ip,
115                     size=4,
116                     op=u"random"
117                 ),
118                 STLVmWrFlowVar(
119                     fv_name=u"ip_src",
120                     pkt_offset=u"IP.src"
121                 ),
122                 STLVmFlowVar(
123                     name=u"ip_dst",
124                     min_value=self.p1_dst_start_ip,
125                     max_value=self.p1_dst_end_ip,
126                     size=4,
127                     op=u"random"
128                 ),
129                 STLVmWrFlowVar(
130                     fv_name=u"ip_dst",
131                     pkt_offset=u"IP.dst"
132                 ),
133                 STLVmFixIpv4(
134                     offset=u"IP"
135                 )
136             ]
137         )
138         # Direction 1 --> 0
139         vm2 = STLScVmRaw(
140             [
141                 STLVmFlowVar(
142                     name=u"vlan",
143                     min_value=self.p2_vlan_start,
144                     max_value=self.p2_vlan_end,
145                     size=2,
146                     op=u"inc"
147                 ),
148                 STLVmWrFlowVar(
149                     fv_name=u"vlan",
150                     pkt_offset=u"Dot1Q.vlan"
151                 ),
152                 STLVmFlowVar(
153                     name=u"ip_src",
154                     min_value=self.p2_src_start_ip,
155                     max_value=self.p2_src_end_ip,
156                     size=4,
157                     op=u"random"
158                 ),
159                 STLVmWrFlowVar(
160                     fv_name=u"ip_src",
161                     pkt_offset=u"IP.src"
162                 ),
163                 STLVmFlowVar(
164                     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()