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