427060ffeeef75232b0a6e9492dcdd179dbca74e
[csit.git] / GPL / traffic_profiles / trex / profile_trex_astf_base_class.py
1 # Copyright (c) 2021 Cisco and/or its affiliates.
2 #
3 # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
4 #
5 # Licensed under the Apache License 2.0 or
6 # GNU General Public License v2.0 or later;  you may not use this file
7 # except in compliance with one of these Licenses. You
8 # may obtain a copy of the Licenses at:
9 #
10 #     http://www.apache.org/licenses/LICENSE-2.0
11 #     https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 #
13 # Note: If this file is linked with Scapy, which is GPLv2+, your use of it
14 # must be under GPLv2+.  If at any point in the future it is no longer linked
15 # with Scapy (or other GPLv2+ licensed software), you are free to choose
16 # Apache 2.
17 #
18 # Unless required by applicable law or agreed to in writing, software
19 # distributed under the License is distributed on an "AS IS" BASIS,
20 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 # See the License for the specific language governing permissions and
22 # limitations under the License.
23
24 """Base class for profiles for T-rex advanced stateful (astf) traffic generator.
25 """
26
27 from random import choices
28 from string import ascii_letters
29
30 from trex.astf.api import *
31
32
33 class TrafficProfileBaseClass:
34     """Base class for profiles for T-rex astf traffic generator."""
35
36     STREAM_TABLE = {
37         u"IMIX_v4": [
38             {u"size": 60, u"pps": 28, u"isg": 0},
39             {u"size": 590, u"pps": 20, u"isg": 0.1},
40             {u"size": 1514, u"pps": 4, u"isg": 0.2}
41         ],
42         'IMIX_v4_1': [
43             {u"size": 64, u"pps": 28, u"isg": 0},
44             {u"size": 570, u"pps": 16, u"isg": 0.1},
45             {u"size": 1518, u"pps": 4, u"isg": 0.2}
46         ]
47     }
48
49     def __init__(self):
50         # Default values of required parameters; can be overwritten in
51         # "get_profile" method.
52         self.framesize = 64
53         self._pcap_dir = u""
54
55         # If needed, add your own parameters.
56
57     @property
58     def pcap_dir(self):
59         """Pcap file directory.
60
61         If needed, implement your own algorithm.
62
63         :returns: Pcap file directory.
64         :rtype: str
65         """
66         return self._pcap_dir
67
68     def _gen_padding(self, current_length, required_length=0):
69         """Generate padding.
70
71         If needed, implement your own algorithm.
72
73         :param current_length: Current length of the packet.
74         :param required_length: Required length of the packet. If set to 0 then
75         self.framesize value is used.
76         :type current_length: int
77         :type required_length: int
78         :returns: The generated padding.
79         :rtype: str
80         """
81         # TODO: Add support for IMIX frame size;
82         #  use random.randrange(0, len(self.STREAM_TABLE[self.framesize])) ?
83         if not required_length:
84             required_length = self.framesize
85
86         return str(choices(ascii_letters, k=required_length - current_length))
87
88     def define_profile(self):
89         """Define profile to be used by T-Rex astf traffic generator.
90
91         This method MUST return:
92
93             return ip_gen, templates, cap_list
94
95             templates or cap_list CAN be None.
96
97         :returns: IP generator and profile templates or list of pcap files for
98         traffic generator.
99         :rtype: tuple
100         """
101         raise NotImplementedError
102
103     def create_profile(self):
104         """Create traffic profile.
105
106         Implement your own traffic profiles.
107
108         :returns: Traffic profile.
109         :rtype: trex.astf.trex_astf_profile.ASTFProfile
110         """
111         ip_gen, templates, cap_list = self.define_profile()
112
113         # In most cases you will not have to change the code below:
114
115         # profile
116         profile = ASTFProfile(
117             default_ip_gen=ip_gen,
118             templates=templates,
119             cap_list=cap_list
120         )
121
122         return profile
123
124     def get_profile(self, **kwargs):
125         """Get traffic profile created by "create_profile" method.
126
127         If needed, add your own parameters.
128
129         :param kwargs: Key-value pairs used by "create_profile" method while
130         creating the profile.
131         :returns: Traffic profile.
132         :rtype: trex.astf.trex_astf_profile.ASTFProfile
133         """
134         self.framesize = kwargs[u"framesize"]
135         self._pcap_dir = kwargs.get(
136             u"pcap_dir",u"/opt/trex-core-2.86/scripts/avl"
137         )
138
139         return self.create_profile()