5349744d3849fccee31b5b89f0e3ea8a917f787d
[csit.git] / GPL / traffic_profiles / trex / trex-astf-ethip4udp-16384h-pps.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 """Traffic profile for T-rex advanced stateful (astf) traffic generator.
24 Traffic profile:
25  - Two streams sent in directions 0 --> 1 (client -> server, requests) and
26    1 --> 0 (server -> client, responses) at the same time.
27  - Packet: ETH / IP / UDP
28  - Direction 0 --> 1:
29    - Source IP address range:      192.168.0.0 - 192.168.63.255
30    - Destination IP address range: 20.0.0.0 - 20.0.63.255
31  - Direction 1 --> 0:
32    - Source IP address range:      destination IP address from packet received
33      on port 1
34    - Destination IP address range: source IP address from packet received
35      on port 1
36
37 This is a profile for PPS tests, it combines UDP connect and data transfer.
38 No delays, server response waits for full request.
39 """
40
41 from trex.astf.api import *
42 from profile_trex_astf_base_class import TrafficProfileBaseClass
43
44
45 class TrafficProfile(TrafficProfileBaseClass):
46     """Traffic profile."""
47
48     def __init__(self, **kwargs):
49         """Initialization and setting of profile parameters."""
50
51         super(TrafficProfileBaseClass, self).__init__()
52
53         # IPs used in packet headers.
54         self.p1_src_start_ip = u"192.168.0.0"
55         self.p1_src_end_ip = u"192.168.63.255"
56         self.p1_dst_start_ip = u"20.0.0.0"
57         self.p1_dst_end_ip = u"20.0.63.255"
58
59         self.headers_size = 42  # 14B l2 + 20B ipv4 + 8B UDP
60
61         self.udp_data = u""
62
63         self.n_data = 32  # TODO: set via input parameter
64         self.m_delay = 1200000  # delay 1200s (1,200,000 ms)
65         self.u_delay = 1000 * self.m_delay  # delay 1200s (1,200,000,000 us)
66         self.limit = 1032192
67
68     def define_profile(self):
69         """Define profile to be used by advanced stateful traffic generator.
70
71         This method MUST return:
72
73             return ip_gen, templates
74
75         :returns: IP generator and profile templates for ASTFProfile().
76         :rtype: tuple
77         """
78         if self.framesize == 64:
79             self.udp_data += self._gen_padding(self.headers_size, 72)
80         if self.framesize == 1518:
81             self.udp_data += self._gen_padding(self.headers_size, 1514)
82
83         # Client program.
84         prog_c = ASTFProgram(stream=False)
85         prog_c.set_keepalive_msg(self.m_delay)
86         prog_c.send_msg(self.udp_data)
87         # No delay, PPS tests combine connect and data send (no data receive).
88         prog_c.set_var(u"var1", self.n_data)
89         prog_c.set_label(u"a:")
90         prog_c.send_msg(self.udp_data)
91         prog_c.jmp_nz(u"var1", u"a:")
92         # We should read the server response,
93         # but no reason to overload client workers even more.
94
95         # Server program.
96         prog_s = ASTFProgram(stream=False)
97         prog_s.set_keepalive_msg(self.m_delay)
98         # If server closes too soon, new instances are started
99         # leading in too much replies. To prevent that, we need to recv all.
100         prog_s.recv_msg(1 + self.n_data)
101         # In packet loss scenarios, some instances never get here.
102         # This maybe increases server traffic duration,
103         # but no other way if we want to avoid
104         # TRex creating a second instance of the same server.
105         prog_s.send_msg(self.udp_data)
106         prog_s.set_var(u"var2", self.n_data)
107         prog_s.set_label(u"b:")
108         prog_s.send_msg(self.udp_data)
109         prog_s.jmp_nz(u"var2", u"b:")
110         # VPP never duplicates packets,
111         # so it is safe to close the server instance now.
112
113         # ip generators
114         ip_gen_c = ASTFIPGenDist(
115             ip_range=[self.p1_src_start_ip, self.p1_src_end_ip],
116             distribution=u"seq"
117         )
118         ip_gen_s = ASTFIPGenDist(
119             ip_range=[self.p1_dst_start_ip, self.p1_dst_end_ip],
120             distribution=u"seq"
121         )
122         ip_gen = ASTFIPGen(
123             glob=ASTFIPGenGlobal(ip_offset=u"0.0.0.1"),
124             dist_client=ip_gen_c,
125             dist_server=ip_gen_s
126         )
127
128         # server association
129         s_assoc = ASTFAssociation(rules=ASTFAssociationRule(port=8080))
130
131         # template
132         temp_c = ASTFTCPClientTemplate(
133             program=prog_c,
134             ip_gen=ip_gen,
135             limit=self.limit,
136             port=8080
137         )
138         temp_s = ASTFTCPServerTemplate(program=prog_s, assoc=s_assoc)
139         template = ASTFTemplate(client_template=temp_c, server_template=temp_s)
140
141         return ip_gen, template, None
142
143
144 def register():
145     """Register this traffic profile to T-Rex.
146
147     Do not change this function.
148
149     :return: Traffic Profiles.
150     :rtype: Object
151     """
152     return TrafficProfile()