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