feat(astf): Support framesizes for ASTF
[csit.git] / GPL / traffic_profiles / trex / trex-astf-ethip4udp-262144h-pps.py
1 # Copyright (c) 2022 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
16 # Apache 2.
17 #
18 # Unless required by applicable law or agreed to in writing, software
19 # distributed under the License is distributed on an "AS IS" BASIS,
20 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 # See the License for the specific language governing permissions and
22 # limitations under the License.
23
24 """Traffic profile for T-rex advanced stateful (astf) traffic generator.
25 Traffic profile:
26  - Two streams sent in directions 0 --> 1 (client -> server, requests) and
27    1 --> 0 (server -> client, responses) at the same time.
28  - Packet: ETH / IP / UDP
29  - Direction 0 --> 1:
30    - Source IP address range:      172.16.0.0 - 172.19.255.255
31    - Destination IP address range: 20.16.0.0 - 20.19.255.255
32  - Direction 1 --> 0:
33    - Source IP address range:      destination IP address from packet received
34      on port 1
35    - Destination IP address range: source IP address from packet received
36      on port 1
37
38 This is a profile for PPS tests, it combines UDP connect and data transfer.
39 No delays, server response waits for full request.
40 """
41
42 from trex.astf.api import *
43 from profile_trex_astf_base_class import TrafficProfileBaseClass
44
45
46 class TrafficProfile(TrafficProfileBaseClass):
47     """Traffic profile."""
48
49     def define_profile(self):
50         """Define profile to be used by advanced stateful traffic generator.
51
52         This method MUST return:
53
54             return ip_gen, templates, None
55
56         :returns: IP generator and profile templates for ASTFProfile().
57         :rtype: tuple
58         """
59         # IPs used in packet headers.
60         p1_src_start_ip = u"172.16.0.0"
61         p1_src_end_ip = u"172.19.255.255"
62         p1_dst_start_ip = u"20.16.0.0"
63         p1_dst_end_ip = u"20.19.255.255"
64
65         # The difference between data size and frame size.
66         headers_size = 46  # 18B L2 + 20B IPv4 + 8B UDP.
67
68         # TODO: Use the "connection protocol" from CPS traffic profile?
69         # Currently, the first data packet is all DUT sees about the session.
70
71         # Avoid sending keepalives.
72         m_delay = 2000000  # delay 2000s (2,000,000 ms)
73
74         # Data, not padded yet.
75         udp_data = u""
76         # Pad the data to achieve the intended frame size.
77         udp_data += self._gen_padding(headers_size)
78
79         # Safety check, the current programs send at least 1 packet.
80         if self.n_data_frames < 1:
81             raise RuntimeError("n_data_frames < 1: {self.n_data_frames}")
82
83         # Client program.
84         prog_c = ASTFProgram(stream=False)
85         prog_c.set_keepalive_msg(m_delay)
86         prog_c.set_var(u"var1", self.n_data_frames)
87         prog_c.set_label(u"a:")
88         prog_c.send_msg(udp_data)
89         prog_c.jmp_nz(u"var1", u"a:")
90         # We should read the server response,
91         # but no reason to overload client workers even more.
92
93         # Server program.
94         prog_s = ASTFProgram(stream=False)
95         prog_s.set_keepalive_msg(m_delay)
96         # If server closes too soon, new instances are started
97         # leading in too much replies. To prevent that, we need to recv all.
98         prog_s.recv_msg(self.n_data_frames)
99         # In packet loss scenarios, some instances never get here.
100         # This maybe increases server traffic duration,
101         # but no other way if we want to avoid
102         # TRex creating a second instance of the same server.
103         prog_s.set_var(u"var2", self.n_data_frames)
104         prog_s.set_label(u"b:")
105         prog_s.send_msg(udp_data)
106         prog_s.jmp_nz(u"var2", u"b:")
107         # VPP never duplicates packets,
108         # so it is safe to close the server instance now.
109
110         # ip generators
111         ip_gen_c = ASTFIPGenDist(
112             ip_range=[p1_src_start_ip, p1_src_end_ip],
113             distribution=u"seq",
114         )
115         ip_gen_s = ASTFIPGenDist(
116             ip_range=[p1_dst_start_ip, p1_dst_end_ip],
117             distribution=u"seq",
118         )
119         ip_gen = ASTFIPGen(
120             glob=ASTFIPGenGlobal(ip_offset=u"0.0.0.1"),
121             dist_client=ip_gen_c,
122             dist_server=ip_gen_s,
123         )
124
125         # server association
126         s_assoc = ASTFAssociation(rules=ASTFAssociationRule(port=8080))
127
128         # template
129         temp_c = ASTFTCPClientTemplate(
130             program=prog_c,
131             ip_gen=ip_gen,
132             limit=16515072,  # TODO: Any benefit of making this configurable?
133             port=8080,
134         )
135         temp_s = ASTFTCPServerTemplate(program=prog_s, assoc=s_assoc)
136         template = ASTFTemplate(client_template=temp_c, server_template=temp_s)
137
138         return ip_gen, template, None
139
140
141 def register():
142     """Register this traffic profile to T-Rex.
143
144     Do not change this function.
145
146     :return: Traffic Profiles.
147     :rtype: Object
148     """
149     return TrafficProfile()