Skip to content

Commit

Permalink
Merge branch 'selftests-drv-net-add-a-simple-tso-test'
Browse files Browse the repository at this point in the history
Jakub Kicinski says:

====================
selftests: drv-net: add a simple TSO test

Add a simple test for exercising TSO over tunnels.

Similarly to csum test we want to iterate over ip versions.
Rework how addresses are stored in env to make this easier.
====================

Link: https://patch.msgid.link/[email protected]
Signed-off-by: Jakub Kicinski <[email protected]>
  • Loading branch information
kuba-moo committed Feb 20, 2025
2 parents 22af030 + 0d0f417 commit 6718198
Show file tree
Hide file tree
Showing 6 changed files with 305 additions and 61 deletions.
1 change: 1 addition & 0 deletions tools/testing/selftests/drivers/net/hw/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ TEST_PROGS = \
nic_performance.py \
pp_alloc_fail.py \
rss_ctx.py \
tso.py \
#

TEST_FILES := \
Expand Down
48 changes: 18 additions & 30 deletions tools/testing/selftests/drivers/net/hw/csum.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@
from lib.py import EthtoolFamily, NetDrvEpEnv
from lib.py import bkg, cmd, wait_port_listen

def test_receive(cfg, ipv4=False, extra_args=None):
def test_receive(cfg, ipver="6", extra_args=None):
"""Test local nic checksum receive. Remote host sends crafted packets."""
if not cfg.have_rx_csum:
raise KsftSkipEx(f"Test requires rx checksum offload on {cfg.ifname}")

if ipv4:
ip_args = f"-4 -S {cfg.remote_v4} -D {cfg.v4}"
else:
ip_args = f"-6 -S {cfg.remote_v6} -D {cfg.v6}"
ip_args = f"-{ipver} -S {cfg.remote_addr_v[ipver]} -D {cfg.addr_v[ipver]}"

rx_cmd = f"{cfg.bin_local} -i {cfg.ifname} -n 100 {ip_args} -r 1 -R {extra_args}"
tx_cmd = f"{cfg.bin_remote} -i {cfg.ifname} -n 100 {ip_args} -r 1 -T {extra_args}"
Expand All @@ -27,17 +24,14 @@ def test_receive(cfg, ipv4=False, extra_args=None):
cmd(tx_cmd, host=cfg.remote)


def test_transmit(cfg, ipv4=False, extra_args=None):
def test_transmit(cfg, ipver="6", extra_args=None):
"""Test local nic checksum transmit. Remote host verifies packets."""
if (not cfg.have_tx_csum_generic and
not (cfg.have_tx_csum_ipv4 and ipv4) and
not (cfg.have_tx_csum_ipv6 and not ipv4)):
not (cfg.have_tx_csum_ipv4 and ipver == "4") and
not (cfg.have_tx_csum_ipv6 and ipver == "6")):
raise KsftSkipEx(f"Test requires tx checksum offload on {cfg.ifname}")

if ipv4:
ip_args = f"-4 -S {cfg.v4} -D {cfg.remote_v4}"
else:
ip_args = f"-6 -S {cfg.v6} -D {cfg.remote_v6}"
ip_args = f"-{ipver} -S {cfg.addr_v[ipver]} -D {cfg.remote_addr_v[ipver]}"

# Cannot randomize input when calculating zero checksum
if extra_args != "-U -Z":
Expand All @@ -51,26 +45,20 @@ def test_transmit(cfg, ipv4=False, extra_args=None):
cmd(tx_cmd)


def test_builder(name, cfg, ipv4=False, tx=False, extra_args=""):
def test_builder(name, cfg, ipver="6", tx=False, extra_args=""):
"""Construct specific tests from the common template.
Most tests follow the same basic pattern, differing only in
Direction of the test and optional flags passed to csum."""
def f(cfg):
if ipv4:
cfg.require_v4()
else:
cfg.require_v6()
cfg.require_ipver(ipver)

if tx:
test_transmit(cfg, ipv4, extra_args)
test_transmit(cfg, ipver, extra_args)
else:
test_receive(cfg, ipv4, extra_args)
test_receive(cfg, ipver, extra_args)

if ipv4:
f.__name__ = "ipv4_" + name
else:
f.__name__ = "ipv6_" + name
f.__name__ = f"ipv{ipver}_" + name
return f


Expand Down Expand Up @@ -104,15 +92,15 @@ def main() -> None:
cfg.bin_remote = cfg.remote.deploy(cfg.bin_local)

cases = []
for ipv4 in [True, False]:
cases.append(test_builder("rx_tcp", cfg, ipv4, False, "-t"))
cases.append(test_builder("rx_tcp_invalid", cfg, ipv4, False, "-t -E"))
for ipver in ["4", "6"]:
cases.append(test_builder("rx_tcp", cfg, ipver, False, "-t"))
cases.append(test_builder("rx_tcp_invalid", cfg, ipver, False, "-t -E"))

cases.append(test_builder("rx_udp", cfg, ipv4, False, ""))
cases.append(test_builder("rx_udp_invalid", cfg, ipv4, False, "-E"))
cases.append(test_builder("rx_udp", cfg, ipver, False, ""))
cases.append(test_builder("rx_udp_invalid", cfg, ipver, False, "-E"))

cases.append(test_builder("tx_udp_csum_offload", cfg, ipv4, True, "-U"))
cases.append(test_builder("tx_udp_zero_checksum", cfg, ipv4, True, "-U -Z"))
cases.append(test_builder("tx_udp_csum_offload", cfg, ipver, True, "-U"))
cases.append(test_builder("tx_udp_zero_checksum", cfg, ipver, True, "-U -Z"))

ksft_run(cases=cases, args=(cfg, ))
ksft_exit()
Expand Down
6 changes: 3 additions & 3 deletions tools/testing/selftests/drivers/net/hw/devmem.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ def require_devmem(cfg):

@ksft_disruptive
def check_rx(cfg) -> None:
cfg.require_v6()
cfg.require_ipver("6")
require_devmem(cfg)

port = rand_port()
listen_cmd = f"./ncdevmem -l -f {cfg.ifname} -s {cfg.v6} -p {port}"
listen_cmd = f"./ncdevmem -l -f {cfg.ifname} -s {cfg.addr_v['6']} -p {port}"

with bkg(listen_cmd) as socat:
wait_port_listen(port)
cmd(f"echo -e \"hello\\nworld\"| socat -u - TCP6:[{cfg.v6}]:{port}", host=cfg.remote, shell=True)
cmd(f"echo -e \"hello\\nworld\"| socat -u - TCP6:[{cfg.addr_v['6']}]:{port}", host=cfg.remote, shell=True)

ksft_eq(socat.stdout.strip(), "hello\nworld")

Expand Down
241 changes: 241 additions & 0 deletions tools/testing/selftests/drivers/net/hw/tso.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0

"""Run the tools/testing/selftests/net/csum testsuite."""

import fcntl
import socket
import struct
import termios
import time

from lib.py import ksft_pr, ksft_run, ksft_exit, KsftSkipEx, KsftXfailEx
from lib.py import ksft_eq, ksft_ge, ksft_lt
from lib.py import EthtoolFamily, NetdevFamily, NetDrvEpEnv
from lib.py import bkg, cmd, defer, ethtool, ip, rand_port, wait_port_listen


def sock_wait_drain(sock, max_wait=1000):
"""Wait for all pending write data on the socket to get ACKed."""
for _ in range(max_wait):
one = b'\0' * 4
outq = fcntl.ioctl(sock.fileno(), termios.TIOCOUTQ, one)
outq = struct.unpack("I", outq)[0]
if outq == 0:
break
time.sleep(0.01)
ksft_eq(outq, 0)


def tcp_sock_get_retrans(sock):
"""Get the number of retransmissions for the TCP socket."""
info = sock.getsockopt(socket.SOL_TCP, socket.TCP_INFO, 512)
return struct.unpack("I", info[100:104])[0]


def run_one_stream(cfg, ipver, remote_v4, remote_v6, should_lso):
cfg.require_cmd("socat", remote=True)

port = rand_port()
listen_cmd = f"socat -{ipver} -t 2 -u TCP-LISTEN:{port},reuseport /dev/null,ignoreeof"

with bkg(listen_cmd, host=cfg.remote) as nc:
wait_port_listen(port, host=cfg.remote)

if ipver == "4":
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((remote_v4, port))
else:
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
sock.connect((remote_v6, port))

# Small send to make sure the connection is working.
sock.send("ping".encode())
sock_wait_drain(sock)

# Send 4MB of data, record the LSO packet count.
qstat_old = cfg.netnl.qstats_get({"ifindex": cfg.ifindex}, dump=True)[0]
buf = b"0" * 1024 * 1024 * 4
sock.send(buf)
sock_wait_drain(sock)
qstat_new = cfg.netnl.qstats_get({"ifindex": cfg.ifindex}, dump=True)[0]

# No math behind the 10 here, but try to catch cases where
# TCP falls back to non-LSO.
ksft_lt(tcp_sock_get_retrans(sock), 10)
sock.close()

# Check that at least 90% of the data was sent as LSO packets.
# System noise may cause false negatives. Also header overheads
# will add up to 5% of extra packes... The check is best effort.
total_lso_wire = len(buf) * 0.90 // cfg.dev["mtu"]
total_lso_super = len(buf) * 0.90 // cfg.dev["tso_max_size"]
if should_lso:
if cfg.have_stat_super_count:
ksft_ge(qstat_new['tx-hw-gso-packets'] -
qstat_old['tx-hw-gso-packets'],
total_lso_super,
comment="Number of LSO super-packets with LSO enabled")
if cfg.have_stat_wire_count:
ksft_ge(qstat_new['tx-hw-gso-wire-packets'] -
qstat_old['tx-hw-gso-wire-packets'],
total_lso_wire,
comment="Number of LSO wire-packets with LSO enabled")
else:
if cfg.have_stat_super_count:
ksft_lt(qstat_new['tx-hw-gso-packets'] -
qstat_old['tx-hw-gso-packets'],
15, comment="Number of LSO super-packets with LSO disabled")
if cfg.have_stat_wire_count:
ksft_lt(qstat_new['tx-hw-gso-wire-packets'] -
qstat_old['tx-hw-gso-wire-packets'],
500, comment="Number of LSO wire-packets with LSO disabled")


def build_tunnel(cfg, outer_ipver, tun_info):
local_v4 = NetDrvEpEnv.nsim_v4_pfx + "1"
local_v6 = NetDrvEpEnv.nsim_v6_pfx + "1"
remote_v4 = NetDrvEpEnv.nsim_v4_pfx + "2"
remote_v6 = NetDrvEpEnv.nsim_v6_pfx + "2"

local_addr = cfg.addr_v[outer_ipver]
remote_addr = cfg.remote_addr_v[outer_ipver]

tun_type = tun_info[0]
tun_arg = tun_info[2]
ip(f"link add {tun_type}-ksft type {tun_type} {tun_arg} local {local_addr} remote {remote_addr} dev {cfg.ifname}")
defer(ip, f"link del {tun_type}-ksft")
ip(f"link set dev {tun_type}-ksft up")
ip(f"addr add {local_v4}/24 dev {tun_type}-ksft")
ip(f"addr add {local_v6}/64 dev {tun_type}-ksft")

ip(f"link add {tun_type}-ksft type {tun_type} {tun_arg} local {remote_addr} remote {local_addr} dev {cfg.remote_ifname}",
host=cfg.remote)
defer(ip, f"link del {tun_type}-ksft", host=cfg.remote)
ip(f"link set dev {tun_type}-ksft up", host=cfg.remote)
ip(f"addr add {remote_v4}/24 dev {tun_type}-ksft", host=cfg.remote)
ip(f"addr add {remote_v6}/64 dev {tun_type}-ksft", host=cfg.remote)

return remote_v4, remote_v6


def test_builder(name, cfg, outer_ipver, feature, tun=None, inner_ipver=None):
"""Construct specific tests from the common template."""
def f(cfg):
cfg.require_ipver(outer_ipver)

if not cfg.have_stat_super_count and \
not cfg.have_stat_wire_count:
raise KsftSkipEx(f"Device does not support LSO queue stats")

ipver = outer_ipver
if tun:
remote_v4, remote_v6 = build_tunnel(cfg, ipver, tun)
ipver = inner_ipver
else:
remote_v4 = cfg.remote_addr_v["4"]
remote_v6 = cfg.remote_addr_v["6"]

tun_partial = tun and tun[1]
# Tunnel which can silently fall back to gso-partial
has_gso_partial = tun and 'tx-gso-partial' in cfg.features

# For TSO4 via partial we need mangleid
if ipver == "4" and feature in cfg.partial_features:
ksft_pr("Testing with mangleid enabled")
if 'tx-tcp-mangleid-segmentation' not in cfg.features:
ethtool(f"-K {cfg.ifname} tx-tcp-mangleid-segmentation on")
defer(ethtool, f"-K {cfg.ifname} tx-tcp-mangleid-segmentation off")

# First test without the feature enabled.
ethtool(f"-K {cfg.ifname} {feature} off")
if has_gso_partial:
ethtool(f"-K {cfg.ifname} tx-gso-partial off")
run_one_stream(cfg, ipver, remote_v4, remote_v6, should_lso=False)

# Now test with the feature enabled.
# For compatible tunnels only - just GSO partial, not specific feature.
if has_gso_partial:
ethtool(f"-K {cfg.ifname} tx-gso-partial on")
run_one_stream(cfg, ipver, remote_v4, remote_v6,
should_lso=tun_partial)

# Full feature enabled.
if feature in cfg.features:
ethtool(f"-K {cfg.ifname} {feature} on")
run_one_stream(cfg, ipver, remote_v4, remote_v6, should_lso=True)
else:
raise KsftXfailEx(f"Device does not support {feature}")

f.__name__ = name + ((outer_ipver + "_") if tun else "") + "ipv" + inner_ipver
return f


def query_nic_features(cfg) -> None:
"""Query and cache the NIC features."""
cfg.have_stat_super_count = False
cfg.have_stat_wire_count = False

cfg.features = set()
features = cfg.ethnl.features_get({"header": {"dev-index": cfg.ifindex}})
for f in features["active"]["bits"]["bit"]:
cfg.features.add(f["name"])

# Check which features are supported via GSO partial
cfg.partial_features = set()
if 'tx-gso-partial' in cfg.features:
ethtool(f"-K {cfg.ifname} tx-gso-partial off")

no_partial = set()
features = cfg.ethnl.features_get({"header": {"dev-index": cfg.ifindex}})
for f in features["active"]["bits"]["bit"]:
no_partial.add(f["name"])
cfg.partial_features = cfg.features - no_partial
ethtool(f"-K {cfg.ifname} tx-gso-partial on")

stats = cfg.netnl.qstats_get({"ifindex": cfg.ifindex}, dump=True)
if stats:
if 'tx-hw-gso-packets' in stats[0]:
ksft_pr("Detected qstat for LSO super-packets")
cfg.have_stat_super_count = True
if 'tx-hw-gso-wire-packets' in stats[0]:
ksft_pr("Detected qstat for LSO wire-packets")
cfg.have_stat_wire_count = True


def main() -> None:
with NetDrvEpEnv(__file__, nsim_test=False) as cfg:
cfg.ethnl = EthtoolFamily()
cfg.netnl = NetdevFamily()

query_nic_features(cfg)

test_info = (
# name, v4/v6 ethtool_feature tun:(type, partial, args)
("", "4", "tx-tcp-segmentation", None),
("", "6", "tx-tcp6-segmentation", None),
("vxlan", "", "tx-udp_tnl-segmentation", ("vxlan", True, "id 100 dstport 4789 noudpcsum")),
("vxlan_csum", "", "tx-udp_tnl-csum-segmentation", ("vxlan", False, "id 100 dstport 4789 udpcsum")),
("gre", "4", "tx-gre-segmentation", ("ipgre", False, "")),
("gre", "6", "tx-gre-segmentation", ("ip6gre", False, "")),
)

cases = []
for outer_ipver in ["4", "6"]:
for info in test_info:
# Skip if test which only works for a specific IP version
if info[1] and outer_ipver != info[1]:
continue

cases.append(test_builder(info[0], cfg, outer_ipver, info[2],
tun=info[3], inner_ipver="4"))
if info[3]:
cases.append(test_builder(info[0], cfg, outer_ipver, info[2],
tun=info[3], inner_ipver="6"))

ksft_run(cases=cases, args=(cfg, ))
ksft_exit()


if __name__ == "__main__":
main()
Loading

0 comments on commit 6718198

Please sign in to comment.