+ :returns: nothing
+ :raises RuntimeError: If node type is not a TG,
+ or if TRex teardown fails.
+ """
+ subtype = check_subtype(node)
+ if subtype == NodeSubTypeTG.TREX:
+ exec_cmd_no_error(
+ node,
+ u"sh -c "
+ u"\"if pgrep t-rex; then sudo pkill t-rex && sleep 3; fi\"",
+ sudo=False,
+ message=u"pkill t-rex failed"
+ )
+
+ def _parse_traffic_results(self, stdout):
+ """Parse stdout of scripts into fields of self.
+
+ Block of code to reuse, by sync start, or stop after async.
+ TODO: Is the output TG subtype dependent?
+
+ :param stdout: Text containing the standard output.
+ :type stdout: str
+ """
+ # last line from console output
+ line = stdout.splitlines()[-1]
+ self._result = line
+ logger.info(f"TrafficGen result: {self._result}")
+ self._received = self._result.split(u", ")[1].split(u"=", 1)[1]
+ self._sent = self._result.split(u", ")[2].split(u"=", 1)[1]
+ self._loss = self._result.split(u", ")[3].split(u"=", 1)[1]
+ self._latency = list()
+ self._latency.append(self._result.split(u", ")[4].split(u"=", 1)[1])
+ self._latency.append(self._result.split(u", ")[5].split(u"=", 1)[1])
+
+ def trex_stl_stop_remote_exec(self, node):
+ """Execute script on remote node over ssh to stop running traffic.
+
+ Internal state is updated with measurement results.
+
+ :param node: TRex generator node.
+ :type node: dict
+ :raises RuntimeError: If stop traffic script fails.
+ """
+ # No need to check subtype, we know it is TREX.
+ x_args = u""
+ for index, value in enumerate(self._xstats):
+ if value is not None:
+ # Nested quoting is fun.
+ value = value.replace(u"'", u"\"")
+ x_args += f" --xstat{index}='\"'\"'{value}'\"'\"'"
+ stdout, _ = exec_cmd_no_error(
+ node, f"sh -c '{Constants.REMOTE_FW_DIR}/resources/tools/trex/"
+ f"trex_stateless_stop.py{x_args}'",
+ message=u"TRex stateless runtime error"
+ )
+ self._parse_traffic_results(stdout)
+
+ def trex_stl_start_remote_exec(
+ self, duration, rate, frame_size, traffic_profile, async_call=False,
+ latency=True, warmup_time=5.0, traffic_directions=2, tx_port=0,
+ rx_port=1):
+ """Execute script on remote node over ssh to start traffic.
+
+ In sync mode, measurement results are stored internally.
+ In async mode, initial data including xstats are stored internally.
+
+ :param duration: Time expresed in seconds for how long to send traffic.
+ :param rate: Traffic rate expressed with units (pps, %)
+ :param frame_size: L2 frame size to send (without padding and IPG).
+ :param traffic_profile: Module name as a traffic profile identifier.
+ See resources/traffic_profiles/trex for implemented modules.
+ :param async_call: If enabled then don't wait for all incomming trafic.
+ :param latency: With latency measurement.
+ :param warmup_time: Warmup time period.
+ :param traffic_directions: Traffic is bi- (2) or uni- (1) directional.
+ Default: 2
+ :param tx_port: Traffic generator transmit port for first flow.
+ Default: 0
+ :param rx_port: Traffic generator receive port for first flow.
+ Default: 1
+ :type duration: float
+ :type rate: str
+ :type frame_size: str
+ :type traffic_profile: str
+ :type async_call: bool
+ :type latency: bool
+ :type warmup_time: float
+ :type traffic_directions: int
+ :type tx_port: int
+ :type rx_port: int
+ :raises RuntimeError: In case of TG driver issue.
+ """
+ # No need to check subtype, we know it is TREX.
+ reorder = self._ifaces_reordered # Just to make the next line fit.
+ p_0, p_1 = (rx_port, tx_port) if reorder else (tx_port, rx_port)
+
+ if not isinstance(duration, (float, int)):
+ duration = float(duration)
+ if not isinstance(warmup_time, (float, int)):
+ warmup_time = float(warmup_time)
+ command = f"sh -c \"" \
+ f"{Constants.REMOTE_FW_DIR}/resources/tools/trex/" \
+ f"trex_stateless_profile.py " \
+ f"--profile {Constants.REMOTE_FW_DIR}/resources/" \
+ f"traffic_profiles/trex/{traffic_profile}.py " \
+ f"--duration {duration!r} --frame_size {frame_size} " \
+ f"--rate {rate!r} --warmup_time {warmup_time!r} " \
+ f"--port_0 {p_0} --port_1 {p_1} " \
+ f"--traffic_directions {traffic_directions}"
+ if async_call:
+ command += u" --async_start"
+ if latency:
+ command += u" --latency"
+ if Constants.TREX_SEND_FORCE:
+ command += u" --force"
+ command += u"\""
+
+ stdout, _ = exec_cmd_no_error(
+ self._node, command, timeout=float(duration) + 60,
+ message=u"TRex stateless runtime error"
+ )
+
+ self.traffic_directions = traffic_directions
+ if async_call:
+ # no result
+ self._start_time = time.time()
+ self._rate = float(rate[:-3]) if u"pps" in rate else float(rate)
+ self._received = None
+ self._sent = None
+ self._loss = None
+ self._latency = None
+ xstats = [None, None]
+ index = 0
+ for line in stdout.splitlines():
+ if f"Xstats snapshot {index}: " in line:
+ xstats[index] = line[19:]
+ index += 1
+ if index == 2:
+ break
+ self._xstats = tuple(xstats)
+ else:
+ self._parse_traffic_results(stdout)
+ self._start_time = None
+ self._rate = None
+
+ def stop_traffic_on_tg(self):
+ """Stop all traffic on TG.
+
+ :returns: Structure containing the result of the measurement.
+ :rtype: ReceiveRateMeasurement
+ :raises RuntimeError: If TG is not set.
+ """
+ subtype = check_subtype(self._node)
+ if subtype == NodeSubTypeTG.TREX:
+ self.trex_stl_stop_remote_exec(self._node)
+ return self.get_measurement_result()
+
+ def send_traffic_on_tg(
+ self, duration, rate, frame_size, traffic_profile, warmup_time=5,
+ async_call=False, latency=True, traffic_directions=2, tx_port=0,
+ rx_port=1):
+ """Send traffic from all configured interfaces on TG.
+
+ In async mode, xstats is stored internally,
+ to enable getting correct result when stopping the traffic.
+ In both modes, stdout is returned,
+ but _parse_traffic_results only works in sync output.
+
+ Note that bidirectional traffic also contains flows
+ transmitted from rx_port and received in tx_port.
+ But some tests use asymmetric traffic, so those arguments are relevant.
+
+ Also note that traffic generator uses DPDK driver which might
+ reorder port numbers based on wiring and PCI numbering.
+ This method handles that, so argument values are invariant,
+ but you can see swapped valued in debug logs.
+
+ TODO: Is it better to have less descriptive argument names
+ just to make them less probable to be viewed as misleading or confusing?
+ See https://gerrit.fd.io/r/#/c/17625/11/resources/libraries/python\
+ /TrafficGenerator.py@406
+
+ :param duration: Duration of test traffic generation in seconds.
+ :param rate: Offered load per interface (e.g. 1%, 3gbps, 4mpps, ...).
+ :param frame_size: Frame size (L2) in Bytes.
+ :param traffic_profile: Module name as a traffic profile identifier.
+ See resources/traffic_profiles/trex for implemented modules.
+ :param warmup_time: Warmup phase in seconds.
+ :param async_call: Async mode.
+ :param latency: With latency measurement.
+ :param traffic_directions: Traffic is bi- (2) or uni- (1) directional.
+ Default: 2
+ :param tx_port: Traffic generator transmit port for first flow.
+ Default: 0
+ :param rx_port: Traffic generator receive port for first flow.
+ Default: 1