1 # Copyright (c) 2022 Cisco and/or its affiliates.
3 # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
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:
10 # http://www.apache.org/licenses/LICENSE-2.0
11 # https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
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
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.
24 """Base class for profiles for T-rex advanced stateful (astf) traffic generator.
27 from random import choices
28 from string import ascii_letters
30 from trex.astf.api import *
33 class TrafficProfileBaseClass:
34 """Base class for profiles for T-rex astf traffic generator."""
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}
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}
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.
55 """Pcap file directory.
57 If needed, implement your own algorithm.
59 :returns: Pcap file directory.
64 def _gen_padding(self, current_length, required_length=0):
67 If needed, implement your own algorithm.
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.
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
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))
88 def define_profile(self):
89 """Define profile to be used by T-Rex astf traffic generator.
91 This method MUST return:
93 return ip_gen, templates, kwargs
95 templates or kwargs CAN be None.
96 Kwargs can be used to define PCAP file, set MSS, ...
98 :returns: IP generator and profile templates or list of pcap files for
102 raise NotImplementedError
104 def create_profile(self):
105 """Create traffic profile.
107 Implement your own traffic profiles.
109 :returns: Traffic profile.
110 :rtype: trex.astf.trex_astf_profile.ASTFProfile
112 ip_gen, templates, kwargs = self.define_profile()
116 profile = ASTFProfile(
117 default_ip_gen=ip_gen,
124 def get_profile(self, **kwargs):
125 """Get traffic profile created by "create_profile" method.
127 If needed, add your own parameters.
129 :param kwargs: Key-value pairs used by "create_profile" method while
130 creating the profile.
132 :returns: Traffic profile.
133 :rtype: trex.astf.trex_astf_profile.ASTFProfile
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"
141 return self.create_profile()