1 # Copyright (c) 2019 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 stream profiles for T-rex traffic generator.
21 from random import choice
22 from string import letters
24 from trex.stl.api import *
27 class TrafficStreamsBaseClass(object):
28 """Base class for stream profiles for T-rex traffic generator.
33 {'size': 60, 'pps': 28, 'isg': 0},
34 {'size': 590, 'pps': 20, 'isg': 0.1},
35 {'size': 1514, 'pps': 4, 'isg': 0.2}
38 {'size': 64, 'pps': 28, 'isg': 0},
39 {'size': 570, 'pps': 16, 'isg': 0.1},
40 {'size': 1518, 'pps': 4, 'isg': 0.2}
46 # Default value of frame size, it will be overwritten by the value of
47 # "framesize" parameter of "get_streams" method.
50 # If needed, add your own parameters.
52 def _gen_payload(self, length):
55 If needed, implement your own algorithm.
57 :param length: Length of generated payload.
59 :returns: The generated payload.
64 for _ in range(length):
65 payload += choice(letters)
69 def _get_start_end_ipv6(self, start_ip, end_ip):
70 """Get start host and number of hosts from IPv6 as integer.
72 :param start_ip: Start IPv6.
73 :param end_ip: End IPv6.
74 :type start_ip: string
76 :return: Start host, number of hosts.
78 :raises: ValueError if start_ip is greater then end_ip.
79 :raises: socket.error if the IP addresses are not valid IPv6 addresses.
83 ip1 = socket.inet_pton(socket.AF_INET6, start_ip)
84 ip2 = socket.inet_pton(socket.AF_INET6, end_ip)
86 hi1, lo1 = struct.unpack('!QQ', ip1)
87 hi2, lo2 = struct.unpack('!QQ', ip2)
89 if ((hi1 << 64) | lo1) > ((hi2 << 64) | lo2):
90 raise ValueError("IPv6: start_ip is greater then end_ip")
92 return lo1, abs(int(lo1) - int(lo2))
94 except socket.error as err:
98 def define_packets(self):
99 """Define the packets to be sent from the traffic generator.
101 This method MUST return:
103 return base_pkt_a, base_pkt_b, vm1, vm2
105 vm1 and vm2 CAN be None.
107 :returns: Packets to be sent from the traffic generator.
110 raise NotImplementedError
112 def create_streams(self):
113 """Create traffic streams.
115 Implement your own traffic streams.
117 :returns: Traffic streams.
121 base_pkt_a, base_pkt_b, vm1, vm2 = self.define_packets()
123 # In most cases you will not have to change the code below:
125 # Frame size is defined as an integer, e.g. 64, 1518:
126 if isinstance(self.framesize, int):
128 # Create a base packet and pad it to size
129 payload_len = max(0, self.framesize - len(base_pkt_a) - 4) # No FCS
132 pkt_a = STLPktBuilder(
133 pkt=base_pkt_a / self._gen_payload(payload_len), vm=vm1)
135 pkt_b = STLPktBuilder(
136 pkt=base_pkt_b / self._gen_payload(payload_len), vm=vm2)
138 # Packets for latency measurement:
140 pkt_lat_a = STLPktBuilder(
141 pkt=base_pkt_a / self._gen_payload(payload_len), vm=vm1)
143 pkt_lat_b = STLPktBuilder(
144 pkt=base_pkt_b / self._gen_payload(payload_len), vm=vm2)
146 # Create the streams:
148 stream1 = STLStream(packet=pkt_a, mode=STLTXCont(pps=9000))
150 # second traffic stream with a phase of 10ns (inter-stream gap)
151 stream2 = STLStream(packet=pkt_b, isg=10.0,
152 mode=STLTXCont(pps=9000))
154 # Streams for latency measurement:
156 lat_stream1 = STLStream(packet=pkt_lat_a,
157 flow_stats=STLFlowLatencyStats(pg_id=0),
158 mode=STLTXCont(pps=9000))
160 # second traffic stream with a phase of 10ns (inter-stream gap)
161 lat_stream2 = STLStream(packet=pkt_lat_b, isg=10.0,
162 flow_stats=STLFlowLatencyStats(pg_id=1),
163 mode=STLTXCont(pps=9000))
165 return [stream1, stream2, lat_stream1, lat_stream2]
167 # Frame size is defined as a string, e.g.IMIX_v4_1:
168 elif isinstance(self.framesize, str):
173 for stream in self.STREAM_TABLE[self.framesize]:
174 payload_len_a = max(0, stream['size'] - len(base_pkt_a) - 4)
175 payload_len_b = max(0, stream['size'] - len(base_pkt_b) - 4)
176 # Create a base packet and pad it to size
177 pkt_a = STLPktBuilder(
178 pkt=base_pkt_a / self._gen_payload(payload_len_a),
180 pkt_b = STLPktBuilder(
181 pkt=base_pkt_b / self._gen_payload(payload_len_b),
184 # Create the streams:
185 stream1.append(STLStream(packet=pkt_a,
187 mode=STLTXCont(pps=stream['pps'])))
188 stream2.append(STLStream(packet=pkt_b,
190 mode=STLTXCont(pps=stream['pps'])))
192 streams.extend(stream1)
193 streams.extend(stream2)
197 def get_streams(self, **kwargs):
198 """Get traffic streams created by "create_streams" method.
200 If needed, add your own parameters.
202 :param kwargs: Key-value pairs used by "create_streams" method while
204 :returns: Traffic streams.
208 self.framesize = kwargs['framesize']
210 return self.create_streams()