1 # Copyright (c) 2018 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 """Performance testing traffic generator library."""
16 from robot.api import logger
17 from robot.libraries.BuiltIn import BuiltIn
19 from resources.libraries.python.constants import Constants
20 from resources.libraries.python.ssh import SSH
21 from resources.libraries.python.topology import NodeType
22 from resources.libraries.python.topology import NodeSubTypeTG
23 from resources.libraries.python.topology import Topology
24 from resources.libraries.python.DropRateSearch import DropRateSearch
26 __all__ = ['TrafficGenerator', 'TGDropRateSearchImpl']
29 class TGDropRateSearchImpl(DropRateSearch):
30 """Drop Rate Search implementation."""
33 super(TGDropRateSearchImpl, self).__init__()
35 def measure_loss(self, rate, frame_size, loss_acceptance,
36 loss_acceptance_type, traffic_type):
37 """Runs the traffic and evaluate the measured results.
39 :param rate: Offered traffic load.
40 :param frame_size: Size of frame.
41 :param loss_acceptance: Permitted drop ratio or frames count.
42 :param loss_acceptance_type: Type of permitted loss.
43 :param traffic_type: Traffic profile ([2,3]-node-L[2,3], ...).
46 :type loss_acceptance: float
47 :type loss_acceptance_type: LossAcceptanceType
48 :type traffic_type: str
49 :returns: Drop threshold exceeded? (True/False)
51 :raises: NotImplementedError if TG is not supported.
52 :raises: RuntimeError if TG is not specified.
54 # we need instance of TrafficGenerator instantiated by Robot Framework
55 # to be able to use trex_stl-*()
56 tg_instance = BuiltIn().get_library_instance(
57 'resources.libraries.python.TrafficGenerator')
59 if tg_instance.node['subtype'] is None:
60 raise RuntimeError('TG subtype not defined')
61 elif tg_instance.node['subtype'] == NodeSubTypeTG.TREX:
62 unit_rate = str(rate) + self.get_rate_type_str()
63 tg_instance.trex_stl_start_remote_exec(self.get_duration(),
64 unit_rate, frame_size,
66 loss = tg_instance.get_loss()
67 sent = tg_instance.get_sent()
68 if self.loss_acceptance_type_is_percentage():
69 loss = (float(loss) / float(sent)) * 100
71 logger.trace("comparing: {} < {} {}".format(loss,
73 loss_acceptance_type))
74 if float(loss) > float(loss_acceptance):
79 raise NotImplementedError("TG subtype not supported")
81 def get_latency(self):
82 """Returns min/avg/max latency.
84 :returns: Latency stats.
87 tg_instance = BuiltIn().get_library_instance(
88 'resources.libraries.python.TrafficGenerator')
89 return tg_instance.get_latency_int()
92 class TrafficGenerator(object):
93 """Traffic Generator."""
95 # use one instance of TrafficGenerator for all tests in test suite
96 ROBOT_LIBRARY_SCOPE = 'TEST SUITE'
103 self._received = None
105 # T-REX interface order mapping
106 self._ifaces_reordered = False
112 :returns: Traffic generator node.
118 """Return number of lost packets.
120 :returns: Number of lost packets.
126 """Return number of sent packets.
128 :returns: Number of sent packets.
133 def get_received(self):
134 """Return number of received packets.
136 :returns: Number of received packets.
139 return self._received
141 def get_latency_int(self):
142 """Return rounded min/avg/max latency.
144 :returns: Latency stats.
149 def initialize_traffic_generator(self, tg_node, tg_if1, tg_if2,
150 tg_if1_adj_node, tg_if1_adj_if,
151 tg_if2_adj_node, tg_if2_adj_if,
153 tg_if1_dst_mac=None, tg_if2_dst_mac=None):
154 """TG initialization.
156 :param tg_node: Traffic generator node.
157 :param tg_if1: TG - name of first interface.
158 :param tg_if2: TG - name of second interface.
159 :param tg_if1_adj_node: TG if1 adjecent node.
160 :param tg_if1_adj_if: TG if1 adjecent interface.
161 :param tg_if2_adj_node: TG if2 adjecent node.
162 :param tg_if2_adj_if: TG if2 adjecent interface.
163 :param test_type: 'L2', 'L3' or 'L7' - OSI Layer testing type.
164 :param tg_if1_dst_mac: Interface 1 destination MAC address.
165 :param tg_if2_dst_mac: Interface 2 destination MAC address.
169 :type tg_if1_adj_node: dict
170 :type tg_if1_adj_if: str
171 :type tg_if2_adj_node: dict
172 :type tg_if2_adj_if: str
174 :type tg_if1_dst_mac: str
175 :type tg_if2_dst_mac: str
177 :raises: RuntimeError in case of issue during initialization.
179 if tg_node['type'] != NodeType.TG:
180 raise RuntimeError('Node type is not a TG')
183 if tg_node['subtype'] == NodeSubTypeTG.TREX:
187 (ret, _, _) = ssh.exec_command(
188 "sudo -E sh -c '{0}/resources/tools/trex/"
189 "trex_installer.sh {1}'".format(Constants.REMOTE_FW_DIR,
190 Constants.TREX_INSTALL_VERSION),
193 raise RuntimeError('TRex installation failed.')
195 if1_pci = Topology().get_interface_pci_addr(tg_node, tg_if1)
196 if2_pci = Topology().get_interface_pci_addr(tg_node, tg_if2)
197 if1_addr = Topology().get_interface_mac(tg_node, tg_if1)
198 if2_addr = Topology().get_interface_mac(tg_node, tg_if2)
200 if test_type == 'L2':
201 if1_adj_addr = if2_addr
202 if2_adj_addr = if1_addr
203 elif test_type == 'L3':
204 if1_adj_addr = Topology().get_interface_mac(tg_if1_adj_node,
206 if2_adj_addr = Topology().get_interface_mac(tg_if2_adj_node,
208 elif test_type == 'L7':
209 if1_addr = Topology().get_interface_ip4(tg_node, tg_if1)
210 if2_addr = Topology().get_interface_ip4(tg_node, tg_if2)
211 if1_adj_addr = Topology().get_interface_ip4(tg_if1_adj_node,
213 if2_adj_addr = Topology().get_interface_ip4(tg_if2_adj_node,
216 raise ValueError("Unknown Test Type")
218 # in case of switched environment we can override MAC addresses
219 if tg_if1_dst_mac is not None and tg_if2_dst_mac is not None:
220 if1_adj_addr = tg_if1_dst_mac
221 if2_adj_addr = tg_if2_dst_mac
223 if min(if1_pci, if2_pci) != if1_pci:
224 if1_pci, if2_pci = if2_pci, if1_pci
225 if1_addr, if2_addr = if2_addr, if1_addr
226 if1_adj_addr, if2_adj_addr = if2_adj_addr, if1_adj_addr
227 self._ifaces_reordered = True
229 if test_type == 'L2' or test_type == 'L3':
230 (ret, _, _) = ssh.exec_command(
231 "sudo sh -c 'cat << EOF > /etc/trex_cfg.yaml\n"
234 " interfaces: [\"{0}\",\"{1}\"]\n"
236 " - dest_mac: [{2}]\n"
238 " - dest_mac: [{4}]\n"
241 .format(if1_pci, if2_pci,
242 "0x"+if1_adj_addr.replace(":", ",0x"),
243 "0x"+if1_addr.replace(":", ",0x"),
244 "0x"+if2_adj_addr.replace(":", ",0x"),
245 "0x"+if2_addr.replace(":", ",0x")))
246 elif test_type == 'L7':
247 (ret, _, _) = ssh.exec_command(
248 "sudo sh -c 'cat << EOF > /etc/trex_cfg.yaml\n"
251 " interfaces: [\"{0}\",\"{1}\"]\n"
254 " default_gw: [{3}]\n"
256 " default_gw: [{5}]\n"
258 .format(if1_pci, if2_pci,
259 if1_addr, if1_adj_addr,
260 if2_addr, if2_adj_addr))
262 raise ValueError("Unknown Test Type")
264 raise RuntimeError('TRex config generation error')
266 for _ in range(0, 3):
267 # kill TRex only if it is already running
269 "sh -c 'pgrep t-rex && sudo pkill t-rex && sleep 3'")
272 (ret, _, _) = ssh.exec_command(
273 "sh -c 'cd {0}/scripts/ && sudo ./trex-cfg'"\
274 .format(Constants.TREX_INSTALL_DIR))
276 raise RuntimeError('trex-cfg failed')
279 if test_type == 'L2' or test_type == 'L3':
280 (ret, _, _) = ssh.exec_command(
281 "sh -c 'cd {0}/scripts/ && "
282 "sudo nohup ./t-rex-64 -i -c 7 --iom 0 > /tmp/trex.log "
283 "2>&1 &' > /dev/null"\
284 .format(Constants.TREX_INSTALL_DIR))
285 elif test_type == 'L7':
286 (ret, _, _) = ssh.exec_command(
287 "sh -c 'cd {0}/scripts/ && "
288 "sudo nohup ./t-rex-64 --astf -i -c 7 --iom 0 > "
289 "/tmp/trex.log 2>&1 &' > /dev/null"\
290 .format(Constants.TREX_INSTALL_DIR))
292 raise ValueError("Unknown Test Type")
294 ssh.exec_command("sh -c 'cat /tmp/trex.log'")
295 raise RuntimeError('t-rex-64 startup failed')
297 # get TRex server info
298 (ret, _, _) = ssh.exec_command(
300 "{0}/resources/tools/trex/trex_server_info.py'"\
301 .format(Constants.REMOTE_FW_DIR),
304 # If we get info TRex is running
306 # after max retries TRex is still not responding to API
307 # critical error occurred
308 raise RuntimeError('t-rex-64 startup failed')
311 def is_trex_running(node):
312 """Check if TRex is running using pidof.
314 :param node: Traffic generator node.
316 :returns: True if TRex is running otherwise False.
318 :raises: RuntimeError if node type is not a TG.
320 if node['type'] != NodeType.TG:
321 raise RuntimeError('Node type is not a TG')
325 ret, _, _ = ssh.exec_command_sudo("pidof t-rex")
326 return bool(int(ret) == 0)
329 def teardown_traffic_generator(node):
332 :param node: Traffic generator node.
335 :raises: RuntimeError if TRex teardown failed.
336 :raises: RuntimeError if node type is not a TG.
338 if node['type'] != NodeType.TG:
339 raise RuntimeError('Node type is not a TG')
340 if node['subtype'] == NodeSubTypeTG.TREX:
343 (ret, _, _) = ssh.exec_command(
344 "sh -c 'sudo pkill t-rex && sleep 3'")
346 raise RuntimeError('pkill t-rex failed')
349 def trex_stl_stop_remote_exec(node):
350 """Execute script on remote node over ssh to stop running traffic.
352 :param node: TRex generator node.
355 :raises: RuntimeError if stop traffic script fails.
360 (ret, _, _) = ssh.exec_command(
361 "sh -c '{}/resources/tools/trex/"
362 "trex_stateless_stop.py'".format(Constants.REMOTE_FW_DIR))
365 raise RuntimeError('TRex stateless runtime error')
367 def trex_stl_start_remote_exec(self, duration, rate, framesize,
368 traffic_type, async_call=False,
369 latency=True, warmup_time=5):
370 """Execute script on remote node over ssh to start traffic.
372 :param duration: Time expresed in seconds for how long to send traffic.
373 :param rate: Traffic rate expressed with units (pps, %)
374 :param framesize: L2 frame size to send (without padding and IPG).
375 :param traffic_type: Traffic profile.
376 :param async_call: If enabled then don't wait for all incomming trafic.
377 :param latency: With latency measurement.
378 :param warmup_time: Warmup time period.
382 :type traffic_type: str
383 :type async_call: bool
385 :type warmup_time: int
387 :raises: RuntimeError in case of TG driver issue.
390 ssh.connect(self._node)
392 _async = "--async" if async_call else ""
393 _latency = "--latency" if latency else ""
394 _p0, _p1 = (2, 1) if self._ifaces_reordered else (1, 2)
396 profile_path = ("{0}/resources/traffic_profiles/trex/"
397 "{1}.py".format(Constants.REMOTE_FW_DIR,
399 (ret, stdout, _) = ssh.exec_command(
401 "'{0}/resources/tools/trex/trex_stateless_profile.py "
411 format(Constants.REMOTE_FW_DIR, profile_path, duration, framesize,
412 rate, warmup_time, _p0 - 1, _p1 - 1, _async, _latency),
413 timeout=int(duration) + 60)
416 raise RuntimeError('TRex stateless runtime error')
419 self._received = None
424 # last line from console output
425 line = stdout.splitlines()[-1]
428 logger.info('TrafficGen result: {0}'.format(self._result))
430 self._received = self._result.split(', ')[1].split('=')[1]
431 self._sent = self._result.split(', ')[2].split('=')[1]
432 self._loss = self._result.split(', ')[3].split('=')[1]
435 self._latency.append(self._result.split(', ')[4].split('=')[1])
436 self._latency.append(self._result.split(', ')[5].split('=')[1])
438 def stop_traffic_on_tg(self):
439 """Stop all traffic on TG.
442 :raises: RuntimeError if TG is not set.
444 if self._node is None:
445 raise RuntimeError("TG is not set")
446 if self._node['subtype'] == NodeSubTypeTG.TREX:
447 self.trex_stl_stop_remote_exec(self._node)
449 def send_traffic_on_tg(self, duration, rate, framesize,
450 traffic_type, warmup_time=5, async_call=False,
452 """Send traffic from all configured interfaces on TG.
454 :param duration: Duration of test traffic generation in seconds.
455 :param rate: Offered load per interface (e.g. 1%, 3gbps, 4mpps, ...).
456 :param framesize: Frame size (L2) in Bytes.
457 :param traffic_type: Traffic profile.
458 :param warmup_time: Warmup phase in seconds.
459 :param async_call: Async mode.
460 :param latency: With latency measurement.
464 :type traffic_type: str
465 :type warmup_time: int
466 :type async_call: bool
470 :raises: RuntimeError if TG is not set.
471 :raises: RuntimeError if node is not TG or subtype is not specified.
472 :raises: NotImplementedError if TG is not supported.
477 raise RuntimeError("TG is not set")
479 if node['type'] != NodeType.TG:
480 raise RuntimeError('Node type is not a TG')
482 if node['subtype'] is None:
483 raise RuntimeError('TG subtype not defined')
484 elif node['subtype'] == NodeSubTypeTG.TREX:
485 self.trex_stl_start_remote_exec(int(duration), rate, framesize,
486 traffic_type, async_call, latency,
487 warmup_time=warmup_time)
489 raise NotImplementedError("TG subtype not supported")
493 def no_traffic_loss_occurred(self):
494 """Fail if loss occurred in traffic run.
497 :raises: Exception if loss occured.
499 if self._loss is None:
500 raise Exception('The traffic generation has not been issued')
501 if self._loss != '0':
502 raise Exception('Traffic loss occurred: {0}'.format(self._loss))
504 def partial_traffic_loss_accepted(self, loss_acceptance,
505 loss_acceptance_type):
506 """Fail if loss is higher then accepted in traffic run.
508 :param loss_acceptance: Permitted drop ratio or frames count.
509 :param loss_acceptance_type: Type of permitted loss.
510 :type loss_acceptance: float
511 :type loss_acceptance_type: LossAcceptanceType
513 :raises: Exception if loss is above acceptance criteria.
515 if self._loss is None:
516 raise Exception('The traffic generation has not been issued')
518 if loss_acceptance_type == 'percentage':
519 loss = (float(self._loss) / float(self._sent)) * 100
520 elif loss_acceptance_type == 'frames':
521 loss = float(self._loss)
523 raise Exception('Loss acceptance type not supported')
525 if loss > float(loss_acceptance):
526 raise Exception("Traffic loss {} above loss acceptance: {}".format(
527 loss, loss_acceptance))