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