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