1b9f483b13ecf2ffaf7aea7a33113bcd4472b5d7
[csit.git] / GPL / traffic_profiles / trex / trex-stl-ethip4udp-1024u63p.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:      192.168.0.0 - 192.168.3.255
21    - Destination IP address range: 20.0.0.0
22    - Source UDP port range:        1024 - 1086
23    - Destination UDP port range:   1024
24  - Direction 1 --> 0:
25    - Source IP address range:      20.0.0.0
26    - Destination IP address range: 68.142.68.0
27    - Source UDP port range:        1024
28    - Destination UDP port range:   1024 - 65535
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"192.168.0.0"
45         self.p1_src_end_ip = u"192.168.3.255"
46         self.p1_dst_start_ip = u"20.0.0.0"
47         self.p1_dst_end_ip = u"20.0.0.0"
48
49         self.p2_src_start_ip = u"20.0.0.0"
50         self.p2_src_end_ip = u"20.0.0.0"
51         self.p2_dst_start_ip = u"68.142.68.0"
52         self.p2_dst_end_ip = u"68.142.68.0"
53
54         # UDP ports used in packet headers.
55         self.p1_src_start_udp_port = 1024
56         self.p1_src_end_udp_port = 1086
57         self.p1_dst_start_udp_port = 1024
58         self.p1_dst_end_udp_port = 1024
59
60         self.p2_src_start_udp_port = 1024
61         self.p2_src_end_udp_port = 1024
62         self.p2_dst_start_udp_port = 1024
63         self.p2_dst_end_udp_port = 65535
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 = STLVM()
103         vm1.var(name="sIP",
104                 min_value=self.p1_src_start_ip,
105                 max_value=self.p1_src_end_ip,
106                 size=4,
107                 op="inc",
108                 next_var="sport")
109         vm1.var(name="sport",
110                 min_value=self.p1_src_start_udp_port,
111                 max_value=self.p1_src_end_udp_port,
112                 size=2,
113                 op="inc")
114         vm1.var(name="dIP",
115                 min_value=self.p1_dst_start_ip,
116                 max_value=self.p1_dst_end_ip,
117                 size=4,
118                 op="inc")
119         vm1.var(name="dport",
120                 min_value=self.p1_dst_start_udp_port,
121                 max_value=self.p1_dst_end_udp_port,
122                 size=2,
123                 op="inc")
124         vm1.write(fv_name="sIP", pkt_offset="IP.src")
125         vm1.write(fv_name="sport", pkt_offset="UDP.sport")
126         vm1.write(fv_name="dIP", pkt_offset="IP.dst")
127         vm1.write(fv_name="dport", pkt_offset="UDP.dport")
128         vm1.fix_chksum(offset='IP')
129         # Direction 0 --> 1
130         vm2 = STLVM()
131         vm2.var(name="sIP",
132                 min_value=self.p2_src_start_ip,
133                 max_value=self.p2_src_end_ip,
134                 size=4,
135                 op="inc",
136                 next_var="sport")
137         vm2.var(name="sport",
138                 min_value=self.p2_src_start_udp_port,
139                 max_value=self.p2_src_end_udp_port,
140                 size=2,
141                 op="inc")
142         vm2.var(name="dIP",
143                 min_value=self.p2_dst_start_ip,
144                 max_value=self.p2_dst_end_ip,
145                 size=4,
146                 op="inc",
147                 next_var="dport")
148         vm2.var(name="dport",
149                 min_value=self.p2_dst_start_udp_port,
150                 max_value=self.p2_dst_end_udp_port,
151                 size=2,
152                 op="inc")
153         vm2.write(fv_name="sIP", pkt_offset="IP.src")
154         vm2.write(fv_name="sport", pkt_offset="UDP.sport")
155         vm2.write(fv_name="dIP", pkt_offset="IP.dst")
156         vm2.write(fv_name="dport", pkt_offset="UDP.dport")
157         vm2.fix_chksum(offset='IP')
158
159         return base_pkt_a, base_pkt_b, vm1, vm2
160
161 def register():
162     """Register this traffic profile to T-rex.
163
164     Do not change this function.
165
166     :return: Traffic streams.
167     :rtype: Object
168     """
169     return TrafficStreams()