Path: blob/master/tools/testing/selftests/drivers/net/hw/nic_timestamp.py
29271 views
#!/usr/bin/env python31# SPDX-License-Identifier: GPL-2.023"""4Tests related to configuration of HW timestamping5"""67import errno8from lib.py import ksft_run, ksft_exit, ksft_ge, ksft_eq, KsftSkipEx9from lib.py import NetDrvEnv, EthtoolFamily, NlError101112def __get_hwtimestamp_support(cfg):13""" Retrieve supported configuration information """1415try:16tsinfo = cfg.ethnl.tsinfo_get({'header': {'dev-name': cfg.ifname}})17except NlError as e:18if e.error == errno.EOPNOTSUPP:19raise KsftSkipEx("timestamping configuration is not supported") from e20raise2122ctx = {}23tx = tsinfo.get('tx-types', {})24rx = tsinfo.get('rx-filters', {})2526bits = tx.get('bits', {})27ctx['tx'] = bits.get('bit', [])28bits = rx.get('bits', {})29ctx['rx'] = bits.get('bit', [])30return ctx313233def __get_hwtimestamp_config(cfg):34""" Retrieve current TS configuration information """3536try:37tscfg = cfg.ethnl.tsconfig_get({'header': {'dev-name': cfg.ifname}})38except NlError as e:39if e.error == errno.EOPNOTSUPP:40raise KsftSkipEx("timestamping configuration is not supported via netlink") from e41raise42return tscfg434445def __set_hwtimestamp_config(cfg, ts):46""" Setup new TS configuration information """4748ts['header'] = {'dev-name': cfg.ifname}49try:50res = cfg.ethnl.tsconfig_set(ts)51except NlError as e:52if e.error == errno.EOPNOTSUPP:53raise KsftSkipEx("timestamping configuration is not supported via netlink") from e54raise55return res565758def test_hwtstamp_tx(cfg):59"""60Test TX timestamp configuration.61The driver should apply provided config and report back proper state.62"""6364orig_tscfg = __get_hwtimestamp_config(cfg)65ts = __get_hwtimestamp_support(cfg)66tx = ts['tx']67for t in tx:68tscfg = orig_tscfg69tscfg['tx-types']['bits']['bit'] = [t]70res = __set_hwtimestamp_config(cfg, tscfg)71if res is None:72res = __get_hwtimestamp_config(cfg)73ksft_eq(res['tx-types']['bits']['bit'], [t])74__set_hwtimestamp_config(cfg, orig_tscfg)757677def test_hwtstamp_rx(cfg):78"""79Test RX timestamp configuration.80The filter configuration is taken from the list of supported filters.81The driver should apply the config without error and report back proper state.82Some extension of the timestamping scope is allowed for PTP filters.83"""8485orig_tscfg = __get_hwtimestamp_config(cfg)86ts = __get_hwtimestamp_support(cfg)87rx = ts['rx']88for r in rx:89tscfg = orig_tscfg90tscfg['rx-filters']['bits']['bit'] = [r]91res = __set_hwtimestamp_config(cfg, tscfg)92if res is None:93res = __get_hwtimestamp_config(cfg)94if r['index'] == 0 or r['index'] == 1:95ksft_eq(res['rx-filters']['bits']['bit'][0]['index'], r['index'])96else:97# the driver can fallback to some value which has higher coverage for timestamping98ksft_ge(res['rx-filters']['bits']['bit'][0]['index'], r['index'])99__set_hwtimestamp_config(cfg, orig_tscfg)100101102def main() -> None:103""" Ksft boiler plate main """104105with NetDrvEnv(__file__, nsim_test=False) as cfg:106cfg.ethnl = EthtoolFamily()107ksft_run([test_hwtstamp_tx, test_hwtstamp_rx], args=(cfg,))108ksft_exit()109110111if __name__ == "__main__":112main()113114115