1 # Copyright (c) 2020 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 Apache 2.
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
23 """Base class for profiles for T-rex advanced stateful (astf) traffic generator.
26 from random import choices
27 from string import ascii_letters
29 from trex.astf.api import *
32 class TrafficProfileBaseClass:
33 """Base class for profiles for T-rex astf traffic generator."""
37 {u"size": 60, u"pps": 28, u"isg": 0},
38 {u"size": 590, u"pps": 20, u"isg": 0.1},
39 {u"size": 1514, u"pps": 4, u"isg": 0.2}
42 {u"size": 64, u"pps": 28, u"isg": 0},
43 {u"size": 570, u"pps": 16, u"isg": 0.1},
44 {u"size": 1518, u"pps": 4, u"isg": 0.2}
49 # Default values of required parameters; can be overwritten in
50 # "get_profile" method.
54 # If needed, add your own parameters.
58 """Pcap file directory.
60 If needed, implement your own algorithm.
62 :returns: Pcap file directory.
67 def _gen_padding(self, current_length, required_length=0):
70 If needed, implement your own algorithm.
72 :param current_length: Current length of the packet.
73 :param required_length: Required length of the packet. If set to 0 then
74 self.framesize value is used.
75 :type current_length: int
76 :type required_length: int
77 :returns: The generated padding.
80 # TODO: Add support for IMIX frame size;
81 # use random.randrange(0, len(self.STREAM_TABLE[self.framesize])) ?
82 if not required_length:
83 required_length = self.framesize
85 return str(choices(ascii_letters, k=required_length - current_length))
87 def define_profile(self):
88 """Define profile to be used by T-Rex astf traffic generator.
90 This method MUST return:
92 return ip_gen, templates, cap_list
94 templates or cap_list CAN be None.
96 :returns: IP generator and profile templates or list of pcap files for
100 raise NotImplementedError
102 def create_profile(self):
103 """Create traffic profile.
105 Implement your own traffic profiles.
107 :returns: Traffic profile.
108 :rtype: trex.astf.trex_astf_profile.ASTFProfile
110 ip_gen, templates, cap_list = self.define_profile()
112 # In most cases you will not have to change the code below:
115 profile = ASTFProfile(
116 default_ip_gen=ip_gen,
123 def get_profile(self, **kwargs):
124 """Get traffic profile created by "create_profile" method.
126 If needed, add your own parameters.
128 :param kwargs: Key-value pairs used by "create_profile" method while
129 creating the profile.
130 :returns: Traffic profile.
131 :rtype: trex.astf.trex_astf_profile.ASTFProfile
133 self.framesize = kwargs[u"framesize"]
134 self._pcap_dir = kwargs.get(
135 u"pcap_dir",u"/opt/trex-core-2.82/scripts/avl"
138 return self.create_profile()