d52d293c926f75be08d31de09e5cb35d96619534
[csit.git] / GPL / traffic_profiles / trex / trex-astf-ethip4tcp-262144h.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 / TCP
20  - Direction 0 --> 1:
21    - Source IP address range:      172.16.0.0 - 172.19.255.255
22    - Destination IP address range: 20.16.0.0 - 20.19.255.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 and tears down TCP session.
30 No delays, no data transfer.
31 """
32
33 from trex.astf.api import *
34 from profile_trex_astf_base_class import TrafficProfileBaseClass
35
36
37 class TrafficProfile(TrafficProfileBaseClass):
38     """Traffic profile."""
39
40     def __init__(self):
41         """Initialization and setting of profile parameters."""
42
43         super(TrafficProfileBaseClass, self).__init__()
44
45         # IPs used in packet headers.
46         self.p1_src_start_ip = u"172.16.0.0"
47         self.p1_src_end_ip = u"172.19.255.255"
48         self.p1_dst_start_ip = u"20.16.0.0"
49         self.p1_dst_end_ip = u"20.19.255.255"
50
51         # Headers length; not used in this profile, just for the record of
52         # header length for TCP packet with 0B payload
53         self.headers_size = 58  # 14B l2 + 20B ipv4 + 24B tcp incl. 4B options
54
55     def define_profile(self):
56         """Define profile to be used by advanced stateful traffic generator.
57
58         This method MUST return:
59
60             return ip_gen, templates, None
61
62         :returns: IP generator and profile templates for ASTFProfile().
63         :rtype: tuple
64         """
65         # client commands
66         prog_c = ASTFProgram()
67         # send syn
68         prog_c.connect()
69         # receive syn-ack (0B sent in tcp syn-ack packet) and send ack
70         prog_c.recv(0)
71
72         # server commands
73         prog_s = ASTFProgram()
74         # receive syn, send syn-ack
75         prog_s.accept()
76         # receive fin-ack, send ack + fin-ack
77         prog_s.wait_for_peer_close()
78
79         # ip generators
80         ip_gen_c = ASTFIPGenDist(
81             ip_range=[self.p1_src_start_ip, self.p1_src_end_ip],
82             distribution=u"seq"
83         )
84         ip_gen_s = ASTFIPGenDist(
85             ip_range=[self.p1_dst_start_ip, self.p1_dst_end_ip],
86             distribution=u"seq"
87         )
88         ip_gen = ASTFIPGen(
89             glob=ASTFIPGenGlobal(ip_offset=u"0.0.0.1"),
90             dist_client=ip_gen_c,
91             dist_server=ip_gen_s
92         )
93
94         # server association
95         s_assoc = ASTFAssociation(rules=ASTFAssociationRule(port=8080))
96
97         # template
98         temp_c = ASTFTCPClientTemplate(
99             program=prog_c,
100             ip_gen=ip_gen,
101             limit=16515072,  # TODO: set via input parameter
102             port=8080
103         )
104         temp_s = ASTFTCPServerTemplate(program=prog_s, assoc=s_assoc)
105         template = ASTFTemplate(client_template=temp_c, server_template=temp_s)
106
107         return ip_gen, template, None
108
109
110 def register():
111     """Register this traffic profile to T-Rex.
112
113     Do not change this function.
114
115     :return: Traffic Profiles.
116     :rtype: Object
117     """
118     return TrafficProfile()