Python3: Migration of files under traffic-profiles/trex
[csit.git] / resources / traffic_profiles / trex / trex-sl-2n-ethip4udp-10u1000p-conc.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:      10.10.10.2 - 10.10.10.11
21    - Destination IP address range: 20.20.20.2 - 20.20.20.11
22    - Source UDP port range:        1001 - 2000
23    - Destination UDP port range:   2001 - 3000
24  - Direction 1 --> 0:
25    - Source IP address range:      20.20.20.2 - 20.20.20.11
26    - Destination IP address range: 10.10.10.2 - 10.10.10.11
27    - Source UDP port range:        2001 - 3000
28    - Destination UDP port range:   1001 - 2000
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"10.10.10.2"
45         self.p1_src_end_ip = u"10.10.10.11"
46         self.p1_dst_start_ip = u"20.20.20.2"
47         self.p1_dst_end_ip = u"20.20.20.11"
48
49         self.p2_src_start_ip = u"20.20.20.2"
50         self.p2_src_end_ip = u"20.20.20.11"
51         self.p2_dst_start_ip = u"10.10.10.2"
52         self.p2_dst_end_ip = u"10.10.10.11"
53
54         # UDP ports used in packet headers.
55         self.p1_src_start_udp_port = 1001
56         self.p1_src_end_udp_port = 2000
57         self.p1_dst_start_udp_port = 2001
58         self.p1_dst_end_udp_port = 3000
59
60         self.p2_src_start_udp_port = 2001
61         self.p2_src_end_udp_port = 3000
62         self.p2_dst_start_udp_port = 1001
63         self.p2_dst_end_udp_port = 2000
64
65     def define_packets(self):
66         """Defines the packets to be sent from the traffic generator.
67
68         Packet definition: | ETH | IP | UDP |
69
70         :returns: Packets to be sent from the traffic generator.
71         :rtype: tuple
72         """
73
74         # Direction 0 --> 1
75         base_pkt_a = (
76             Ether() /
77             IP(
78                 src=self.p1_src_start_ip,
79                 dst=self.p1_dst_start_ip,
80                 proto=17
81             ) /
82             UDP(
83                 sport=self.p1_src_start_udp_port,
84                 dport=self.p1_dst_start_udp_port
85             )
86         )
87         # Direction 1 --> 0
88         base_pkt_b = (
89             Ether() /
90             IP(
91                   src=self.p2_src_start_ip,
92                   dst=self.p2_dst_start_ip,
93                   proto=17
94             ) /
95             UDP(
96                 sport=self.p2_src_start_udp_port,
97                 dport=self.p2_dst_start_udp_port
98             )
99         )
100
101         # Direction 0 --> 1
102         vm1 = STLScVmRaw(
103             [
104                 STLVmTupleGen(
105                     ip_min=self.p1_src_start_ip,
106                     ip_max=self.p1_src_end_ip,
107                     port_min=self.p1_src_start_udp_port,
108                     port_max=self.p1_src_end_udp_port,
109                     name=u"tuple1_src"
110                 ),
111                 STLVmTupleGen(
112                     ip_min=self.p1_dst_start_ip,
113                     ip_max=self.p1_dst_end_ip,
114                     port_min=self.p1_dst_start_udp_port,
115                     port_max=self.p1_dst_end_udp_port,
116                     name=u"tuple1_dst"
117                 ),
118                 STLVmWrFlowVar(
119                     fv_name=u"tuple1_src.ip",
120                     pkt_offset=u"IP.src"
121                 ),
122                 STLVmWrFlowVar(
123                     fv_name=u"tuple1_dst.ip",
124                     pkt_offset=u"IP.dst"
125                 ),
126                 STLVmFixIpv4(
127                     offset=u"IP"
128                 ),
129                 STLVmWrFlowVar(
130                     fv_name=u"tuple1_src.port",
131                     pkt_offset=u"UDP.sport"
132                 ),
133                 STLVmWrFlowVar(
134                     fv_name=u"tuple1_dst.port",
135                     pkt_offset=u"UDP.dport"
136                 )
137             ]
138         )
139         # Direction 0 --> 1
140         vm2 = STLScVmRaw(
141             [
142                 STLVmTupleGen(
143                     ip_min=self.p2_src_start_ip,
144                     ip_max=self.p2_src_end_ip,
145                     port_min=self.p2_src_start_udp_port,
146                     port_max=self.p2_src_end_udp_port,
147                     name=u"tuple2_src"
148                 ),
149                 STLVmTupleGen(
150                     ip_min=self.p2_dst_start_ip,
151                     ip_max=self.p2_dst_end_ip,
152                     port_min=self.p2_dst_start_udp_port,
153                     port_max=self.p2_dst_end_udp_port,
154                     name=u"tuple2_dst"
155                 ),
156                 STLVmWrFlowVar(
157                     fv_name=u"tuple2_src.ip",
158                     pkt_offset=u"IP.src"
159                 ),
160                 STLVmWrFlowVar(
161                     fv_name=u"tuple2_dst.ip",
162                     pkt_offset=u"IP.dst"
163                 ),
164                 STLVmFixIpv4(
165                     offset=u"IP"
166                 ),
167                 STLVmWrFlowVar(
168                     fv_name=u"tuple2_src.port",
169                     pkt_offset=u"UDP.sport"
170                 ),
171                 STLVmWrFlowVar(
172                     fv_name=u"tuple2_dst.port",
173                     pkt_offset=u"UDP.dport"
174                 )
175             ]
176         )
177
178         return base_pkt_a, base_pkt_b, vm1, vm2
179
180
181 def register():
182     """Register this traffic profile to T-rex.
183
184     Do not change this function.
185
186     :return: Traffic streams.
187     :rtype: Object
188     """
189     return TrafficStreams()

©2016 FD.io a Linux Foundation Collaborative Project. All Rights Reserved.
Linux Foundation is a registered trademark of The Linux Foundation. Linux is a registered trademark of Linus Torvalds.
Please see our privacy policy and terms of use.