xref: /linux/tools/testing/selftests/drivers/net/hw/tso.py (revision 2c7e4a2663a1ab5a740c59c31991579b6b865a26)
1#!/usr/bin/env python3
2# SPDX-License-Identifier: GPL-2.0
3
4"""Run the tools/testing/selftests/net/csum testsuite."""
5
6import fcntl
7import socket
8import struct
9import termios
10import time
11
12from lib.py import ksft_pr, ksft_run, ksft_exit, KsftSkipEx, KsftXfailEx
13from lib.py import ksft_eq, ksft_ge, ksft_lt
14from lib.py import EthtoolFamily, NetdevFamily, NetDrvEpEnv
15from lib.py import bkg, cmd, defer, ethtool, ip, rand_port, wait_port_listen
16
17
18def sock_wait_drain(sock, max_wait=1000):
19    """Wait for all pending write data on the socket to get ACKed."""
20    for _ in range(max_wait):
21        one = b'\0' * 4
22        outq = fcntl.ioctl(sock.fileno(), termios.TIOCOUTQ, one)
23        outq = struct.unpack("I", outq)[0]
24        if outq == 0:
25            break
26        time.sleep(0.01)
27    ksft_eq(outq, 0)
28
29
30def tcp_sock_get_retrans(sock):
31    """Get the number of retransmissions for the TCP socket."""
32    info = sock.getsockopt(socket.SOL_TCP, socket.TCP_INFO, 512)
33    return struct.unpack("I", info[100:104])[0]
34
35
36def run_one_stream(cfg, ipver, remote_v4, remote_v6, should_lso):
37    cfg.require_cmd("socat", remote=True)
38
39    port = rand_port()
40    listen_cmd = f"socat -{ipver} -t 2 -u TCP-LISTEN:{port},reuseport /dev/null,ignoreeof"
41
42    with bkg(listen_cmd, host=cfg.remote, exit_wait=True) as nc:
43        wait_port_listen(port, host=cfg.remote)
44
45        if ipver == "4":
46            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
47            sock.connect((remote_v4, port))
48        else:
49            sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
50            sock.connect((remote_v6, port))
51
52        # Small send to make sure the connection is working.
53        sock.send("ping".encode())
54        sock_wait_drain(sock)
55
56        # Send 4MB of data, record the LSO packet count.
57        qstat_old = cfg.netnl.qstats_get({"ifindex": cfg.ifindex}, dump=True)[0]
58        buf = b"0" * 1024 * 1024 * 4
59        sock.send(buf)
60        sock_wait_drain(sock)
61        qstat_new = cfg.netnl.qstats_get({"ifindex": cfg.ifindex}, dump=True)[0]
62
63        # No math behind the 10 here, but try to catch cases where
64        # TCP falls back to non-LSO.
65        ksft_lt(tcp_sock_get_retrans(sock), 10)
66        sock.close()
67
68        # Check that at least 90% of the data was sent as LSO packets.
69        # System noise may cause false negatives. Also header overheads
70        # will add up to 5% of extra packes... The check is best effort.
71        total_lso_wire  = len(buf) * 0.90 // cfg.dev["mtu"]
72        total_lso_super = len(buf) * 0.90 // cfg.dev["tso_max_size"]
73        if should_lso:
74            if cfg.have_stat_super_count:
75                ksft_ge(qstat_new['tx-hw-gso-packets'] -
76                        qstat_old['tx-hw-gso-packets'],
77                        total_lso_super,
78                        comment="Number of LSO super-packets with LSO enabled")
79            if cfg.have_stat_wire_count:
80                ksft_ge(qstat_new['tx-hw-gso-wire-packets'] -
81                        qstat_old['tx-hw-gso-wire-packets'],
82                        total_lso_wire,
83                        comment="Number of LSO wire-packets with LSO enabled")
84        else:
85            if cfg.have_stat_super_count:
86                ksft_lt(qstat_new['tx-hw-gso-packets'] -
87                        qstat_old['tx-hw-gso-packets'],
88                        15, comment="Number of LSO super-packets with LSO disabled")
89            if cfg.have_stat_wire_count:
90                ksft_lt(qstat_new['tx-hw-gso-wire-packets'] -
91                        qstat_old['tx-hw-gso-wire-packets'],
92                        500, comment="Number of LSO wire-packets with LSO disabled")
93
94
95def build_tunnel(cfg, outer_ipver, tun_info):
96    local_v4  = NetDrvEpEnv.nsim_v4_pfx + "1"
97    local_v6  = NetDrvEpEnv.nsim_v6_pfx + "1"
98    remote_v4 = NetDrvEpEnv.nsim_v4_pfx + "2"
99    remote_v6 = NetDrvEpEnv.nsim_v6_pfx + "2"
100
101    local_addr  = cfg.addr_v[outer_ipver]
102    remote_addr = cfg.remote_addr_v[outer_ipver]
103
104    tun_type = tun_info[0]
105    tun_arg  = tun_info[2]
106    ip(f"link add {tun_type}-ksft type {tun_type} {tun_arg} local {local_addr} remote {remote_addr} dev {cfg.ifname}")
107    defer(ip, f"link del {tun_type}-ksft")
108    ip(f"link set dev {tun_type}-ksft up")
109    ip(f"addr add {local_v4}/24 dev {tun_type}-ksft")
110    ip(f"addr add {local_v6}/64 dev {tun_type}-ksft")
111
112    ip(f"link add {tun_type}-ksft type {tun_type} {tun_arg} local {remote_addr} remote {local_addr} dev {cfg.remote_ifname}",
113        host=cfg.remote)
114    defer(ip, f"link del {tun_type}-ksft", host=cfg.remote)
115    ip(f"link set dev {tun_type}-ksft up", host=cfg.remote)
116    ip(f"addr add {remote_v4}/24 dev {tun_type}-ksft", host=cfg.remote)
117    ip(f"addr add {remote_v6}/64 dev {tun_type}-ksft", host=cfg.remote)
118
119    return remote_v4, remote_v6
120
121
122def test_builder(name, cfg, outer_ipver, feature, tun=None, inner_ipver=None):
123    """Construct specific tests from the common template."""
124    def f(cfg):
125        cfg.require_ipver(outer_ipver)
126
127        if not cfg.have_stat_super_count and \
128           not cfg.have_stat_wire_count:
129            raise KsftSkipEx(f"Device does not support LSO queue stats")
130
131        ipver = outer_ipver
132        if tun:
133            remote_v4, remote_v6 = build_tunnel(cfg, ipver, tun)
134            ipver = inner_ipver
135        else:
136            remote_v4 = cfg.remote_addr_v["4"]
137            remote_v6 = cfg.remote_addr_v["6"]
138
139        tun_partial = tun and tun[1]
140        # Tunnel which can silently fall back to gso-partial
141        has_gso_partial = tun and 'tx-gso-partial' in cfg.features
142
143        # For TSO4 via partial we need mangleid
144        if ipver == "4" and feature in cfg.partial_features:
145            ksft_pr("Testing with mangleid enabled")
146            if 'tx-tcp-mangleid-segmentation' not in cfg.features:
147                ethtool(f"-K {cfg.ifname} tx-tcp-mangleid-segmentation on")
148                defer(ethtool, f"-K {cfg.ifname} tx-tcp-mangleid-segmentation off")
149
150        # First test without the feature enabled.
151        ethtool(f"-K {cfg.ifname} {feature} off")
152        if has_gso_partial:
153            ethtool(f"-K {cfg.ifname} tx-gso-partial off")
154        run_one_stream(cfg, ipver, remote_v4, remote_v6, should_lso=False)
155
156        # Now test with the feature enabled.
157        # For compatible tunnels only - just GSO partial, not specific feature.
158        if has_gso_partial:
159            ethtool(f"-K {cfg.ifname} tx-gso-partial on")
160            run_one_stream(cfg, ipver, remote_v4, remote_v6,
161                           should_lso=tun_partial)
162
163        # Full feature enabled.
164        if feature in cfg.features:
165            ethtool(f"-K {cfg.ifname} {feature} on")
166            run_one_stream(cfg, ipver, remote_v4, remote_v6, should_lso=True)
167        else:
168            raise KsftXfailEx(f"Device does not support {feature}")
169
170    f.__name__ = name + ((outer_ipver + "_") if tun else "") + "ipv" + inner_ipver
171    return f
172
173
174def query_nic_features(cfg) -> None:
175    """Query and cache the NIC features."""
176    cfg.have_stat_super_count = False
177    cfg.have_stat_wire_count = False
178
179    cfg.features = set()
180    features = cfg.ethnl.features_get({"header": {"dev-index": cfg.ifindex}})
181    for f in features["active"]["bits"]["bit"]:
182        cfg.features.add(f["name"])
183
184    # Check which features are supported via GSO partial
185    cfg.partial_features = set()
186    if 'tx-gso-partial' in cfg.features:
187        ethtool(f"-K {cfg.ifname} tx-gso-partial off")
188
189        no_partial = set()
190        features = cfg.ethnl.features_get({"header": {"dev-index": cfg.ifindex}})
191        for f in features["active"]["bits"]["bit"]:
192            no_partial.add(f["name"])
193        cfg.partial_features = cfg.features - no_partial
194        ethtool(f"-K {cfg.ifname} tx-gso-partial on")
195
196    stats = cfg.netnl.qstats_get({"ifindex": cfg.ifindex}, dump=True)
197    if stats:
198        if 'tx-hw-gso-packets' in stats[0]:
199            ksft_pr("Detected qstat for LSO super-packets")
200            cfg.have_stat_super_count = True
201        if 'tx-hw-gso-wire-packets' in stats[0]:
202            ksft_pr("Detected qstat for LSO wire-packets")
203            cfg.have_stat_wire_count = True
204
205
206def main() -> None:
207    with NetDrvEpEnv(__file__, nsim_test=False) as cfg:
208        cfg.ethnl = EthtoolFamily()
209        cfg.netnl = NetdevFamily()
210
211        query_nic_features(cfg)
212
213        test_info = (
214            # name,       v4/v6  ethtool_feature              tun:(type,    partial, args)
215            ("",            "4", "tx-tcp-segmentation",           None),
216            ("",            "6", "tx-tcp6-segmentation",          None),
217            ("vxlan",        "", "tx-udp_tnl-segmentation",       ("vxlan",  True,  "id 100 dstport 4789 noudpcsum")),
218            ("vxlan_csum",   "", "tx-udp_tnl-csum-segmentation",  ("vxlan",  False, "id 100 dstport 4789 udpcsum")),
219            ("gre",         "4", "tx-gre-segmentation",           ("gre",    False,  "")),
220            ("gre",         "6", "tx-gre-segmentation",           ("ip6gre", False,  "")),
221        )
222
223        cases = []
224        for outer_ipver in ["4", "6"]:
225            for info in test_info:
226                # Skip if test which only works for a specific IP version
227                if info[1] and outer_ipver != info[1]:
228                    continue
229
230                cases.append(test_builder(info[0], cfg, outer_ipver, info[2],
231                                          tun=info[3], inner_ipver="4"))
232                if info[3]:
233                    cases.append(test_builder(info[0], cfg, outer_ipver, info[2],
234                                              tun=info[3], inner_ipver="6"))
235
236        ksft_run(cases=cases, args=(cfg, ))
237    ksft_exit()
238
239
240if __name__ == "__main__":
241    main()
242