Core: T-rex 2.97
[csit.git] / GPL / traffic_profiles / trex / profile_trex_astf_base_class.py
1 # Copyright (c) 2022 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     # TODO: Declare and document fields in a contructor to make pylint happier.
50     # TODO: Consider passing the values to define_profile(),
51     #       instead of keeping (and documenting) them as instance fields here.
52
53     @property
54     def pcap_dir(self):
55         """Pcap file directory.
56
57         If needed, implement your own algorithm.
58
59         :returns: Pcap file directory.
60         :rtype: str
61         """
62         return self._pcap_dir
63
64     def _gen_padding(self, current_length, required_length=0):
65         """Generate padding.
66
67         If needed, implement your own algorithm.
68
69         :param current_length: Current length of the packet.
70         :param required_length: Required length of the packet. If set to 0 then
71             self.framesize value is used.
72         :type current_length: int
73         :type required_length: int
74         :returns: The generated padding.
75         :rtype: str
76         """
77         # TODO: Add support for IMIX frame size;
78         #  use random.randrange(0, len(self.STREAM_TABLE[self.framesize])) ?
79         if not required_length:
80             required_length = self.framesize
81         missing = required_length - current_length
82         if missing < 0:
83             msg = f"Cannot to pad from {current_length} to {required_length}."
84             raise RuntimeError(msg)
85         padding = u"".join(choices(ascii_letters, k=missing))
86         return padding
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, kwargs
94
95             templates or kwargs CAN be None.
96             Kwargs can be used to define PCAP file, set MSS, ...
97
98         :returns: IP generator and profile templates or list of pcap files for
99             traffic generator.
100         :rtype: tuple
101         """
102         raise NotImplementedError
103
104     def create_profile(self):
105         """Create traffic profile.
106
107         Implement your own traffic profiles.
108
109         :returns: Traffic profile.
110         :rtype: trex.astf.trex_astf_profile.ASTFProfile
111         """
112         ip_gen, templates, kwargs = self.define_profile()
113         if kwargs is None:
114             kwargs = dict()
115
116         profile = ASTFProfile(
117             default_ip_gen=ip_gen,
118             templates=templates,
119             **kwargs
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         :type kwargs: dict
132         :returns: Traffic profile.
133         :rtype: trex.astf.trex_astf_profile.ASTFProfile
134         """
135         self.framesize = kwargs[u"framesize"]
136         self.n_data_frames = kwargs[u"n_data_frames"]
137         self._pcap_dir = kwargs.get(
138             u"pcap_dir", u"/opt/trex-core-2.97/scripts/avl"
139         )
140
141         return self.create_profile()