Path: blob/master/tools/testing/selftests/drivers/net/hw/tso.py
26295 views
#!/usr/bin/env python31# SPDX-License-Identifier: GPL-2.023"""Run the tools/testing/selftests/net/csum testsuite."""45import fcntl6import socket7import struct8import termios9import time1011from lib.py import ksft_pr, ksft_run, ksft_exit, KsftSkipEx, KsftXfailEx12from lib.py import ksft_eq, ksft_ge, ksft_lt13from lib.py import EthtoolFamily, NetdevFamily, NetDrvEpEnv14from lib.py import bkg, cmd, defer, ethtool, ip, rand_port, wait_port_listen151617def sock_wait_drain(sock, max_wait=1000):18"""Wait for all pending write data on the socket to get ACKed."""19for _ in range(max_wait):20one = b'\0' * 421outq = fcntl.ioctl(sock.fileno(), termios.TIOCOUTQ, one)22outq = struct.unpack("I", outq)[0]23if outq == 0:24break25time.sleep(0.01)26ksft_eq(outq, 0)272829def tcp_sock_get_retrans(sock):30"""Get the number of retransmissions for the TCP socket."""31info = sock.getsockopt(socket.SOL_TCP, socket.TCP_INFO, 512)32return struct.unpack("I", info[100:104])[0]333435def run_one_stream(cfg, ipver, remote_v4, remote_v6, should_lso):36cfg.require_cmd("socat", local=False, remote=True)3738port = rand_port()39listen_cmd = f"socat -{ipver} -t 2 -u TCP-LISTEN:{port},reuseport /dev/null,ignoreeof"4041with bkg(listen_cmd, host=cfg.remote, exit_wait=True) as nc:42wait_port_listen(port, host=cfg.remote)4344if ipver == "4":45sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)46sock.connect((remote_v4, port))47else:48sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)49sock.connect((remote_v6, port))5051# Small send to make sure the connection is working.52sock.send("ping".encode())53sock_wait_drain(sock)5455# Send 4MB of data, record the LSO packet count.56qstat_old = cfg.netnl.qstats_get({"ifindex": cfg.ifindex}, dump=True)[0]57buf = b"0" * 1024 * 1024 * 458sock.send(buf)59sock_wait_drain(sock)60qstat_new = cfg.netnl.qstats_get({"ifindex": cfg.ifindex}, dump=True)[0]6162# No math behind the 10 here, but try to catch cases where63# TCP falls back to non-LSO.64ksft_lt(tcp_sock_get_retrans(sock), 10)65sock.close()6667# Check that at least 90% of the data was sent as LSO packets.68# System noise may cause false negatives. Also header overheads69# will add up to 5% of extra packes... The check is best effort.70total_lso_wire = len(buf) * 0.90 // cfg.dev["mtu"]71total_lso_super = len(buf) * 0.90 // cfg.dev["tso_max_size"]72if should_lso:73if cfg.have_stat_super_count:74ksft_ge(qstat_new['tx-hw-gso-packets'] -75qstat_old['tx-hw-gso-packets'],76total_lso_super,77comment="Number of LSO super-packets with LSO enabled")78if cfg.have_stat_wire_count:79ksft_ge(qstat_new['tx-hw-gso-wire-packets'] -80qstat_old['tx-hw-gso-wire-packets'],81total_lso_wire,82comment="Number of LSO wire-packets with LSO enabled")83else:84if cfg.have_stat_super_count:85ksft_lt(qstat_new['tx-hw-gso-packets'] -86qstat_old['tx-hw-gso-packets'],8715, comment="Number of LSO super-packets with LSO disabled")88if cfg.have_stat_wire_count:89ksft_lt(qstat_new['tx-hw-gso-wire-packets'] -90qstat_old['tx-hw-gso-wire-packets'],91500, comment="Number of LSO wire-packets with LSO disabled")929394def build_tunnel(cfg, outer_ipver, tun_info):95local_v4 = NetDrvEpEnv.nsim_v4_pfx + "1"96local_v6 = NetDrvEpEnv.nsim_v6_pfx + "1"97remote_v4 = NetDrvEpEnv.nsim_v4_pfx + "2"98remote_v6 = NetDrvEpEnv.nsim_v6_pfx + "2"99100local_addr = cfg.addr_v[outer_ipver]101remote_addr = cfg.remote_addr_v[outer_ipver]102103tun_type = tun_info[0]104tun_arg = tun_info[1]105ip(f"link add {tun_type}-ksft type {tun_type} {tun_arg} local {local_addr} remote {remote_addr} dev {cfg.ifname}")106defer(ip, f"link del {tun_type}-ksft")107ip(f"link set dev {tun_type}-ksft up")108ip(f"addr add {local_v4}/24 dev {tun_type}-ksft")109ip(f"addr add {local_v6}/64 dev {tun_type}-ksft")110111ip(f"link add {tun_type}-ksft type {tun_type} {tun_arg} local {remote_addr} remote {local_addr} dev {cfg.remote_ifname}",112host=cfg.remote)113defer(ip, f"link del {tun_type}-ksft", host=cfg.remote)114ip(f"link set dev {tun_type}-ksft up", host=cfg.remote)115ip(f"addr add {remote_v4}/24 dev {tun_type}-ksft", host=cfg.remote)116ip(f"addr add {remote_v6}/64 dev {tun_type}-ksft", host=cfg.remote)117118return remote_v4, remote_v6119120121def restore_wanted_features(cfg):122features_cmd = ""123for feature in cfg.hw_features:124setting = "on" if feature in cfg.wanted_features else "off"125features_cmd += f" {feature} {setting}"126try:127ethtool(f"-K {cfg.ifname} {features_cmd}")128except Exception as e:129ksft_pr(f"WARNING: failure restoring wanted features: {e}")130131132def test_builder(name, cfg, outer_ipver, feature, tun=None, inner_ipver=None):133"""Construct specific tests from the common template."""134def f(cfg):135cfg.require_ipver(outer_ipver)136defer(restore_wanted_features, cfg)137138if not cfg.have_stat_super_count and \139not cfg.have_stat_wire_count:140raise KsftSkipEx(f"Device does not support LSO queue stats")141142if feature not in cfg.hw_features:143raise KsftSkipEx(f"Device does not support {feature}")144145ipver = outer_ipver146if tun:147remote_v4, remote_v6 = build_tunnel(cfg, ipver, tun)148ipver = inner_ipver149else:150remote_v4 = cfg.remote_addr_v["4"]151remote_v6 = cfg.remote_addr_v["6"]152153# First test without the feature enabled.154ethtool(f"-K {cfg.ifname} {feature} off")155run_one_stream(cfg, ipver, remote_v4, remote_v6, should_lso=False)156157ethtool(f"-K {cfg.ifname} tx-gso-partial off")158ethtool(f"-K {cfg.ifname} tx-tcp-mangleid-segmentation off")159if feature in cfg.partial_features:160ethtool(f"-K {cfg.ifname} tx-gso-partial on")161if ipver == "4":162ksft_pr("Testing with mangleid enabled")163ethtool(f"-K {cfg.ifname} tx-tcp-mangleid-segmentation on")164165# Full feature enabled.166ethtool(f"-K {cfg.ifname} {feature} on")167run_one_stream(cfg, ipver, remote_v4, remote_v6, should_lso=True)168169f.__name__ = name + ((outer_ipver + "_") if tun else "") + "ipv" + inner_ipver170return f171172173def query_nic_features(cfg) -> None:174"""Query and cache the NIC features."""175cfg.have_stat_super_count = False176cfg.have_stat_wire_count = False177178features = cfg.ethnl.features_get({"header": {"dev-index": cfg.ifindex}})179180cfg.wanted_features = set()181for f in features["wanted"]["bits"]["bit"]:182cfg.wanted_features.add(f["name"])183184cfg.hw_features = set()185hw_all_features_cmd = ""186for f in features["hw"]["bits"]["bit"]:187if f.get("value", False):188feature = f["name"]189cfg.hw_features.add(feature)190hw_all_features_cmd += f" {feature} on"191try:192ethtool(f"-K {cfg.ifname} {hw_all_features_cmd}")193except Exception as e:194ksft_pr(f"WARNING: failure enabling all hw features: {e}")195ksft_pr("partial gso feature detection may be impacted")196197# Check which features are supported via GSO partial198cfg.partial_features = set()199if 'tx-gso-partial' in cfg.hw_features:200ethtool(f"-K {cfg.ifname} tx-gso-partial off")201202no_partial = set()203features = cfg.ethnl.features_get({"header": {"dev-index": cfg.ifindex}})204for f in features["active"]["bits"]["bit"]:205no_partial.add(f["name"])206cfg.partial_features = cfg.hw_features - no_partial207ethtool(f"-K {cfg.ifname} tx-gso-partial on")208209restore_wanted_features(cfg)210211stats = cfg.netnl.qstats_get({"ifindex": cfg.ifindex}, dump=True)212if stats:213if 'tx-hw-gso-packets' in stats[0]:214ksft_pr("Detected qstat for LSO super-packets")215cfg.have_stat_super_count = True216if 'tx-hw-gso-wire-packets' in stats[0]:217ksft_pr("Detected qstat for LSO wire-packets")218cfg.have_stat_wire_count = True219220221def main() -> None:222with NetDrvEpEnv(__file__, nsim_test=False) as cfg:223cfg.ethnl = EthtoolFamily()224cfg.netnl = NetdevFamily()225226query_nic_features(cfg)227228test_info = (229# name, v4/v6 ethtool_feature tun:(type, args, inner ip versions)230("", "4", "tx-tcp-segmentation", None),231("", "6", "tx-tcp6-segmentation", None),232("vxlan", "4", "tx-udp_tnl-segmentation", ("vxlan", "id 100 dstport 4789 noudpcsum", ("4", "6"))),233("vxlan", "6", "tx-udp_tnl-segmentation", ("vxlan", "id 100 dstport 4789 udp6zerocsumtx udp6zerocsumrx", ("4", "6"))),234("vxlan_csum", "", "tx-udp_tnl-csum-segmentation", ("vxlan", "id 100 dstport 4789 udpcsum", ("4", "6"))),235("gre", "4", "tx-gre-segmentation", ("gre", "", ("4", "6"))),236("gre", "6", "tx-gre-segmentation", ("ip6gre","", ("4", "6"))),237)238239cases = []240for outer_ipver in ["4", "6"]:241for info in test_info:242# Skip if test which only works for a specific IP version243if info[1] and outer_ipver != info[1]:244continue245246if info[3]:247cases += [248test_builder(info[0], cfg, outer_ipver, info[2], info[3], inner_ipver)249for inner_ipver in info[3][2]250]251else:252cases.append(test_builder(info[0], cfg, outer_ipver, info[2], None, outer_ipver))253254ksft_run(cases=cases, args=(cfg, ))255ksft_exit()256257258if __name__ == "__main__":259main()260261262