Path: blob/master/tools/testing/selftests/drivers/net/hw/csum.py
26295 views
#!/usr/bin/env python31# SPDX-License-Identifier: GPL-2.023"""Run the tools/testing/selftests/net/csum testsuite."""45from os import path67from lib.py import ksft_run, ksft_exit, KsftSkipEx8from lib.py import EthtoolFamily, NetDrvEpEnv9from lib.py import bkg, cmd, wait_port_listen1011def test_receive(cfg, ipver="6", extra_args=None):12"""Test local nic checksum receive. Remote host sends crafted packets."""13if not cfg.have_rx_csum:14raise KsftSkipEx(f"Test requires rx checksum offload on {cfg.ifname}")1516ip_args = f"-{ipver} -S {cfg.remote_addr_v[ipver]} -D {cfg.addr_v[ipver]}"1718rx_cmd = f"{cfg.bin_local} -i {cfg.ifname} -n 100 {ip_args} -r 1 -R {extra_args}"19tx_cmd = f"{cfg.bin_remote} -i {cfg.remote_ifname} -n 100 {ip_args} -r 1 -T {extra_args}"2021with bkg(rx_cmd, exit_wait=True):22wait_port_listen(34000, proto="udp")23cmd(tx_cmd, host=cfg.remote)242526def test_transmit(cfg, ipver="6", extra_args=None):27"""Test local nic checksum transmit. Remote host verifies packets."""28if (not cfg.have_tx_csum_generic and29not (cfg.have_tx_csum_ipv4 and ipver == "4") and30not (cfg.have_tx_csum_ipv6 and ipver == "6")):31raise KsftSkipEx(f"Test requires tx checksum offload on {cfg.ifname}")3233ip_args = f"-{ipver} -S {cfg.addr_v[ipver]} -D {cfg.remote_addr_v[ipver]}"3435# Cannot randomize input when calculating zero checksum36if extra_args != "-U -Z":37extra_args += " -r 1"3839rx_cmd = f"{cfg.bin_remote} -i {cfg.remote_ifname} -L 1 -n 100 {ip_args} -R {extra_args}"40tx_cmd = f"{cfg.bin_local} -i {cfg.ifname} -L 1 -n 100 {ip_args} -T {extra_args}"4142with bkg(rx_cmd, host=cfg.remote, exit_wait=True):43wait_port_listen(34000, proto="udp", host=cfg.remote)44cmd(tx_cmd)454647def test_builder(name, cfg, ipver="6", tx=False, extra_args=""):48"""Construct specific tests from the common template.4950Most tests follow the same basic pattern, differing only in51Direction of the test and optional flags passed to csum."""52def f(cfg):53cfg.require_ipver(ipver)5455if tx:56test_transmit(cfg, ipver, extra_args)57else:58test_receive(cfg, ipver, extra_args)5960f.__name__ = f"ipv{ipver}_" + name61return f626364def check_nic_features(cfg) -> None:65"""Test whether Tx and Rx checksum offload are enabled.6667If the device under test has either off, then skip the relevant tests."""68cfg.have_tx_csum_generic = False69cfg.have_tx_csum_ipv4 = False70cfg.have_tx_csum_ipv6 = False71cfg.have_rx_csum = False7273ethnl = EthtoolFamily()74features = ethnl.features_get({"header": {"dev-index": cfg.ifindex}})75for f in features["active"]["bits"]["bit"]:76if f["name"] == "tx-checksum-ip-generic":77cfg.have_tx_csum_generic = True78elif f["name"] == "tx-checksum-ipv4":79cfg.have_tx_csum_ipv4 = True80elif f["name"] == "tx-checksum-ipv6":81cfg.have_tx_csum_ipv6 = True82elif f["name"] == "rx-checksum":83cfg.have_rx_csum = True848586def main() -> None:87with NetDrvEpEnv(__file__, nsim_test=False) as cfg:88check_nic_features(cfg)8990cfg.bin_local = cfg.net_lib_dir / "csum"91cfg.bin_remote = cfg.remote.deploy(cfg.bin_local)9293cases = []94for ipver in ["4", "6"]:95cases.append(test_builder("rx_tcp", cfg, ipver, False, "-t"))96cases.append(test_builder("rx_tcp_invalid", cfg, ipver, False, "-t -E"))9798cases.append(test_builder("rx_udp", cfg, ipver, False, ""))99cases.append(test_builder("rx_udp_invalid", cfg, ipver, False, "-E"))100101cases.append(test_builder("tx_udp_csum_offload", cfg, ipver, True, "-U"))102cases.append(test_builder("tx_udp_zero_checksum", cfg, ipver, True, "-U -Z"))103104ksft_run(cases=cases, args=(cfg, ))105ksft_exit()106107108if __name__ == "__main__":109main()110111112