Skip to content

Commit cb1acbd

Browse files
cjubrankuba-moo
authored andcommitted
selftests: drv-net: Use Iperf3Runner in devlink_rate_tc_bw.py
Replace the inline iperf3 subprocess and JSON parsing with Iperf3Runner. Signed-off-by: Carolina Jubran <cjubran@nvidia.com> Reviewed-by: Cosmin Ratiu <cratiu@nvidia.com> Reviewed-by: Nimrod Oren <noren@nvidia.com> Link: https://patch.msgid.link/20251130091938.4109055-4-cjubran@nvidia.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 2a60ce9 commit cb1acbd

1 file changed

Lines changed: 29 additions & 41 deletions

File tree

tools/testing/selftests/drivers/net/hw/devlink_rate_tc_bw.py

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
from lib.py import NetDrvEpEnv, DevlinkFamily
6565
from lib.py import NlError
6666
from lib.py import cmd, defer, ethtool, ip
67+
from lib.py import Iperf3Runner
6768

6869

6970
class BandwidthValidator:
@@ -139,8 +140,8 @@ def setup_vlans_on_vf(vf_ifc):
139140
Sets up two VLAN interfaces on the given VF, each mapped to a different TC.
140141
"""
141142
vlan_configs = [
142-
{"vlan_id": 101, "tc": 3, "ip": "198.51.100.2"},
143-
{"vlan_id": 102, "tc": 4, "ip": "198.51.100.10"},
143+
{"vlan_id": 101, "tc": 3, "ip": "198.51.100.1"},
144+
{"vlan_id": 102, "tc": 4, "ip": "198.51.100.9"},
144145
]
145146

146147
for config in vlan_configs:
@@ -224,28 +225,27 @@ def setup_devlink_rate(cfg):
224225
raise KsftFailEx(f"rate_set failed on VF port {port_index}") from exc
225226

226227

227-
def setup_remote_server(cfg):
228+
def setup_remote_vlans(cfg):
228229
"""
229-
Sets up VLAN interfaces and starts iperf3 servers on the remote side.
230+
Sets up VLAN interfaces on the remote side.
230231
"""
231232
remote_dev = cfg.remote_ifname
232233
vlan_ids = [101, 102]
233-
remote_ips = ["198.51.100.1", "198.51.100.9"]
234+
remote_ips = ["198.51.100.2", "198.51.100.10"]
234235

235236
for vlan_id, ip_addr in zip(vlan_ids, remote_ips):
236237
vlan_dev = f"{remote_dev}.{vlan_id}"
237238
cmd(f"ip link add link {remote_dev} name {vlan_dev} "
238239
f"type vlan id {vlan_id}", host=cfg.remote)
239240
cmd(f"ip addr add {ip_addr}/29 dev {vlan_dev}", host=cfg.remote)
240241
cmd(f"ip link set dev {vlan_dev} up", host=cfg.remote)
241-
cmd(f"iperf3 -s -1 -B {ip_addr}",background=True, host=cfg.remote)
242242
defer(cmd, f"ip link del {vlan_dev}", host=cfg.remote)
243243

244244

245245
def setup_test_environment(cfg, set_tc_mapping=True):
246246
"""
247247
Sets up the complete test environment including VF creation, VLANs,
248-
bridge configuration, devlink rate setup, and the remote server.
248+
bridge configuration and devlink rate setup.
249249
"""
250250
vf_ifc = setup_vf(cfg, set_tc_mapping)
251251
ksft_pr(f"Created VF interface: {vf_ifc}")
@@ -256,51 +256,39 @@ def setup_test_environment(cfg, set_tc_mapping=True):
256256
setup_bridge(cfg)
257257

258258
setup_devlink_rate(cfg)
259-
setup_remote_server(cfg)
260-
time.sleep(2)
259+
setup_remote_vlans(cfg)
261260

262261

263-
def run_iperf_client(server_ip, local_ip, barrier, min_expected_gbps=0.1):
262+
def measure_bandwidth(cfg, server_ip, client_ip, barrier):
264263
"""
265-
Runs a single iperf3 client instance, binding to the given local IP.
266-
Waits on a barrier to synchronize with other threads.
264+
Synchronizes with peers and runs an iperf3-based bandwidth measurement
265+
between the given endpoints. Returns average Gbps.
267266
"""
267+
runner = Iperf3Runner(cfg, server_ip=server_ip, client_ip=client_ip)
268268
try:
269269
barrier.wait(timeout=10)
270270
except Exception as exc:
271271
raise KsftFailEx("iperf3 barrier wait timed") from exc
272272

273-
iperf_cmd = ["iperf3", "-c", server_ip, "-B", local_ip, "-J"]
274-
result = subprocess.run(iperf_cmd, capture_output=True, text=True,
275-
check=True)
276-
277273
try:
278-
output = json.loads(result.stdout)
279-
bits_per_second = output["end"]["sum_received"]["bits_per_second"]
280-
gbps = bits_per_second / 1e9
281-
if gbps < min_expected_gbps:
282-
ksft_pr(
283-
f"iperf3 bandwidth too low: {gbps:.2f} Gbps "
284-
f"(expected ≥ {min_expected_gbps} Gbps)"
285-
)
286-
return None
287-
return gbps
288-
except json.JSONDecodeError as exc:
289-
ksft_pr(f"Failed to parse iperf3 JSON output: {exc}")
290-
return None
274+
bw_gbps = runner.measure_bandwidth(reverse=True)
275+
except Exception as exc:
276+
raise KsftFailEx("iperf3 bandwidth measurement failed") from exc
291277

278+
return bw_gbps
292279

293-
def run_bandwidth_test():
280+
281+
def run_bandwidth_test(cfg):
294282
"""
295-
Launches iperf3 client threads for each VLAN/TC pair and collects results.
283+
Runs parallel bandwidth measurements for each VLAN/TC pair and collects results.
296284
"""
297-
def _run_iperf_client_thread(server_ip, local_ip, results, barrier, tc_ix):
298-
results[tc_ix] = run_iperf_client(server_ip, local_ip, barrier)
285+
def _run_measure_bandwidth_thread(local_ip, remote_ip, results, barrier, tc_ix):
286+
results[tc_ix] = measure_bandwidth(cfg, local_ip, remote_ip, barrier)
299287

300288
vf_vlan_data = [
301289
# (local_ip, remote_ip, TC)
302-
("198.51.100.2", "198.51.100.1", 3),
303-
("198.51.100.10", "198.51.100.9", 4),
290+
("198.51.100.1", "198.51.100.2", 3),
291+
("198.51.100.9", "198.51.100.10", 4),
304292
]
305293

306294
results = {}
@@ -309,8 +297,8 @@ def _run_iperf_client_thread(server_ip, local_ip, results, barrier, tc_ix):
309297

310298
for local_ip, remote_ip, tc_ix in vf_vlan_data:
311299
thread = threading.Thread(
312-
target=_run_iperf_client_thread,
313-
args=(remote_ip, local_ip, results, start_barrier, tc_ix)
300+
target=_run_measure_bandwidth_thread,
301+
args=(local_ip, remote_ip, results, start_barrier, tc_ix)
314302
)
315303
thread.start()
316304
threads.append(thread)
@@ -320,10 +308,11 @@ def _run_iperf_client_thread(server_ip, local_ip, results, barrier, tc_ix):
320308

321309
for tc_ix, tc_bw in results.items():
322310
if tc_bw is None:
323-
raise KsftFailEx("iperf3 client failed; cannot evaluate bandwidth")
311+
raise KsftFailEx("iperf3 failed; cannot evaluate bandwidth")
324312

325313
return results
326314

315+
327316
def calculate_bandwidth_percentages(results):
328317
"""
329318
Calculates the percentage of total bandwidth received by TC3 and TC4.
@@ -398,10 +387,10 @@ def check_bandwidth_distribution(bw_data, validator):
398387

399388
def run_bandwidth_distribution_test(cfg, set_tc_mapping):
400389
"""
401-
Runs parallel iperf3 tests for both TCs and collects results.
390+
Runs parallel bandwidth measurements for both TCs and collects results.
402391
"""
403392
setup_test_environment(cfg, set_tc_mapping)
404-
bandwidths = run_bandwidth_test()
393+
bandwidths = run_bandwidth_test(cfg)
405394
bw_data = calculate_bandwidth_percentages(bandwidths)
406395
test_name = "with TC mapping" if set_tc_mapping else "without TC mapping"
407396
print_bandwidth_results(bw_data, test_name)
@@ -451,7 +440,6 @@ def main() -> None:
451440
)
452441
if not cfg.pci:
453442
raise KsftSkipEx("Could not get PCI address of the interface")
454-
cfg.require_cmd("iperf3", local=True, remote=True)
455443

456444
cfg.bw_validator = BandwidthValidator()
457445

0 commit comments

Comments
 (0)