1 # Copyright (c) 2021 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}
50 # Default values of required parameters; can be overwritten in
51 # "get_profile" method.
55 # If needed, add your own parameters.
59 """Pcap file directory.
61 If needed, implement your own algorithm.
63 :returns: Pcap file directory.
68 def _gen_padding(self, current_length, required_length=0):
71 If needed, implement your own algorithm.
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.
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
86 return str(choices(ascii_letters, k=required_length - current_length))
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, cap_list
95 templates or cap_list CAN be None.
97 :returns: IP generator and profile templates or list of pcap files for
101 raise NotImplementedError
103 def create_profile(self):
104 """Create traffic profile.
106 Implement your own traffic profiles.
108 :returns: Traffic profile.
109 :rtype: trex.astf.trex_astf_profile.ASTFProfile
111 ip_gen, templates, cap_list = self.define_profile()
113 # In most cases you will not have to change the code below:
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.
131 :returns: Traffic profile.
132 :rtype: trex.astf.trex_astf_profile.ASTFProfile
134 self.framesize = kwargs[u"framesize"]
135 self._pcap_dir = kwargs.get(
136 u"pcap_dir",u"/opt/trex-core-2.88/scripts/avl"
139 return self.create_profile()