f0d553d7c466a8630517c5c89eadb66b0fcb93b5
[csit.git] / GPL / traffic_profiles / trex / trex-stl-ethip4udp-4096u63p-udir.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 """Stream profile for T-rex traffic generator.
15
16 Stream profile:
17  - One stream sent in directions 0 --> 1.
18  - Packet: ETH / IP / UDP
19  - Direction 0 --> 1:
20    - Source IP address range:      192.168.0.0 - 192.168.15.255
21    - Destination IP address range: 20.0.0.0 - 20.0.15.255
22    - Source UDP port range:        1024 - 1086
23    - Destination UDP port range:   8080
24 """
25
26 from trex.stl.api import *
27 from profile_trex_stateless_base_class import TrafficStreamsBaseClass
28
29
30 class TrafficStreams(TrafficStreamsBaseClass):
31     """Stream profile."""
32
33     def __init__(self):
34         """Initialization and setting of streams' parameters."""
35
36         super(TrafficStreamsBaseClass, self).__init__()
37
38         # IPs used in packet headers.
39         self.p1_src_start_ip = u"192.168.0.0"
40         self.p1_src_end_ip = u"192.168.15.255"
41         self.p1_dst_start_ip = u"20.0.0.0"
42         self.p1_dst_end_ip = u"20.0.15.255"
43
44         # UDP ports used in packet headers.
45         self.p1_src_start_udp_port = 1024
46         self.p1_src_end_udp_port = 1086
47         self.p1_dst_start_udp_port = 8080
48         self.p1_dst_end_udp_port = 8080
49
50     def define_packets(self):
51         """Defines the packets to be sent from the traffic generator.
52
53         Packet definition: | ETH | IP | UDP |
54
55         :returns: Packets to be sent from the traffic generator.
56         :rtype: tuple
57         """
58
59         # Direction 0 --> 1
60         base_pkt_a = (
61             Ether() /
62             IP(
63                 src=self.p1_src_start_ip,
64                 dst=self.p1_dst_start_ip,
65                 proto=17
66             ) /
67             UDP(
68                 sport=self.p1_src_start_udp_port,
69                 dport=self.p1_dst_start_udp_port
70             )
71         )
72
73         base_pkt_b = (
74             Ether()
75         )
76
77         # Direction 0 --> 1
78         vm1 = STLVM()
79         vm1.var(name="sIP",
80                 min_value=self.p1_src_start_ip,
81                 max_value=self.p1_src_end_ip,
82                 size=4,
83                 op="inc",
84                 next_var="sport")
85         vm1.var(name="sport",
86                 min_value=self.p1_src_start_udp_port,
87                 max_value=self.p1_src_end_udp_port,
88                 size=2,
89                 op="inc")
90         vm1.var(name="dIP",
91                 min_value=self.p1_dst_start_ip,
92                 max_value=self.p1_dst_end_ip,
93                 size=4,
94                 op="inc")
95         vm1.var(name="dport",
96                 min_value=self.p1_dst_start_udp_port,
97                 max_value=self.p1_dst_end_udp_port,
98                 size=2,
99                 op="inc")
100         vm1.write(fv_name="sIP", pkt_offset="IP.src")
101         vm1.write(fv_name="sport", pkt_offset="UDP.sport")
102         vm1.write(fv_name="dIP", pkt_offset="IP.dst")
103         vm1.write(fv_name="dport", pkt_offset="UDP.dport")
104         vm1.fix_chksum(offset='IP')
105
106         vm2 = STLVM()
107
108         return base_pkt_a, base_pkt_b, vm1, vm2
109
110 def register():
111     """Register this traffic profile to T-rex.
112
113     Do not change this function.
114
115     :return: Traffic streams.
116     :rtype: Object
117     """
118     return TrafficStreams()