8fa680c804da14930790ef680945589c6fa7025f
[csit.git] / GPL / traffic_profiles / trex / trex-stl-ethip4udp-4096u63p.py
1 # Copyright (c) 2020 Cisco 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  - Two streams sent in directions 0 --> 1 and 1 --> 0 at the same time.
27  - Packet: ETH / IP / UDP
28  - Direction 0 --> 1:
29    - Source IP address range:      192.168.0.0 - 192.168.15.255
30    - Destination IP address range: 20.0.0.0
31    - Source UDP port range:        1024 - 1086
32    - Destination UDP port range:   1024
33  - Direction 1 --> 0:
34    - Source IP address range:      20.0.0.0
35    - Destination IP address range: 68.142.68.0 - 68.142.68.3
36    - Source UDP port range:        1024
37    - Destination UDP port range:   1024 - 65535
38 """
39
40 from trex.stl.api import *
41 from profile_trex_stateless_base_class import TrafficStreamsBaseClass
42
43
44 class TrafficStreams(TrafficStreamsBaseClass):
45     """Stream profile."""
46
47     def __init__(self):
48         """Initialization and setting of streams' parameters."""
49
50         super(TrafficStreamsBaseClass, self).__init__()
51
52         # IPs used in packet headers.
53         self.p1_src_start_ip = u"192.168.0.0"
54         self.p1_src_end_ip = u"192.168.15.255"
55         self.p1_dst_start_ip = u"20.0.0.0"
56         self.p1_dst_end_ip = u"20.0.0.0"
57
58         self.p2_src_start_ip = u"20.0.0.0"
59         self.p2_src_end_ip = u"20.0.0.0"
60         self.p2_dst_start_ip = u"68.142.68.0"
61         self.p2_dst_end_ip = u"68.142.68.3"
62
63         # UDP ports used in packet headers.
64         self.p1_src_start_udp_port = 1024
65         self.p1_src_end_udp_port = 1086
66         self.p1_dst_start_udp_port = 1024
67         self.p1_dst_end_udp_port = 1024
68
69         self.p2_src_start_udp_port = 1024
70         self.p2_src_end_udp_port = 1024
71         self.p2_dst_start_udp_port = 1024
72         self.p2_dst_end_udp_port = 65535
73
74     def define_packets(self):
75         """Defines the packets to be sent from the traffic generator.
76
77         Packet definition: | ETH | IP | UDP |
78
79         :returns: Packets to be sent from the traffic generator.
80         :rtype: tuple
81         """
82
83         # Direction 0 --> 1
84         base_pkt_a = (
85             Ether() /
86             IP(
87                 src=self.p1_src_start_ip,
88                 dst=self.p1_dst_start_ip,
89                 proto=17
90             ) /
91             UDP(
92                 sport=self.p1_src_start_udp_port,
93                 dport=self.p1_dst_start_udp_port
94             )
95         )
96         # Direction 1 --> 0
97         base_pkt_b = (
98             Ether() /
99             IP(
100                 src=self.p2_src_start_ip,
101                 dst=self.p2_dst_start_ip,
102                 proto=17
103             ) /
104             UDP(
105                 sport=self.p2_src_start_udp_port,
106                 dport=self.p2_dst_start_udp_port
107             )
108         )
109
110         # Direction 0 --> 1
111         vm1 = STLVM()
112         vm1.var(name="sIP",
113                 min_value=self.p1_src_start_ip,
114                 max_value=self.p1_src_end_ip,
115                 size=4,
116                 op="inc",
117                 next_var="sport")
118         vm1.var(name="sport",
119                 min_value=self.p1_src_start_udp_port,
120                 max_value=self.p1_src_end_udp_port,
121                 size=2,
122                 op="inc")
123         vm1.var(name="dIP",
124                 min_value=self.p1_dst_start_ip,
125                 max_value=self.p1_dst_end_ip,
126                 size=4,
127                 op="inc")
128         vm1.var(name="dport",
129                 min_value=self.p1_dst_start_udp_port,
130                 max_value=self.p1_dst_end_udp_port,
131                 size=2,
132                 op="inc")
133         vm1.write(fv_name="sIP", pkt_offset="IP.src")
134         vm1.write(fv_name="sport", pkt_offset="UDP.sport")
135         vm1.write(fv_name="dIP", pkt_offset="IP.dst")
136         vm1.write(fv_name="dport", pkt_offset="UDP.dport")
137         vm1.fix_chksum(offset='IP')
138         # Direction 0 --> 1
139         vm2 = STLVM()
140         vm2.var(name="sIP",
141                 min_value=self.p2_src_start_ip,
142                 max_value=self.p2_src_end_ip,
143                 size=4,
144                 op="inc",
145                 next_var="sport")
146         vm2.var(name="sport",
147                 min_value=self.p2_src_start_udp_port,
148                 max_value=self.p2_src_end_udp_port,
149                 size=2,
150                 op="inc")
151         vm2.var(name="dIP",
152                 min_value=self.p2_dst_start_ip,
153                 max_value=self.p2_dst_end_ip,
154                 size=4,
155                 op="inc",
156                 next_var="dport")
157         vm2.var(name="dport",
158                 min_value=self.p2_dst_start_udp_port,
159                 max_value=self.p2_dst_end_udp_port,
160                 size=2,
161                 op="inc")
162         vm2.write(fv_name="sIP", pkt_offset="IP.src")
163         vm2.write(fv_name="sport", pkt_offset="UDP.sport")
164         vm2.write(fv_name="dIP", pkt_offset="IP.dst")
165         vm2.write(fv_name="dport", pkt_offset="UDP.dport")
166         vm2.fix_chksum(offset='IP')
167
168         return base_pkt_a, base_pkt_b, vm1, vm2
169
170 def register():
171     """Register this traffic profile to T-rex.
172
173     Do not change this function.
174
175     :return: Traffic streams.
176     :rtype: Object
177     """
178     return TrafficStreams()