csum.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env python3
  2. # SPDX-License-Identifier: GPL-2.0
  3. """Run the tools/testing/selftests/net/csum testsuite."""
  4. from os import path
  5. from lib.py import ksft_run, ksft_exit, KsftSkipEx
  6. from lib.py import EthtoolFamily, NetDrvEpEnv
  7. from lib.py import bkg, cmd, wait_port_listen
  8. def test_receive(cfg, ipv4=False, extra_args=None):
  9. """Test local nic checksum receive. Remote host sends crafted packets."""
  10. if not cfg.have_rx_csum:
  11. raise KsftSkipEx(f"Test requires rx checksum offload on {cfg.ifname}")
  12. if ipv4:
  13. ip_args = f"-4 -S {cfg.remote_v4} -D {cfg.v4}"
  14. else:
  15. ip_args = f"-6 -S {cfg.remote_v6} -D {cfg.v6}"
  16. rx_cmd = f"{cfg.bin_local} -i {cfg.ifname} -n 100 {ip_args} -r 1 -R {extra_args}"
  17. tx_cmd = f"{cfg.bin_remote} -i {cfg.remote_ifname} -n 100 {ip_args} -r 1 -T {extra_args}"
  18. with bkg(rx_cmd, exit_wait=True):
  19. wait_port_listen(34000, proto="udp")
  20. cmd(tx_cmd, host=cfg.remote)
  21. def test_transmit(cfg, ipv4=False, extra_args=None):
  22. """Test local nic checksum transmit. Remote host verifies packets."""
  23. if (not cfg.have_tx_csum_generic and
  24. not (cfg.have_tx_csum_ipv4 and ipv4) and
  25. not (cfg.have_tx_csum_ipv6 and not ipv4)):
  26. raise KsftSkipEx(f"Test requires tx checksum offload on {cfg.ifname}")
  27. if ipv4:
  28. ip_args = f"-4 -S {cfg.v4} -D {cfg.remote_v4}"
  29. else:
  30. ip_args = f"-6 -S {cfg.v6} -D {cfg.remote_v6}"
  31. # Cannot randomize input when calculating zero checksum
  32. if extra_args != "-U -Z":
  33. extra_args += " -r 1"
  34. rx_cmd = f"{cfg.bin_remote} -i {cfg.remote_ifname} -L 1 -n 100 {ip_args} -R {extra_args}"
  35. tx_cmd = f"{cfg.bin_local} -i {cfg.ifname} -L 1 -n 100 {ip_args} -T {extra_args}"
  36. with bkg(rx_cmd, host=cfg.remote, exit_wait=True):
  37. wait_port_listen(34000, proto="udp", host=cfg.remote)
  38. cmd(tx_cmd)
  39. def test_builder(name, cfg, ipv4=False, tx=False, extra_args=""):
  40. """Construct specific tests from the common template.
  41. Most tests follow the same basic pattern, differing only in
  42. Direction of the test and optional flags passed to csum."""
  43. def f(cfg):
  44. if ipv4:
  45. cfg.require_v4()
  46. else:
  47. cfg.require_v6()
  48. if tx:
  49. test_transmit(cfg, ipv4, extra_args)
  50. else:
  51. test_receive(cfg, ipv4, extra_args)
  52. if ipv4:
  53. f.__name__ = "ipv4_" + name
  54. else:
  55. f.__name__ = "ipv6_" + name
  56. return f
  57. def check_nic_features(cfg) -> None:
  58. """Test whether Tx and Rx checksum offload are enabled.
  59. If the device under test has either off, then skip the relevant tests."""
  60. cfg.have_tx_csum_generic = False
  61. cfg.have_tx_csum_ipv4 = False
  62. cfg.have_tx_csum_ipv6 = False
  63. cfg.have_rx_csum = False
  64. ethnl = EthtoolFamily()
  65. features = ethnl.features_get({"header": {"dev-index": cfg.ifindex}})
  66. for f in features["active"]["bits"]["bit"]:
  67. if f["name"] == "tx-checksum-ip-generic":
  68. cfg.have_tx_csum_generic = True
  69. elif f["name"] == "tx-checksum-ipv4":
  70. cfg.have_tx_csum_ipv4 = True
  71. elif f["name"] == "tx-checksum-ipv6":
  72. cfg.have_tx_csum_ipv6 = True
  73. elif f["name"] == "rx-checksum":
  74. cfg.have_rx_csum = True
  75. def main() -> None:
  76. with NetDrvEpEnv(__file__, nsim_test=False) as cfg:
  77. check_nic_features(cfg)
  78. cfg.bin_local = path.abspath(path.dirname(__file__) + "/../../../net/lib/csum")
  79. cfg.bin_remote = cfg.remote.deploy(cfg.bin_local)
  80. cases = []
  81. for ipv4 in [True, False]:
  82. cases.append(test_builder("rx_tcp", cfg, ipv4, False, "-t"))
  83. cases.append(test_builder("rx_tcp_invalid", cfg, ipv4, False, "-t -E"))
  84. cases.append(test_builder("rx_udp", cfg, ipv4, False, ""))
  85. cases.append(test_builder("rx_udp_invalid", cfg, ipv4, False, "-E"))
  86. cases.append(test_builder("tx_udp_csum_offload", cfg, ipv4, True, "-U"))
  87. cases.append(test_builder("tx_udp_zero_checksum", cfg, ipv4, True, "-U -Z"))
  88. ksft_run(cases=cases, args=(cfg, ))
  89. ksft_exit()
  90. if __name__ == "__main__":
  91. main()