b2d83b55b3a56b6baa68a9db1c0c8394eccc0b92
[csit.git] / GPL / traffic_profiles / trex / trex-stl-2n-ethip4udp-lb.py
1 # Copyright (c) 2020 Intel 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  - Packet: ETH / IP / UDP
27  - Direction 0 --> 1:
28    - Source IP address range:      192.168.50.74 - 192.168.50.79
29    - Destination IP address range: 90.1.2.1
30  - Direction 1 --> 0:
31    - Source IP address range:      192.168.60.74 - 192.168.60.79
32    - Destination IP address range: 192.168.50.74 - 192.168.50.79
33 """
34
35 from trex.stl.api import *
36 from profile_trex_stateless_base_class import TrafficStreamsBaseClass
37
38
39 class TrafficStreams(TrafficStreamsBaseClass):
40     """Stream profile."""
41
42     def __init__(self):
43         """Initialization and setting of streams' parameters."""
44
45         super(TrafficStreamsBaseClass, self).__init__()
46
47         # IPs used in packet headers.
48         self.p1_src_start_ip = u"192.168.50.74"
49         self.p1_src_end_ip = u"192.168.50.79"
50         self.p1_dst_start_ip = u"90.1.2.1"
51
52         self.p2_src_start_ip = u"192.168.60.74"
53         self.p2_src_end_ip = u"192.168.60.79"
54         self.p2_dst_start_ip = u"192.168.50.74"
55         self.p2_dst_end_ip = u"192.168.50.79"
56
57         # UDP ports used in packet headers.
58         self.p1_src_udp_port = 63
59         self.p1_dst_udp_port = 20000
60
61         self.p2_src_udp_port = 3307
62         self.p2_dst_udp_port = 63
63
64     def define_packets(self):
65         """Defines the packets to be sent from the traffic generator.
66
67         Packet definition: | ETH | IP | UDP
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             IP(
77                 src=self.p1_src_start_ip,
78                 dst=self.p1_dst_start_ip,
79                 proto=17) /
80             UDP(
81                 sport=self.p1_src_udp_port,
82                 dport=self.p1_dst_udp_port
83              )
84         )
85         # Direction 1 --> 0
86         base_pkt_b = (
87             Ether() /
88             IP(
89                 src=self.p2_src_start_ip,
90                 dst=self.p2_dst_start_ip,
91                 proto=17
92              ) /
93             UDP(
94                 sport=self.p2_src_udp_port,
95                 dport=self.p2_dst_udp_port
96              )
97         )
98
99         # Direction 0 --> 1
100         vm1 = STLScVmRaw(
101             [
102                 STLVmFlowVar(
103                     name=u"src",
104                     min_value=self.p1_src_start_ip,
105                     max_value=self.p1_src_end_ip,
106                     size=4,
107                     op=u"inc"
108                 ),
109                 STLVmWrFlowVar(
110                     fv_name=u"src",
111                     pkt_offset=u"IP.src"
112                 ),
113                 STLVmFixIpv4(
114                     offset=u"IP"
115                 )
116             ]
117         )
118         # Direction 1 --> 0
119         vm2 = STLScVmRaw(
120             [
121                 STLVmFlowVar(
122                     name=u"src",
123                     min_value=self.p2_src_start_ip,
124                     max_value=self.p2_src_end_ip,
125                     size=4,
126                     op=u"inc"
127                 ),
128                 STLVmWrFlowVar(
129                     fv_name=u"src",
130                     pkt_offset=u"IP.src"
131                 ),
132                 STLVmFlowVar(
133                     name=u"dst",
134                     min_value=self.p2_dst_start_ip,
135                     max_value=self.p2_dst_end_ip,
136                     size=4,
137                     op=u"inc"
138                 ),
139                 STLVmWrFlowVar(
140                     fv_name=u"dst",
141                     pkt_offset=u"IP.dst"
142                 ),
143                 STLVmFixIpv4(
144                     offset=u"IP"
145                 )
146             ]
147         )
148         return base_pkt_a, base_pkt_b, vm1, vm2
149
150
151 def register():
152     """Register this traffic profile to T-rex.
153
154     Do not change this function.
155
156     :return: Traffic streams.
157     :rtype: Object
158     """
159     return TrafficStreams()