Support existing test types with ASTF
[csit.git] / GPL / traffic_profiles / trex / trex-astf-ethip4udp-65536h-pps.py
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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """Traffic profile for T-rex advanced stateful (astf) traffic generator.
15 Traffic profile:
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
19  - Direction 0 --> 1:
20    - Source IP address range:      192.168.0.0 - 192.168.255.255
21    - Destination IP address range: 20.0.0.0 - 20.0.255.255
22  - Direction 1 --> 0:
23    - Source IP address range:      destination IP address from packet received
24      on port 1
25    - Destination IP address range: source IP address from packet received
26      on port 1
27 """
28
29 from trex.astf.api import *
30 from profile_trex_astf_base_class import TrafficProfileBaseClass
31
32
33 class TrafficProfile(TrafficProfileBaseClass):
34     """Traffic profile."""
35
36     def __init__(self, **kwargs):
37         """Initialization and setting of profile parameters."""
38
39         super(TrafficProfileBaseClass, self).__init__()
40
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.255.255"
44         self.p1_dst_start_ip = u"20.0.0.0"
45         self.p1_dst_end_ip = u"20.0.255.255"
46
47         self.headers_size = 42  # 14B l2 + 20B ipv4 + 8B UDP
48
49         self.udp_data = u""
50
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)
54         self.limit = 4128768
55
56     def define_profile(self):
57         """Define profile to be used by advanced stateful traffic generator.
58
59         This method MUST return:
60
61             return ip_gen, templates
62
63         :returns: IP generator and profile templates for ASTFProfile().
64         :rtype: tuple
65         """
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)
70
71         # Client program.
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.
82
83         # Server program.
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.
100
101         # ip generators
102         ip_gen_c = ASTFIPGenDist(
103             ip_range=[self.p1_src_start_ip, self.p1_src_end_ip],
104             distribution=u"seq"
105         )
106         ip_gen_s = ASTFIPGenDist(
107             ip_range=[self.p1_dst_start_ip, self.p1_dst_end_ip],
108             distribution=u"seq"
109         )
110         ip_gen = ASTFIPGen(
111             glob=ASTFIPGenGlobal(ip_offset=u"0.0.0.1"),
112             dist_client=ip_gen_c,
113             dist_server=ip_gen_s
114         )
115
116         # server association
117         s_assoc = ASTFAssociation(rules=ASTFAssociationRule(port=8080))
118
119         # template
120         temp_c = ASTFTCPClientTemplate(
121             program=prog_c,
122             ip_gen=ip_gen,
123             limit=self.limit,
124             port=8080
125         )
126         temp_s = ASTFTCPServerTemplate(program=prog_s, assoc=s_assoc)
127         template = ASTFTemplate(client_template=temp_c, server_template=temp_s)
128
129         return ip_gen, template, None
130
131
132 def register():
133     """Register this traffic profile to T-Rex.
134
135     Do not change this function.
136
137     :return: Traffic Profiles.
138     :rtype: Object
139     """
140     return TrafficProfile()