1#!/usr/bin/env python3 2# SPDX-License-Identifier: GPL-2.0 3 4""" 5HW GRO tests focusing on device machinery like stats, rather than protocol 6processing. 7""" 8 9import glob 10import re 11 12from lib.py import ksft_run, ksft_exit, ksft_pr 13from lib.py import ksft_eq, ksft_ge, ksft_variants 14from lib.py import NetDrvEpEnv, NetdevFamily 15from lib.py import KsftSkipEx 16from lib.py import bkg, cmd, defer, ethtool, ip 17 18 19# gro.c uses hardcoded DPORT=8000 20GRO_DPORT = 8000 21 22 23def _get_queue_stats(cfg, queue_id): 24 """Get stats for a specific Rx queue.""" 25 cfg.wait_hw_stats_settle() 26 data = cfg.netnl.qstats_get({"ifindex": cfg.ifindex, "scope": ["queue"]}, 27 dump=True) 28 for q in data: 29 if q.get('queue-type') == 'rx' and q.get('queue-id') == queue_id: 30 return q 31 return {} 32 33 34def _resolve_dmac(cfg, ipver): 35 """Find the destination MAC address for sending packets.""" 36 attr = "dmac" + ipver 37 if hasattr(cfg, attr): 38 return getattr(cfg, attr) 39 40 route = ip(f"-{ipver} route get {cfg.addr_v[ipver]}", 41 json=True, host=cfg.remote)[0] 42 gw = route.get("gateway") 43 if not gw: 44 setattr(cfg, attr, cfg.dev['address']) 45 return getattr(cfg, attr) 46 47 cmd(f"ping -c1 -W0 -I{cfg.remote_ifname} {gw}", host=cfg.remote) 48 neigh = ip(f"neigh get {gw} dev {cfg.remote_ifname}", 49 json=True, host=cfg.remote)[0] 50 setattr(cfg, attr, neigh['lladdr']) 51 return getattr(cfg, attr) 52 53 54def _require_ntuple(cfg): 55 features = ethtool(f"-k {cfg.ifname}", json=True)[0] 56 if not features["ntuple-filters"]["active"]: 57 if features["ntuple-filters"]["fixed"]: 58 raise KsftSkipEx("Device does not support ntuple-filters") 59 ethtool(f"-K {cfg.ifname} ntuple-filters on") 60 defer(ethtool, f"-K {cfg.ifname} ntuple-filters off") 61 62 63def _setup_isolated_queue(cfg): 64 """Set up an isolated queue for testing using ntuple filter. 65 66 Remove queue 1 from the default RSS context and steer test traffic to it. 67 """ 68 _require_ntuple(cfg) 69 test_queue = 1 70 71 qcnt = len(glob.glob(f"/sys/class/net/{cfg.ifname}/queues/rx-*")) 72 if qcnt < 2: 73 raise KsftSkipEx(f"Need at least 2 queues, have {qcnt}") 74 75 # Remove queue 1 from default RSS context by setting its weight to 0 76 weights = ["1"] * qcnt 77 weights[test_queue] = "0" 78 ethtool(f"-X {cfg.ifname} weight " + " ".join(weights)) 79 defer(ethtool, f"-X {cfg.ifname} default") 80 81 # Set up ntuple filter to steer our test traffic to the isolated queue 82 flow = f"flow-type tcp{cfg.addr_ipver} " 83 flow += f"dst-ip {cfg.addr} dst-port {GRO_DPORT} action {test_queue}" 84 output = ethtool(f"-N {cfg.ifname} {flow}").stdout 85 ntuple_id = int(output.split()[-1]) 86 defer(ethtool, f"-N {cfg.ifname} delete {ntuple_id}") 87 88 return test_queue 89 90 91def _run_gro_test(cfg, test_name, num_flows=None, ignore_fail=False, 92 order_check=False): 93 """Run gro binary with given test and return output.""" 94 if not hasattr(cfg, "bin_remote"): 95 cfg.bin_local = cfg.net_lib_dir / "gro" 96 cfg.bin_remote = cfg.remote.deploy(cfg.bin_local) 97 98 ipver = cfg.addr_ipver 99 protocol = f"--ipv{ipver}" 100 dmac = _resolve_dmac(cfg, ipver) 101 102 base_args = [ 103 protocol, 104 f"--dmac {dmac}", 105 f"--smac {cfg.remote_dev['address']}", 106 f"--daddr {cfg.addr}", 107 f"--saddr {cfg.remote_addr_v[ipver]}", 108 f"--test {test_name}", 109 ] 110 if num_flows: 111 base_args.append(f"--num-flows {num_flows}") 112 if order_check: 113 base_args.append("--order-check") 114 115 args = " ".join(base_args) 116 117 rx_cmd = f"{cfg.bin_local} {args} --rx --iface {cfg.ifname}" 118 tx_cmd = f"{cfg.bin_remote} {args} --iface {cfg.remote_ifname}" 119 120 with bkg(rx_cmd, ksft_ready=True, exit_wait=True, fail=False) as rx_proc: 121 cmd(tx_cmd, host=cfg.remote) 122 123 if not ignore_fail: 124 ksft_eq(rx_proc.ret, 0) 125 if rx_proc.ret != 0: 126 ksft_pr(rx_proc) 127 128 return rx_proc.stdout 129 130 131def _require_hw_gro_stats(cfg, queue_id): 132 """Check if device reports HW GRO stats for the queue.""" 133 stats = _get_queue_stats(cfg, queue_id) 134 required = ['rx-packets', 'rx-hw-gro-packets', 'rx-hw-gro-wire-packets'] 135 for stat in required: 136 if stat not in stats: 137 raise KsftSkipEx(f"Driver does not report '{stat}' via qstats") 138 139 140def _set_ethtool_feat(cfg, current, feats): 141 """Set ethtool features with defer to restore original state.""" 142 s2n = {True: "on", False: "off"} 143 144 new = ["-K", cfg.ifname] 145 old = ["-K", cfg.ifname] 146 no_change = True 147 for name, state in feats.items(): 148 new += [name, s2n[state]] 149 old += [name, s2n[current[name]["active"]]] 150 151 if current[name]["active"] != state: 152 no_change = False 153 if current[name]["fixed"]: 154 raise KsftSkipEx(f"Device does not support {name}") 155 if no_change: 156 return 157 158 eth_cmd = ethtool(" ".join(new)) 159 defer(ethtool, " ".join(old)) 160 161 # If ethtool printed something kernel must have modified some features 162 if eth_cmd.stdout: 163 ksft_pr(eth_cmd) 164 165 166def _setup_hw_gro(cfg): 167 """Enable HW GRO on the device, disabling SW GRO.""" 168 feat = ethtool(f"-k {cfg.ifname}", json=True)[0] 169 170 # Try to disable SW GRO and enable HW GRO 171 _set_ethtool_feat(cfg, feat, 172 {"generic-receive-offload": False, 173 "rx-gro-hw": True, 174 "large-receive-offload": False}) 175 176 # Some NICs treat HW GRO as a GRO sub-feature so disabling GRO 177 # will also clear HW GRO. Use a hack of installing XDP generic 178 # to skip SW GRO, even when enabled. 179 feat = ethtool(f"-k {cfg.ifname}", json=True)[0] 180 if not feat["rx-gro-hw"]["active"]: 181 ksft_pr("Driver clears HW GRO when SW GRO is cleared, using generic XDP workaround") 182 prog = cfg.net_lib_dir / "xdp_dummy.bpf.o" 183 ip(f"link set dev {cfg.ifname} xdpgeneric obj {prog} sec xdp") 184 defer(ip, f"link set dev {cfg.ifname} xdpgeneric off") 185 186 # Attaching XDP may change features, fetch the latest state 187 feat = ethtool(f"-k {cfg.ifname}", json=True)[0] 188 189 _set_ethtool_feat(cfg, feat, 190 {"generic-receive-offload": True, 191 "rx-gro-hw": True, 192 "large-receive-offload": False}) 193 194 195def _check_gro_stats(cfg, test_queue, stats_before, 196 expect_rx, expect_gro, expect_wire): 197 """Validate GRO stats against expected values.""" 198 stats_after = _get_queue_stats(cfg, test_queue) 199 200 rx_delta = (stats_after.get('rx-packets', 0) - 201 stats_before.get('rx-packets', 0)) 202 gro_delta = (stats_after.get('rx-hw-gro-packets', 0) - 203 stats_before.get('rx-hw-gro-packets', 0)) 204 wire_delta = (stats_after.get('rx-hw-gro-wire-packets', 0) - 205 stats_before.get('rx-hw-gro-wire-packets', 0)) 206 207 ksft_eq(rx_delta, expect_rx, comment="rx-packets") 208 ksft_eq(gro_delta, expect_gro, comment="rx-hw-gro-packets") 209 ksft_eq(wire_delta, expect_wire, comment="rx-hw-gro-wire-packets") 210 211 212def test_gro_stats_single(cfg): 213 """ 214 Test that a single packet doesn't affect GRO stats. 215 216 Send a single packet that cannot be coalesced (nothing to coalesce with). 217 GRO stats should not increase since no coalescing occurred. 218 rx-packets should increase by 2 (1 data + 1 FIN). 219 """ 220 _setup_hw_gro(cfg) 221 222 test_queue = _setup_isolated_queue(cfg) 223 _require_hw_gro_stats(cfg, test_queue) 224 225 stats_before = _get_queue_stats(cfg, test_queue) 226 227 _run_gro_test(cfg, "single") 228 229 # 1 data + 1 FIN = 2 rx-packets, no coalescing 230 _check_gro_stats(cfg, test_queue, stats_before, 231 expect_rx=2, expect_gro=0, expect_wire=0) 232 233 234def test_gro_stats_full(cfg): 235 """ 236 Test GRO stats when overwhelming HW GRO capacity. 237 238 Send 500 flows to exceed HW GRO flow capacity on a single queue. 239 This should result in some packets not being coalesced. 240 Validate that qstats match what gro.c observed. 241 """ 242 _setup_hw_gro(cfg) 243 244 test_queue = _setup_isolated_queue(cfg) 245 _require_hw_gro_stats(cfg, test_queue) 246 247 num_flows = 500 248 stats_before = _get_queue_stats(cfg, test_queue) 249 250 # Run capacity test - will likely fail because not all packets coalesce 251 output = _run_gro_test(cfg, "capacity", num_flows=num_flows, 252 ignore_fail=True) 253 254 # Parse gro.c output: "STATS: received=X wire=Y coalesced=Z" 255 match = re.search(r'STATS: received=(\d+) wire=(\d+) coalesced=(\d+)', 256 output) 257 if not match: 258 raise KsftSkipEx(f"Could not parse gro.c output: {output}") 259 260 rx_frames = int(match.group(2)) 261 gro_coalesced = int(match.group(3)) 262 263 ksft_ge(gro_coalesced, 1, 264 comment="At least some packets should coalesce") 265 266 # received + 1 FIN, coalesced super-packets, coalesced * 2 wire packets 267 _check_gro_stats(cfg, test_queue, stats_before, 268 expect_rx=rx_frames + 1, 269 expect_gro=gro_coalesced, 270 expect_wire=gro_coalesced * 2) 271 272 273@ksft_variants([4, 32, 512]) 274def test_gro_order(cfg, num_flows): 275 """ 276 Test that HW GRO preserves packet ordering between flows. 277 278 Packets may get delayed until the aggregate is released, 279 but reordering between aggregates and packet terminating 280 the aggregate and normal packets should not happen. 281 282 Note that this test is stricter than truly required. 283 Reordering packets between flows should not cause issues. 284 This test will also fail if traffic is run over an ECMP fabric. 285 """ 286 _setup_hw_gro(cfg) 287 _setup_isolated_queue(cfg) 288 289 _run_gro_test(cfg, "capacity", num_flows=num_flows, order_check=True) 290 291 292def main() -> None: 293 """ Ksft boiler plate main """ 294 295 with NetDrvEpEnv(__file__, nsim_test=False) as cfg: 296 cfg.netnl = NetdevFamily() 297 ksft_run([test_gro_stats_single, 298 test_gro_stats_full, 299 test_gro_order], args=(cfg,)) 300 ksft_exit() 301 302 303if __name__ == "__main__": 304 main() 305