cd81cb96eeaf3eed617fa9ee9609362427ee7db1
[csit.git] / GPL / traffic_profiles / trex / trex-astf-ethip4udp-4096h.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
16 Traffic profile:
17  - Two streams sent in directions 0 --> 1 (client -> server, requests) and
18    1 --> 0 (server -> client, responses) at the same time.
19  - Packet: ETH / IP / UDP
20  - Direction 0 --> 1:
21    - Source IP address range:      192.168.0.0 - 192.168.15.255
22    - Destination IP address range: 20.0.0.0 - 20.0.15.255
23  - Direction 1 --> 0:
24    - Source IP address range:      destination IP address from packet received
25      on port 1
26    - Destination IP address range: source IP address from packet received
27      on port 1
28
29 This is a profile for CPS tests, it only sets up UDP session.
30 No delays, no data transfer.
31 Keepalive mechanism cannot be disabled, so it is at least set to long waits.
32 """
33
34 from trex.astf.api import *
35
36 from profile_trex_astf_base_class import TrafficProfileBaseClass
37
38
39 class TrafficProfile(TrafficProfileBaseClass):
40     """Traffic profile."""
41
42     def __init__(self):
43         """Initialization and setting of profile parameters."""
44
45         super(TrafficProfileBaseClass, self).__init__()
46
47         # IPs used in packet headers.
48         self.p1_src_start_ip = u"192.168.0.0"
49         self.p1_src_end_ip = u"192.168.15.255"
50         self.p1_dst_start_ip = u"20.0.0.0"
51         self.p1_dst_end_ip = u"20.0.15.255"
52
53         # UDP messages
54         self.udp_req = u"GET"
55         self.udp_res = u"ACK"
56
57         # Headers length
58         self.headers_size = 42  # 14B l2 + 20B ipv4 + 8B udp
59
60         # No need to set keepalive, both programs end just after start&send.
61
62     def define_profile(self):
63         """Define profile to be used by advanced stateful traffic generator.
64
65         This method MUST return:
66             return ip_gen, templates, None
67
68         :returns: IP generator and profile templates ASTFProfile().
69         :rtype: tuple
70         """
71         self.udp_req += self._gen_padding(self.headers_size + len(self.udp_req))
72         self.udp_res += self._gen_padding(self.headers_size + len(self.udp_res))
73
74         # client commands
75         prog_c = ASTFProgram(stream=False)
76         # send REQ message
77         prog_c.send_msg(self.udp_req)
78         # No need to process the response, seeing L2 counter is enough.
79         # Client program can end here.
80
81         # server commands
82         prog_s = ASTFProgram(stream=False)
83         # When server instance is created means REQ is visible in L2 counter.
84         # No need to receive explicitly?
85         # send RES message
86         prog_s.send_msg(self.udp_res)
87         # Server program can end here.
88
89         # ip generators
90         ip_gen_c = ASTFIPGenDist(
91             ip_range=[self.p1_src_start_ip, self.p1_src_end_ip],
92             distribution=u"seq"
93         )
94         ip_gen_s = ASTFIPGenDist(
95             ip_range=[self.p1_dst_start_ip, self.p1_dst_end_ip],
96             distribution=u"seq"
97         )
98         ip_gen = ASTFIPGen(
99             glob=ASTFIPGenGlobal(ip_offset=u"0.0.0.1"),
100             dist_client=ip_gen_c,
101             dist_server=ip_gen_s
102         )
103
104         # server association
105         s_assoc = ASTFAssociation(rules=ASTFAssociationRule(port=8080))
106
107         # template
108         temp_c = ASTFTCPClientTemplate(
109             program=prog_c,
110             ip_gen=ip_gen,
111             limit=258048,  # TODO: set via input parameter ?
112             port=8080
113         )
114         temp_s = ASTFTCPServerTemplate(program=prog_s, assoc=s_assoc)
115         template = ASTFTemplate(client_template=temp_c, server_template=temp_s)
116
117         return ip_gen, template, None
118
119
120 def register():
121     """Register this traffic profile to T-Rex.
122
123     Do not change this function.
124
125     :return: Traffic Profiles.
126     :rtype: Object
127     """
128     return TrafficProfile()