xref: /linux/tools/testing/selftests/drivers/net/lib/py/feat.py (revision 42a9fb3382fc2573e92f41d203b095d9a372cfc9)
1# SPDX-License-Identifier: GPL-2.0
2
3"""
4Netdev feature helper utilities for kernel selftests.
5
6Provides common operations for changing device features via ethtool.
7"""
8
9from . import KsftXfailEx, defer, ethtool, ksft_pr
10
11
12def set_ethtool_feat(dev, current, feats, host=None, check=True):
13    """Set ethtool features with defer to restore original state."""
14    s2n = {True: "on", False: "off"}
15
16    new = ["-K", dev]
17    old = ["-K", dev]
18    no_change = True
19    for name, state in feats.items():
20        new += [name, s2n[state]]
21        old += [name, s2n[current[name]["active"]]]
22
23        if current[name]["active"] != state:
24            no_change = False
25            if current[name]["fixed"]:
26                raise KsftXfailEx(f"Device does not support {name}")
27    if no_change:
28        return
29
30    eth_cmd = ethtool(" ".join(new), host=host)
31    defer(ethtool, " ".join(old), host=host)
32
33    # If ethtool printed something kernel must have modified some features
34    if eth_cmd.stdout:
35        ksft_pr(eth_cmd)
36
37    if not check:
38        return
39
40    current = ethtool(f"-k {dev}", json=True, host=host)[0]
41    for name, state in feats.items():
42        if current[name]["active"] != state:
43            raise KsftXfailEx(f"{name} was not set to {s2n[state]}")
44