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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 """Traffic profile for T-rex advanced stateful (astf) traffic generator.
16 - Two streams sent in directions 0 --> 1 (client -> server, requests) and
17 1 --> 0 (server -> client, responses) at the same time.
18 - Packet: ETH / IP / UDP
20 - Source IP address range: 192.168.0.0 - 192.168.3.255
21 - Destination IP address range: 20.0.0.0 - 20.0.3.255
23 - Source IP address range: destination IP address from packet received
25 - Destination IP address range: source IP address from packet received
29 from trex.astf.api import *
30 from profile_trex_astf_base_class import TrafficProfileBaseClass
33 class TrafficProfile(TrafficProfileBaseClass):
34 """Traffic profile."""
36 def __init__(self, **kwargs):
37 """Initialization and setting of profile parameters."""
39 super(TrafficProfileBaseClass, self).__init__()
41 # IPs used in packet headers.
42 self.p1_src_start_ip = u"192.168.0.0"
43 self.p1_src_end_ip = u"192.168.3.255"
44 self.p1_dst_start_ip = u"20.0.0.0"
45 self.p1_dst_end_ip = u"20.0.3.255"
47 self.headers_size = 42 # 14B l2 + 20B ipv4 + 8B UDP
51 self.n_data = 32 # TODO: set via input parameter
52 self.m_delay = 1200000 # delay 1200s (1,200,000 ms)
53 self.u_delay = 1000 * self.m_delay # delay 1200s (1,200,000,000 us)
56 def define_profile(self):
57 """Define profile to be used by advanced stateful traffic generator.
59 This method MUST return:
61 return ip_gen, templates
63 :returns: IP generator and profile templates for ASTFProfile().
66 if self.framesize == 64:
67 self.udp_data += self._gen_padding(self.headers_size, 72)
68 if self.framesize == 1518:
69 self.udp_data += self._gen_padding(self.headers_size, 1514)
72 prog_c = ASTFProgram(stream=False)
73 prog_c.set_keepalive_msg(self.m_delay)
74 prog_c.send_msg(self.udp_data)
75 # No delay, PPS tests combine connect and data send (no data receive).
76 prog_c.set_var(u"var1", self.n_data)
77 prog_c.set_label(u"a:")
78 prog_c.send_msg(self.udp_data)
79 prog_c.jmp_nz(u"var1", u"a:")
80 # We should read the server response,
81 # but no reason to overload client workers even more.
84 prog_s = ASTFProgram(stream=False)
85 prog_s.set_keepalive_msg(self.m_delay)
86 # If server closes too soon, new instances are started
87 # leading in too much replies. To prevent that, we need to recv all.
88 prog_s.recv_msg(1 + self.n_data)
89 # In packet loss scenarios, some instances never get here.
90 # This maybe increases server traffic duration,
91 # but no other way if we want to avoid
92 # TRex creating a second instance of the same server.
93 prog_s.send_msg(self.udp_data)
94 prog_s.set_var(u"var2", self.n_data)
95 prog_s.set_label(u"b:")
96 prog_s.send_msg(self.udp_data)
97 prog_s.jmp_nz(u"var2", u"b:")
98 # VPP never duplicates packets,
99 # so it is safe to close the server instance now.
102 ip_gen_c = ASTFIPGenDist(
103 ip_range=[self.p1_src_start_ip, self.p1_src_end_ip],
106 ip_gen_s = ASTFIPGenDist(
107 ip_range=[self.p1_dst_start_ip, self.p1_dst_end_ip],
111 glob=ASTFIPGenGlobal(ip_offset=u"0.0.0.1"),
112 dist_client=ip_gen_c,
117 s_assoc = ASTFAssociation(rules=ASTFAssociationRule(port=8080))
120 temp_c = ASTFTCPClientTemplate(
126 temp_s = ASTFTCPServerTemplate(program=prog_s, assoc=s_assoc)
127 template = ASTFTemplate(client_template=temp_c, server_template=temp_s)
129 return ip_gen, template, None
133 """Register this traffic profile to T-Rex.
135 Do not change this function.
137 :return: Traffic Profiles.
140 return TrafficProfile()