7ec61dc2e51845bd4ee47abe2b5a1581aca816d2
[csit.git] / GPL / traffic_profiles / trex / trex-astf-ethip4udp-262144h-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:      172.16.0.0 - 172.19.255.255
21    - Destination IP address range: 20.16.0.0 - 20.19.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 This is a profile for PPS tests, it combines UDP connect and data transfer.
29 No delays, server response waits for full request.
30 """
31
32 from trex.astf.api import *
33 from profile_trex_astf_base_class import TrafficProfileBaseClass
34
35
36 class TrafficProfile(TrafficProfileBaseClass):
37     """Traffic profile."""
38
39     def __init__(self, **kwargs):
40         """Initialization and setting of profile parameters."""
41
42         super(TrafficProfileBaseClass, self).__init__()
43
44         # IPs used in packet headers.
45         self.p1_src_start_ip = u"172.16.0.0"
46         self.p1_src_end_ip = u"172.19.255.255"
47         self.p1_dst_start_ip = u"20.16.0.0"
48         self.p1_dst_end_ip = u"20.19.255.255"
49
50         self.headers_size = 42  # 14B l2 + 20B ipv4 + 8B UDP
51
52         self.udp_data = u""
53
54         self.n_data = 32  # TODO: set via input parameter
55         self.m_delay = 1200000  # delay 1200s (1,200,000 ms)
56         self.u_delay = 1000 * self.m_delay  # delay 1200s (1,200,000,000 us)
57         self.limit = 16515072
58
59     def define_profile(self):
60         """Define profile to be used by advanced stateful traffic generator.
61
62         This method MUST return:
63
64             return ip_gen, templates
65
66         :returns: IP generator and profile templates for ASTFProfile().
67         :rtype: tuple
68         """
69         if self.framesize == 64:
70             self.udp_data += self._gen_padding(self.headers_size, 72)
71         if self.framesize == 1518:
72             self.udp_data += self._gen_padding(self.headers_size, 1514)
73
74         # Client program.
75         prog_c = ASTFProgram(stream=False)
76         prog_c.set_keepalive_msg(self.m_delay)
77         prog_c.send_msg(self.udp_data)
78         # No delay, PPS tests combine connect and data send (no data receive).
79         prog_c.set_var(u"var1", self.n_data)
80         prog_c.set_label(u"a:")
81         prog_c.send_msg(self.udp_data)
82         prog_c.jmp_nz(u"var1", u"a:")
83         # We should read the server response,
84         # but no reason to overload client workers even more.
85
86         # Server program.
87         prog_s = ASTFProgram(stream=False)
88         prog_s.set_keepalive_msg(self.m_delay)
89         # If server closes too soon, new instances are started
90         # leading in too much replies. To prevent that, we need to recv all.
91         prog_s.recv_msg(1 + self.n_data)
92         # In packet loss scenarios, some instances never get here.
93         # This maybe increases server traffic duration,
94         # but no other way if we want to avoid
95         # TRex creating a second instance of the same server.
96         prog_s.send_msg(self.udp_data)
97         prog_s.set_var(u"var2", self.n_data)
98         prog_s.set_label(u"b:")
99         prog_s.send_msg(self.udp_data)
100         prog_s.jmp_nz(u"var2", u"b:")
101         # VPP never duplicates packets,
102         # so it is safe to close the server instance now.
103
104         # ip generators
105         ip_gen_c = ASTFIPGenDist(
106             ip_range=[self.p1_src_start_ip, self.p1_src_end_ip],
107             distribution=u"seq"
108         )
109         ip_gen_s = ASTFIPGenDist(
110             ip_range=[self.p1_dst_start_ip, self.p1_dst_end_ip],
111             distribution=u"seq"
112         )
113         ip_gen = ASTFIPGen(
114             glob=ASTFIPGenGlobal(ip_offset=u"0.0.0.1"),
115             dist_client=ip_gen_c,
116             dist_server=ip_gen_s
117         )
118
119         # server association
120         s_assoc = ASTFAssociation(rules=ASTFAssociationRule(port=8080))
121
122         # template
123         temp_c = ASTFTCPClientTemplate(
124             program=prog_c,
125             ip_gen=ip_gen,
126             limit=self.limit,
127             port=8080
128         )
129         temp_s = ASTFTCPServerTemplate(program=prog_s, assoc=s_assoc)
130         template = ASTFTemplate(client_template=temp_c, server_template=temp_s)
131
132         return ip_gen, template, None
133
134
135 def register():
136     """Register this traffic profile to T-Rex.
137
138     Do not change this function.
139
140     :return: Traffic Profiles.
141     :rtype: Object
142     """
143     return TrafficProfile()