Python3: Migration of files under traffic-profiles/trex
[csit.git] / resources / traffic_profiles / trex / trex-sl-3n-ethip4-ip4dst1000-2cnf.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 /
19  - Direction 0 --> 1:
20    - Source IP address range:      10.0.0.1
21    - Destination IP address range: 20.0.0.0 - 20.0.3.231
22  - Direction 1 --> 0:
23    - Source IP address range:      20.0.0.1
24    - Destination IP address range: 10.0.0.0 - 10.0.3.231
25 """
26
27 from trex.stl.api import *
28 from profile_trex_stateless_base_class import TrafficStreamsBaseClass
29
30
31 class TrafficStreams(TrafficStreamsBaseClass):
32     """Stream profile."""
33
34     def __init__(self):
35         """Initialization and setting of streams' parameters."""
36
37         super(TrafficStreamsBaseClass, self).__init__()
38
39         self.p1_dst_start_mac = u"02:02:00:00:12:00"
40
41         self.p2_dst_start_mac = u"02:02:00:00:02:00"
42
43         # IPs used in packet headers.
44         self.p1_src_start_ip = u"10.0.0.1"
45         self.p1_dst_start_ip = u"20.0.0.0"
46         self.p1_dst_end_ip = u"20.0.3.231"
47
48         self.p2_src_start_ip = u"20.0.0.1"
49         self.p2_dst_start_ip = u"10.0.0.0"
50         self.p2_dst_end_ip = u"10.0.3.231"
51
52     def define_packets(self):
53         """Defines the packets to be sent from the traffic generator.
54
55         Packet definition: | ETH | IP |
56
57         :returns: Packets to be sent from the traffic generator.
58         :rtype: tuple
59         """
60
61         # Direction 0 --> 1
62         base_pkt_a = (
63             Ether(
64                 dst=self.p1_dst_start_mac
65             ) /            IP(
66                 src=self.p1_src_start_ip,
67                 dst=self.p1_dst_start_ip,
68                 proto=61
69             )
70         )
71         # Direction 1 --> 0
72         base_pkt_b = (
73             Ether(
74               dst=self.p2_dst_start_mac
75             ) /
76             IP(
77               src=self.p2_src_start_ip,
78               dst=self.p2_dst_start_ip,
79               proto=61
80             )
81         )
82
83         # Direction 0 --> 1
84         vm1 = STLScVmRaw(
85             [
86                 STLVmFlowVar(
87                     name=u"mac_dst",
88                     min_value=0,
89                     max_value=1,
90                     size=1,
91                     op=u"inc"
92                 ),
93                 STLVmFlowVar(
94                     name=u"dst",
95                     min_value=self.p1_dst_start_ip,
96                     max_value=self.p1_dst_end_ip,
97                     size=4,
98                     op=u"inc"
99                 ),
100                 STLVmWrFlowVar(
101                     fv_name=u"dst",
102                     pkt_offset=u"IP.dst"
103                 ),
104                 STLVmFixIpv4(
105                    offset=u"IP"
106                 )
107             ]
108         )
109         # Direction 1 --> 0
110         vm2 = STLScVmRaw(
111             [
112                 STLVmFlowVar(
113                     name=u"mac_dst",
114                     min_value=0,
115                     max_value=1,
116                     size=1,
117                     op=u"inc"
118                 ),
119                 STLVmWrFlowVar(
120                     fv_name=u"mac_dst",
121                     pkt_offset=5
122                 ),
123                 STLVmFlowVar(
124                     name=u"dst",
125                     min_value=self.p2_dst_start_ip,
126                     max_value=self.p2_dst_end_ip,
127                     size=4,
128                     op=u"inc"
129                 ),
130                 STLVmWrFlowVar(
131                     fv_name=u"dst",
132                     pkt_offset=u"IP.dst"
133                 ),
134                 STLVmFixIpv4(
135                     offset=u"IP"
136                 )
137             ]
138         )
139
140         return base_pkt_a, base_pkt_b, vm1, vm2
141
142
143 def register():
144     """Register this traffic profile to T-rex.
145
146     Do not change this function.
147
148     :return: Traffic streams.
149     :rtype: Object
150     """
151     return TrafficStreams()
152