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