TRex ASTF onboarding Part I
[csit.git] / resources / traffic_profiles / trex / trex-sf-2n-ethip4tcphttp-1u1s-nginx-cps.py
1 # Copyright (c) 2018 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 """ASTF profile for TRex traffic generator.
15
16 ASTF profile:
17  - Client side traffic in directions 0 --> 1.
18  - Server side traffic disabled.
19  - Packet: ETH / IP / TCP / HTTP1.1
20  - Direction 0 --> 1:
21    - Source IP address range:      172.16.130.2 - 172.16.130.2
22    - Destination IP address range: 192.168.0.1
23 """
24
25 from trex_astf_lib.api import *
26
27
28 class Prof1():
29     def __init__(self):
30         """Initialization and setting of streams' parameters."""
31
32         # Response content length.
33         self.content_len = 0
34         # Number of requests per HTTP transaction.
35         self.requests = 1
36         # Number of transactions per HTTP connection.
37         self.transaction_per_conn = 1
38         # Use TCP RST instead of FIN+ACK.
39         self.tcp_reset = False
40
41         # IP used in packet headers.
42         self.p1_src_start_ip = '172.16.130.2'
43         self.p1_src_end_ip = '172.16.130.2'
44         self.p1_dst_start_ip = '192.168.0.1'
45         self.p1_dst_end_ip = '192.168.0.1'
46
47         self.http_req = (b'GET /0KB.bin HTTP/1.1\r\n'
48                           'Host: {host}\r\n'
49                           'User-Agent: trex/astf\r\n'
50                           'Accept: */*\r\n'
51                           'Connection: keep-alive\r\n\r\n'
52                           .format(host=self.p1_dst_start_ip))
53         self.http_res = (b'HTTP/1.1 200 OK\r\n'
54                           'Server: nginx/1.13.7\r\n'
55                           'Date: Mon, 01 Jan 2018 00:00:00 GMT\r\n'
56                           'Content-Type: application/octet-stream\r\n'
57                           'Content-Length: {length}\r\n'
58                           'Last-Modified: Mon, 01 Jan 2018 00:00:00 GMT\r\n'
59                           'Connection: keep-alive\r\n'
60                           'ETag: "5a027c14-0"\r\n'
61                           'Accept-Ranges: bytes\r\n\r\n'
62                           .format(length=self.content_len))
63
64     def create_profile(self):
65         # client operations
66         prog_c = ASTFProgram()
67         prog_c.connect()
68         for i in range(self.transaction_per_conn):
69             prog_c.send(self.http_req * self.requests)
70             prog_c.recv((len(self.http_res) + self.content_len) * self.requests)
71         if self.tcp_reset:
72             prog_c.reset()
73
74         # ip generator
75         ip_gen_c = ASTFIPGenDist(ip_range=[self.p1_src_start_ip,
76                                            self.p1_src_end_ip],
77                                  distribution="seq")
78         ip_gen_s = ASTFIPGenDist(ip_range=[self.p1_dst_start_ip,
79                                            self.p1_dst_end_ip],
80                                  distribution="seq")
81         ip_gen = ASTFIPGen(glob=ASTFIPGenGlobal(ip_offset="0.0.0.1"),
82                            dist_client=ip_gen_c,
83                            dist_server=ip_gen_s)
84
85         # TCP parameters
86         tcp_params = ASTFTCPInfo(window=32768)
87         # client tunables
88         c_glob_info = ASTFGlobalInfo()
89
90         # template
91         client_template = ASTFTCPClientTemplate(program=prog_c,
92                                                 tcp_info=tcp_params,
93                                                 ip_gen=ip_gen)
94         server_template = ASTFTCPServerTemplate(program=ASTFProgram(),
95                                                 tcp_info=tcp_params)
96         template = ASTFTemplate(client_template=client_template,
97                                 server_template=server_template)
98
99         # profile
100         return ASTFProfile(default_ip_gen=ip_gen, templates=template,
101                            default_c_glob_info=c_glob_info)
102
103     def get_profile(self):
104         return self.create_profile()
105
106
107 def register():
108     return Prof1()
109