1# SPDX-License-Identifier: GPL-2.0 2 3import time 4 5from lib.py import ksft_pr, cmd, ip, rand_port, wait_port_listen 6 7class GenerateTraffic: 8 def __init__(self, env): 9 env.require_cmd("iperf3", remote=True) 10 11 self.env = env 12 13 port = rand_port() 14 self._iperf_server = cmd(f"iperf3 -s -p {port}", background=True) 15 wait_port_listen(port) 16 time.sleep(0.1) 17 self._iperf_client = cmd(f"iperf3 -c {env.addr} -P 16 -p {port} -t 86400", 18 background=True, host=env.remote) 19 20 # Wait for traffic to ramp up 21 pkt = ip("-s link show dev " + env.ifname, json=True)[0]["stats64"]["rx"]["packets"] 22 for _ in range(50): 23 time.sleep(0.1) 24 now = ip("-s link show dev " + env.ifname, json=True)[0]["stats64"]["rx"]["packets"] 25 if now - pkt > 1000: 26 return 27 pkt = now 28 self.stop(verbose=True) 29 raise Exception("iperf3 traffic did not ramp up") 30 31 def stop(self, verbose=None): 32 self._iperf_client.process(terminate=True) 33 if verbose: 34 ksft_pr(">> Client:") 35 ksft_pr(self._iperf_client.stdout) 36 ksft_pr(self._iperf_client.stderr) 37 self._iperf_server.process(terminate=True) 38 if verbose: 39 ksft_pr(">> Server:") 40 ksft_pr(self._iperf_server.stdout) 41 ksft_pr(self._iperf_server.stderr) 42