1 # Copyright (c) 2022 Cisco and/or its affiliates.
3 # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
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:
10 # http://www.apache.org/licenses/LICENSE-2.0
11 # https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
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
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.
24 """Traffic profile for T-rex advanced stateful (astf) traffic generator.
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
30 - Source IP address range: 192.168.0.0 - 192.168.255.255
31 - Destination IP address range: 20.0.0.0 - 20.0.255.255
33 - Source IP address range: destination IP address from packet received
35 - Destination IP address range: source IP address from packet received
38 This is a profile for PPS tests, it combines UDP connect and data transfer.
39 No delays, server response waits for full request.
42 from trex.astf.api import *
43 from profile_trex_astf_base_class import TrafficProfileBaseClass
46 class TrafficProfile(TrafficProfileBaseClass):
47 """Traffic profile."""
49 def define_profile(self):
50 """Define profile to be used by advanced stateful traffic generator.
52 This method MUST return:
54 return ip_gen, templates, None
56 :returns: IP generator and profile templates for ASTFProfile().
59 # IPs used in packet headers.
60 p1_src_start_ip = u"192.168.0.0"
61 p1_src_end_ip = u"192.168.255.255"
62 p1_dst_start_ip = u"20.0.0.0"
63 p1_dst_end_ip = u"20.0.255.255"
65 # The difference between data size and frame size.
66 headers_size = 46 # 18B L2 + 20B IPv4 + 8B UDP.
68 # TODO: Use the "connection protocol" from CPS traffic profile?
69 # Currently, the first data packet is all DUT sees about the session.
71 # Avoid sending keepalives.
72 m_delay = 2000000 # delay 2000s (2,000,000 ms)
74 # Data, not padded yet.
76 # Pad the data to achieve the intended frame size.
77 udp_data += self._gen_padding(headers_size)
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}")
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.
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.
111 ip_gen_c = ASTFIPGenDist(
112 ip_range=[p1_src_start_ip, p1_src_end_ip],
115 ip_gen_s = ASTFIPGenDist(
116 ip_range=[p1_dst_start_ip, p1_dst_end_ip],
120 glob=ASTFIPGenGlobal(ip_offset=u"0.0.0.1"),
121 dist_client=ip_gen_c,
122 dist_server=ip_gen_s,
126 s_assoc = ASTFAssociation(rules=ASTFAssociationRule(port=8080))
129 temp_c = ASTFTCPClientTemplate(
132 limit=4128768, # TODO: Any benefit of making this configurable?
135 temp_s = ASTFTCPServerTemplate(program=prog_s, assoc=s_assoc)
136 template = ASTFTemplate(client_template=temp_c, server_template=temp_s)
138 return ip_gen, template, None
142 """Register this traffic profile to T-Rex.
144 Do not change this function.
146 :return: Traffic Profiles.
149 return TrafficProfile()