1 # Copyright (c) 2020 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """Base class for profiles for T-rex advanced stateful (astf) traffic generator.
17 from random import choices
18 from string import ascii_letters
20 from trex.astf.api import *
23 class TrafficProfileBaseClass:
24 """Base class for profiles for T-rex astf traffic generator."""
28 {u"size": 60, u"pps": 28, u"isg": 0},
29 {u"size": 590, u"pps": 20, u"isg": 0.1},
30 {u"size": 1514, u"pps": 4, u"isg": 0.2}
33 {u"size": 64, u"pps": 28, u"isg": 0},
34 {u"size": 570, u"pps": 16, u"isg": 0.1},
35 {u"size": 1518, u"pps": 4, u"isg": 0.2}
40 # Default values of required parameters; can be overwritten in
41 # "get_profile" method.
45 # If needed, add your own parameters.
49 """Pcap file directory.
51 If needed, implement your own algorithm.
53 :returns: Pcap file directory.
58 def _gen_padding(self, current_length, required_length=0):
61 If needed, implement your own algorithm.
63 :param current_length: Current length of the packet.
64 :param required_length: Required length of the packet. If set to 0 then
65 self.framesize value is used.
66 :type current_length: int
67 :type required_length: int
68 :returns: The generated padding.
71 # TODO: Add support for IMIX frame size;
72 # use random.randrange(0, len(self.STREAM_TABLE[self.framesize])) ?
73 if not required_length:
74 required_length = self.framesize
76 return str(choices(ascii_letters, k=required_length - current_length))
78 def define_profile(self):
79 """Define profile to be used by T-Rex astf traffic generator.
81 This method MUST return:
83 return ip_gen, templates, cap_list
85 templates or cap_list CAN be None.
87 :returns: IP generator and profile templates or list of pcap files for
91 raise NotImplementedError
93 def create_profile(self):
94 """Create traffic profile.
96 Implement your own traffic profiles.
98 :returns: Traffic profile.
99 :rtype: trex.astf.trex_astf_profile.ASTFProfile
101 ip_gen, templates, cap_list = self.define_profile()
103 # In most cases you will not have to change the code below:
106 profile = ASTFProfile(
107 default_ip_gen=ip_gen,
114 def get_profile(self, **kwargs):
115 """Get traffic profile created by "create_profile" method.
117 If needed, add your own parameters.
119 :param kwargs: Key-value pairs used by "create_profile" method while
120 creating the profile.
121 :returns: Traffic profile.
122 :rtype: trex.astf.trex_astf_profile.ASTFProfile
124 self.framesize = kwargs[u"framesize"]
125 self._pcap_dir = kwargs.get(
126 u"pcap_dir",u"/opt/trex-core-2.82/scripts/avl"
129 return self.create_profile()