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    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[1]
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 restore_wanted_features(cfg):
123    features_cmd = ""
124    for feature in cfg.hw_features:
125        setting = "on" if feature in cfg.wanted_features else "off"
126        features_cmd += f" {feature} {setting}"
127    try:
128        ethtool(f"-K {cfg.ifname} {features_cmd}")
129    except Exception as e:
130        ksft_pr(f"WARNING: failure restoring wanted features: {e}")
131
132
133def test_builder(name, cfg, outer_ipver, feature, tun=None, inner_ipver=None):
134    """Construct specific tests from the common template."""
135    def f(cfg):
136        cfg.require_ipver(outer_ipver)
137        defer(restore_wanted_features, cfg)
138
139        if not cfg.have_stat_super_count and \
140           not cfg.have_stat_wire_count:
141            raise KsftSkipEx(f"Device does not support LSO queue stats")
142
143        if feature not in cfg.hw_features:
144            raise KsftSkipEx(f"Device does not support {feature}")
145
146        ipver = outer_ipver
147        if tun:
148            remote_v4, remote_v6 = build_tunnel(cfg, ipver, tun)
149            ipver = inner_ipver
150        else:
151            remote_v4 = cfg.remote_addr_v["4"]
152            remote_v6 = cfg.remote_addr_v["6"]
153
154        # First test without the feature enabled.
155        ethtool(f"-K {cfg.ifname} {feature} off")
156        run_one_stream(cfg, ipver, remote_v4, remote_v6, should_lso=False)
157
158        ethtool(f"-K {cfg.ifname} tx-gso-partial off")
159        ethtool(f"-K {cfg.ifname} tx-tcp-mangleid-segmentation off")
160        if feature in cfg.partial_features:
161            ethtool(f"-K {cfg.ifname} tx-gso-partial on")
162            if ipver == "4":
163                ksft_pr("Testing with mangleid enabled")
164                ethtool(f"-K {cfg.ifname} tx-tcp-mangleid-segmentation on")
165
166        # Full feature enabled.
167        ethtool(f"-K {cfg.ifname} {feature} on")
168        run_one_stream(cfg, ipver, remote_v4, remote_v6, should_lso=True)
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    features = cfg.ethnl.features_get({"header": {"dev-index": cfg.ifindex}})
180
181    cfg.wanted_features = set()
182    for f in features["wanted"]["bits"]["bit"]:
183        cfg.wanted_features.add(f["name"])
184
185    cfg.hw_features = set()
186    hw_all_features_cmd = ""
187    for f in features["hw"]["bits"]["bit"]:
188        if f.get("value", False):
189            feature = f["name"]
190            cfg.hw_features.add(feature)
191            hw_all_features_cmd += f" {feature} on"
192    try:
193        ethtool(f"-K {cfg.ifname} {hw_all_features_cmd}")
194    except Exception as e:
195        ksft_pr(f"WARNING: failure enabling all hw features: {e}")
196        ksft_pr("partial gso feature detection may be impacted")
197
198    # Check which features are supported via GSO partial
199    cfg.partial_features = set()
200    if 'tx-gso-partial' in cfg.hw_features:
201        ethtool(f"-K {cfg.ifname} tx-gso-partial off")
202
203        no_partial = set()
204        features = cfg.ethnl.features_get({"header": {"dev-index": cfg.ifindex}})
205        for f in features["active"]["bits"]["bit"]:
206            no_partial.add(f["name"])
207        cfg.partial_features = cfg.hw_features - no_partial
208        ethtool(f"-K {cfg.ifname} tx-gso-partial on")
209
210    restore_wanted_features(cfg)
211
212    stats = cfg.netnl.qstats_get({"ifindex": cfg.ifindex}, dump=True)
213    if stats:
214        if 'tx-hw-gso-packets' in stats[0]:
215            ksft_pr("Detected qstat for LSO super-packets")
216            cfg.have_stat_super_count = True
217        if 'tx-hw-gso-wire-packets' in stats[0]:
218            ksft_pr("Detected qstat for LSO wire-packets")
219            cfg.have_stat_wire_count = True
220
221
222def main() -> None:
223    with NetDrvEpEnv(__file__, nsim_test=False) as cfg:
224        cfg.ethnl = EthtoolFamily()
225        cfg.netnl = NetdevFamily()
226
227        query_nic_features(cfg)
228
229        test_info = (
230            # name,       v4/v6  ethtool_feature               tun:(type, args, inner ip versions)
231            ("",           "4", "tx-tcp-segmentation",         None),
232            ("",           "6", "tx-tcp6-segmentation",        None),
233            ("vxlan",      "4", "tx-udp_tnl-segmentation",     ("vxlan", "id 100 dstport 4789 noudpcsum", ("4", "6"))),
234            ("vxlan",      "6", "tx-udp_tnl-segmentation",     ("vxlan", "id 100 dstport 4789 udp6zerocsumtx udp6zerocsumrx", ("4", "6"))),
235            ("vxlan_csum", "", "tx-udp_tnl-csum-segmentation", ("vxlan", "id 100 dstport 4789 udpcsum", ("4", "6"))),
236            ("gre",        "4", "tx-gre-segmentation",         ("gre",   "", ("4", "6"))),
237            ("gre",        "6", "tx-gre-segmentation",         ("ip6gre","", ("4", "6"))),
238        )
239
240        cases = []
241        for outer_ipver in ["4", "6"]:
242            for info in test_info:
243                # Skip if test which only works for a specific IP version
244                if info[1] and outer_ipver != info[1]:
245                    continue
246
247                if info[3]:
248                    cases += [
249                        test_builder(info[0], cfg, outer_ipver, info[2], info[3], inner_ipver)
250                        for inner_ipver in info[3][2]
251                    ]
252                else:
253                    cases.append(test_builder(info[0], cfg, outer_ipver, info[2], None, outer_ipver))
254
255        ksft_run(cases=cases, args=(cfg, ))
256    ksft_exit()
257
258
259if __name__ == "__main__":
260    main()
261