1 # Copyright (c) 2021 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.63.255
31 - Destination IP address range: 20.0.0.0 - 20.0.63.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 __init__(self, **kwargs):
50 """Initialization and setting of profile parameters."""
52 super(TrafficProfileBaseClass, self).__init__()
54 # IPs used in packet headers.
55 self.p1_src_start_ip = u"192.168.0.0"
56 self.p1_src_end_ip = u"192.168.63.255"
57 self.p1_dst_start_ip = u"20.0.0.0"
58 self.p1_dst_end_ip = u"20.0.63.255"
60 self.headers_size = 42 # 14B l2 + 20B ipv4 + 8B UDP
64 self.n_data = 32 # TODO: set via input parameter
65 self.m_delay = 2000000 # delay 2000s (2,000,000 ms)
66 self.u_delay = 1000 * self.m_delay # delay 2000s (2,000,000,000 us)
69 def define_profile(self):
70 """Define profile to be used by advanced stateful traffic generator.
72 This method MUST return:
74 return ip_gen, templates
76 :returns: IP generator and profile templates for ASTFProfile().
79 if self.framesize == 64:
80 self.udp_data += self._gen_padding(self.headers_size, 72)
81 if self.framesize == 1518:
82 self.udp_data += self._gen_padding(self.headers_size, 1514)
85 prog_c = ASTFProgram(stream=False)
86 prog_c.set_keepalive_msg(self.m_delay)
87 prog_c.send_msg(self.udp_data)
88 # No delay, PPS tests combine connect and data send (no data receive).
89 prog_c.set_var(u"var1", self.n_data)
90 prog_c.set_label(u"a:")
91 prog_c.send_msg(self.udp_data)
92 prog_c.jmp_nz(u"var1", u"a:")
93 # We should read the server response,
94 # but no reason to overload client workers even more.
97 prog_s = ASTFProgram(stream=False)
98 prog_s.set_keepalive_msg(self.m_delay)
99 # If server closes too soon, new instances are started
100 # leading in too much replies. To prevent that, we need to recv all.
101 prog_s.recv_msg(1 + self.n_data)
102 # In packet loss scenarios, some instances never get here.
103 # This maybe increases server traffic duration,
104 # but no other way if we want to avoid
105 # TRex creating a second instance of the same server.
106 prog_s.send_msg(self.udp_data)
107 prog_s.set_var(u"var2", self.n_data)
108 prog_s.set_label(u"b:")
109 prog_s.send_msg(self.udp_data)
110 prog_s.jmp_nz(u"var2", u"b:")
111 # VPP never duplicates packets,
112 # so it is safe to close the server instance now.
115 ip_gen_c = ASTFIPGenDist(
116 ip_range=[self.p1_src_start_ip, self.p1_src_end_ip],
119 ip_gen_s = ASTFIPGenDist(
120 ip_range=[self.p1_dst_start_ip, self.p1_dst_end_ip],
124 glob=ASTFIPGenGlobal(ip_offset=u"0.0.0.1"),
125 dist_client=ip_gen_c,
130 s_assoc = ASTFAssociation(rules=ASTFAssociationRule(port=8080))
133 temp_c = ASTFTCPClientTemplate(
139 temp_s = ASTFTCPServerTemplate(program=prog_s, assoc=s_assoc)
140 template = ASTFTemplate(client_template=temp_c, server_template=temp_s)
142 return ip_gen, template, None
146 """Register this traffic profile to T-Rex.
148 Do not change this function.
150 :return: Traffic Profiles.
153 return TrafficProfile()