xref: /linux/tools/testing/selftests/drivers/net/hw/nic_timestamp.py (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
1#!/usr/bin/env python3
2# SPDX-License-Identifier: GPL-2.0
3
4"""
5Tests related to configuration of HW timestamping
6"""
7
8import errno
9from lib.py import ksft_run, ksft_exit, ksft_ge, ksft_eq, KsftSkipEx
10from lib.py import NetDrvEnv, EthtoolFamily, NlError
11
12
13def __get_hwtimestamp_support(cfg):
14    """ Retrieve supported configuration information """
15
16    try:
17        tsinfo = cfg.ethnl.tsinfo_get({'header': {'dev-name': cfg.ifname}})
18    except NlError as e:
19        if e.error == errno.EOPNOTSUPP:
20            raise KsftSkipEx("timestamping configuration is not supported") from e
21        raise
22
23    ctx = {}
24    tx = tsinfo.get('tx-types', {})
25    rx = tsinfo.get('rx-filters', {})
26
27    bits = tx.get('bits', {})
28    ctx['tx'] = bits.get('bit', [])
29    bits = rx.get('bits', {})
30    ctx['rx'] = bits.get('bit', [])
31    return ctx
32
33
34def __get_hwtimestamp_config(cfg):
35    """ Retrieve current TS configuration information """
36
37    try:
38        tscfg = cfg.ethnl.tsconfig_get({'header': {'dev-name': cfg.ifname}})
39    except NlError as e:
40        if e.error == errno.EOPNOTSUPP:
41            raise KsftSkipEx("timestamping configuration is not supported via netlink") from e
42        raise
43    return tscfg
44
45
46def __set_hwtimestamp_config(cfg, ts):
47    """ Setup new TS configuration information """
48
49    ts['header'] = {'dev-name': cfg.ifname}
50    try:
51        res = cfg.ethnl.tsconfig_set(ts)
52    except NlError as e:
53        if e.error == errno.EOPNOTSUPP:
54            raise KsftSkipEx("timestamping configuration is not supported via netlink") from e
55        raise
56    return res
57
58
59def test_hwtstamp_tx(cfg):
60    """
61    Test TX timestamp configuration.
62    The driver should apply provided config and report back proper state.
63    """
64
65    orig_tscfg = __get_hwtimestamp_config(cfg)
66    ts = __get_hwtimestamp_support(cfg)
67    tx = ts['tx']
68    for t in tx:
69        tscfg = orig_tscfg
70        tscfg['tx-types']['bits']['bit'] = [t]
71        res = __set_hwtimestamp_config(cfg, tscfg)
72        if res is None:
73            res = __get_hwtimestamp_config(cfg)
74        ksft_eq(res['tx-types']['bits']['bit'], [t])
75    __set_hwtimestamp_config(cfg, orig_tscfg)
76
77
78def test_hwtstamp_rx(cfg):
79    """
80    Test RX timestamp configuration.
81    The filter configuration is taken from the list of supported filters.
82    The driver should apply the config without error and report back proper state.
83    Some extension of the timestamping scope is allowed for PTP filters.
84    """
85
86    orig_tscfg = __get_hwtimestamp_config(cfg)
87    ts = __get_hwtimestamp_support(cfg)
88    rx = ts['rx']
89    for r in rx:
90        tscfg = orig_tscfg
91        tscfg['rx-filters']['bits']['bit'] = [r]
92        res = __set_hwtimestamp_config(cfg, tscfg)
93        if res is None:
94            res = __get_hwtimestamp_config(cfg)
95        if r['index'] == 0 or r['index'] == 1:
96            ksft_eq(res['rx-filters']['bits']['bit'][0]['index'], r['index'])
97        else:
98            # the driver can fallback to some value which has higher coverage for timestamping
99            ksft_ge(res['rx-filters']['bits']['bit'][0]['index'], r['index'])
100    __set_hwtimestamp_config(cfg, orig_tscfg)
101
102
103def main() -> None:
104    """ Ksft boiler plate main """
105
106    with NetDrvEnv(__file__, nsim_test=False) as cfg:
107        cfg.ethnl = EthtoolFamily()
108        ksft_run([test_hwtstamp_tx, test_hwtstamp_rx], args=(cfg,))
109        ksft_exit()
110
111
112if __name__ == "__main__":
113    main()
114