xref: /linux/tools/testing/selftests/drivers/net/hw/tso.py (revision 6698d6ce6a6b20d8780ade93b9b689787b5a4945)
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", local=False, remote=True)
38
39    # Set recv window clamp to avoid overwhelming receiver on debug kernels
40    # the 200k clamp should still let use reach > 15Gbps on real HW
41    port = rand_port()
42    listen_opts = f"{port},reuseport,tcp-window-clamp=200000"
43    listen_cmd = f"socat -{ipver} -t 2 -u TCP-LISTEN:{listen_opts} /dev/null,ignoreeof"
44
45    with bkg(listen_cmd, host=cfg.remote, exit_wait=True) as nc:
46        wait_port_listen(port, host=cfg.remote)
47
48        if ipver == "4":
49            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
50            sock.connect((remote_v4, port))
51        else:
52            sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
53            sock.connect((remote_v6, port))
54
55        # Small send to make sure the connection is working.
56        sock.send("ping".encode())
57        sock_wait_drain(sock)
58
59        # Send 4MB of data, record the LSO packet count.
60        qstat_old = cfg.netnl.qstats_get({"ifindex": cfg.ifindex}, dump=True)[0]
61        buf = b"0" * 1024 * 1024 * 4
62        sock.send(buf)
63        sock_wait_drain(sock)
64        qstat_new = cfg.netnl.qstats_get({"ifindex": cfg.ifindex}, dump=True)[0]
65
66        # Check that at least 90% of the data was sent as LSO packets.
67        # System noise may cause false negatives. Also header overheads
68        # will add up to 5% of extra packes... The check is best effort.
69        total_lso_wire  = len(buf) * 0.90 // cfg.dev["mtu"]
70        total_lso_super = len(buf) * 0.90 // cfg.dev["tso_max_size"]
71
72        # Make sure we have order of magnitude more LSO packets than
73        # retransmits, in case TCP retransmitted all the LSO packets.
74        ksft_lt(tcp_sock_get_retrans(sock), total_lso_wire / 16)
75        sock.close()
76
77        if should_lso:
78            if cfg.have_stat_super_count:
79                ksft_ge(qstat_new['tx-hw-gso-packets'] -
80                        qstat_old['tx-hw-gso-packets'],
81                        total_lso_super,
82                        comment="Number of LSO super-packets with LSO enabled")
83            if cfg.have_stat_wire_count:
84                ksft_ge(qstat_new['tx-hw-gso-wire-packets'] -
85                        qstat_old['tx-hw-gso-wire-packets'],
86                        total_lso_wire,
87                        comment="Number of LSO wire-packets with LSO enabled")
88        else:
89            if cfg.have_stat_super_count:
90                ksft_lt(qstat_new['tx-hw-gso-packets'] -
91                        qstat_old['tx-hw-gso-packets'],
92                        15, comment="Number of LSO super-packets with LSO disabled")
93            if cfg.have_stat_wire_count:
94                ksft_lt(qstat_new['tx-hw-gso-wire-packets'] -
95                        qstat_old['tx-hw-gso-wire-packets'],
96                        500, comment="Number of LSO wire-packets with LSO disabled")
97
98
99def build_tunnel(cfg, outer_ipver, tun_info):
100    local_v4  = NetDrvEpEnv.nsim_v4_pfx + "1"
101    local_v6  = NetDrvEpEnv.nsim_v6_pfx + "1"
102    remote_v4 = NetDrvEpEnv.nsim_v4_pfx + "2"
103    remote_v6 = NetDrvEpEnv.nsim_v6_pfx + "2"
104
105    local_addr  = cfg.addr_v[outer_ipver]
106    remote_addr = cfg.remote_addr_v[outer_ipver]
107
108    tun_type = tun_info[0]
109    tun_arg  = tun_info[1]
110    ip(f"link add {tun_type}-ksft type {tun_type} {tun_arg} local {local_addr} remote {remote_addr} dev {cfg.ifname}")
111    defer(ip, f"link del {tun_type}-ksft")
112    ip(f"link set dev {tun_type}-ksft up")
113    ip(f"addr add {local_v4}/24 dev {tun_type}-ksft")
114    ip(f"addr add {local_v6}/64 dev {tun_type}-ksft")
115
116    ip(f"link add {tun_type}-ksft type {tun_type} {tun_arg} local {remote_addr} remote {local_addr} dev {cfg.remote_ifname}",
117        host=cfg.remote)
118    defer(ip, f"link del {tun_type}-ksft", host=cfg.remote)
119    ip(f"link set dev {tun_type}-ksft up", host=cfg.remote)
120    ip(f"addr add {remote_v4}/24 dev {tun_type}-ksft", host=cfg.remote)
121    ip(f"addr add {remote_v6}/64 dev {tun_type}-ksft", host=cfg.remote)
122
123    return remote_v4, remote_v6
124
125
126def restore_wanted_features(cfg):
127    features_cmd = ""
128    for feature in cfg.hw_features:
129        setting = "on" if feature in cfg.wanted_features else "off"
130        features_cmd += f" {feature} {setting}"
131    try:
132        ethtool(f"-K {cfg.ifname} {features_cmd}")
133    except Exception as e:
134        ksft_pr(f"WARNING: failure restoring wanted features: {e}")
135
136
137def test_builder(name, cfg, outer_ipver, feature, tun=None, inner_ipver=None):
138    """Construct specific tests from the common template."""
139    def f(cfg):
140        cfg.require_ipver(outer_ipver)
141        defer(restore_wanted_features, cfg)
142
143        if not cfg.have_stat_super_count and \
144           not cfg.have_stat_wire_count:
145            raise KsftSkipEx(f"Device does not support LSO queue stats")
146
147        if feature not in cfg.hw_features:
148            raise KsftSkipEx(f"Device does not support {feature}")
149
150        ipver = outer_ipver
151        if tun:
152            remote_v4, remote_v6 = build_tunnel(cfg, ipver, tun)
153            ipver = inner_ipver
154        else:
155            remote_v4 = cfg.remote_addr_v["4"]
156            remote_v6 = cfg.remote_addr_v["6"]
157
158        # First test without the feature enabled.
159        ethtool(f"-K {cfg.ifname} {feature} off")
160        run_one_stream(cfg, ipver, remote_v4, remote_v6, should_lso=False)
161
162        ethtool(f"-K {cfg.ifname} tx-gso-partial off")
163        ethtool(f"-K {cfg.ifname} tx-tcp-mangleid-segmentation off")
164        if feature in cfg.partial_features:
165            ethtool(f"-K {cfg.ifname} tx-gso-partial on")
166            if ipver == "4":
167                ksft_pr("Testing with mangleid enabled")
168                ethtool(f"-K {cfg.ifname} tx-tcp-mangleid-segmentation on")
169
170        # Full feature enabled.
171        ethtool(f"-K {cfg.ifname} {feature} on")
172        run_one_stream(cfg, ipver, remote_v4, remote_v6, should_lso=True)
173
174    f.__name__ = name + ((outer_ipver + "_") if tun else "") + "ipv" + inner_ipver
175    return f
176
177
178def query_nic_features(cfg) -> None:
179    """Query and cache the NIC features."""
180    cfg.have_stat_super_count = False
181    cfg.have_stat_wire_count = False
182
183    features = cfg.ethnl.features_get({"header": {"dev-index": cfg.ifindex}})
184
185    cfg.wanted_features = set()
186    for f in features["wanted"]["bits"]["bit"]:
187        cfg.wanted_features.add(f["name"])
188
189    cfg.hw_features = set()
190    hw_all_features_cmd = ""
191    for f in features["hw"]["bits"]["bit"]:
192        if f.get("value", False):
193            feature = f["name"]
194            cfg.hw_features.add(feature)
195            hw_all_features_cmd += f" {feature} on"
196    try:
197        ethtool(f"-K {cfg.ifname} {hw_all_features_cmd}")
198    except Exception as e:
199        ksft_pr(f"WARNING: failure enabling all hw features: {e}")
200        ksft_pr("partial gso feature detection may be impacted")
201
202    # Check which features are supported via GSO partial
203    cfg.partial_features = set()
204    if 'tx-gso-partial' in cfg.hw_features:
205        ethtool(f"-K {cfg.ifname} tx-gso-partial off")
206
207        no_partial = set()
208        features = cfg.ethnl.features_get({"header": {"dev-index": cfg.ifindex}})
209        for f in features["active"]["bits"]["bit"]:
210            no_partial.add(f["name"])
211        cfg.partial_features = cfg.hw_features - no_partial
212        ethtool(f"-K {cfg.ifname} tx-gso-partial on")
213
214    restore_wanted_features(cfg)
215
216    stats = cfg.netnl.qstats_get({"ifindex": cfg.ifindex}, dump=True)
217    if stats:
218        if 'tx-hw-gso-packets' in stats[0]:
219            ksft_pr("Detected qstat for LSO super-packets")
220            cfg.have_stat_super_count = True
221        if 'tx-hw-gso-wire-packets' in stats[0]:
222            ksft_pr("Detected qstat for LSO wire-packets")
223            cfg.have_stat_wire_count = True
224
225
226def main() -> None:
227    with NetDrvEpEnv(__file__, nsim_test=False) as cfg:
228        cfg.ethnl = EthtoolFamily()
229        cfg.netnl = NetdevFamily()
230
231        query_nic_features(cfg)
232
233        test_info = (
234            # name,       v4/v6  ethtool_feature               tun:(type, args, inner ip versions)
235            ("",           "4", "tx-tcp-segmentation",         None),
236            ("",           "6", "tx-tcp6-segmentation",        None),
237            ("vxlan",      "4", "tx-udp_tnl-segmentation",     ("vxlan", "id 100 dstport 4789 noudpcsum", ("4", "6"))),
238            ("vxlan",      "6", "tx-udp_tnl-segmentation",     ("vxlan", "id 100 dstport 4789 udp6zerocsumtx udp6zerocsumrx", ("4", "6"))),
239            ("vxlan_csum", "", "tx-udp_tnl-csum-segmentation", ("vxlan", "id 100 dstport 4789 udpcsum", ("4", "6"))),
240            ("gre",        "4", "tx-gre-segmentation",         ("gre",   "", ("4", "6"))),
241            ("gre",        "6", "tx-gre-segmentation",         ("ip6gre","", ("4", "6"))),
242        )
243
244        cases = []
245        for outer_ipver in ["4", "6"]:
246            for info in test_info:
247                # Skip if test which only works for a specific IP version
248                if info[1] and outer_ipver != info[1]:
249                    continue
250
251                if info[3]:
252                    cases += [
253                        test_builder(info[0], cfg, outer_ipver, info[2], info[3], inner_ipver)
254                        for inner_ipver in info[3][2]
255                    ]
256                else:
257                    cases.append(test_builder(info[0], cfg, outer_ipver, info[2], None, outer_ipver))
258
259        ksft_run(cases=cases, args=(cfg, ))
260    ksft_exit()
261
262
263if __name__ == "__main__":
264    main()
265