1#!/usr/bin/env python3 2# SPDX-License-Identifier: GPL-2.0 3 4from lib.py import ksft_run, ksft_exit 5from lib.py import ksft_in, ksft_true, KsftSkipEx, KsftXfailEx 6from lib.py import EthtoolFamily, NetdevFamily, RtnlFamily, NlError 7from lib.py import NetDrvEnv 8 9ethnl = EthtoolFamily() 10netfam = NetdevFamily() 11rtnl = RtnlFamily() 12 13 14def check_pause(cfg) -> None: 15 global ethnl 16 17 try: 18 ethnl.pause_get({"header": {"dev-index": cfg.ifindex}}) 19 except NlError as e: 20 if e.error == 95: 21 raise KsftXfailEx("pause not supported by the device") 22 raise 23 24 data = ethnl.pause_get({"header": {"dev-index": cfg.ifindex, 25 "flags": {'stats'}}}) 26 ksft_true(data['stats'], "driver does not report stats") 27 28 29def check_fec(cfg) -> None: 30 global ethnl 31 32 try: 33 ethnl.fec_get({"header": {"dev-index": cfg.ifindex}}) 34 except NlError as e: 35 if e.error == 95: 36 raise KsftXfailEx("FEC not supported by the device") 37 raise 38 39 data = ethnl.fec_get({"header": {"dev-index": cfg.ifindex, 40 "flags": {'stats'}}}) 41 ksft_true(data['stats'], "driver does not report stats") 42 43 44def pkt_byte_sum(cfg) -> None: 45 global netfam, rtnl 46 47 def get_qstat(test): 48 global netfam 49 stats = netfam.qstats_get({}, dump=True) 50 if stats: 51 for qs in stats: 52 if qs["ifindex"]== test.ifindex: 53 return qs 54 55 qstat = get_qstat(cfg) 56 if qstat is None: 57 raise KsftSkipEx("qstats not supported by the device") 58 59 for key in ['tx-packets', 'tx-bytes', 'rx-packets', 'rx-bytes']: 60 ksft_in(key, qstat, "Drivers should always report basic keys") 61 62 # Compare stats, rtnl stats and qstats must match, 63 # but the interface may be up, so do a series of dumps 64 # each time the more "recent" stats must be higher or same. 65 def stat_cmp(rstat, qstat): 66 for key in ['tx-packets', 'tx-bytes', 'rx-packets', 'rx-bytes']: 67 if rstat[key] != qstat[key]: 68 return rstat[key] - qstat[key] 69 return 0 70 71 for _ in range(10): 72 rtstat = rtnl.getlink({"ifi-index": cfg.ifindex})['stats'] 73 if stat_cmp(rtstat, qstat) < 0: 74 raise Exception("RTNL stats are lower, fetched later") 75 qstat = get_qstat(cfg) 76 if stat_cmp(rtstat, qstat) > 0: 77 raise Exception("Qstats are lower, fetched later") 78 79 80def main() -> None: 81 with NetDrvEnv(__file__) as cfg: 82 ksft_run([check_pause, check_fec, pkt_byte_sum], 83 args=(cfg, )) 84 ksft_exit() 85 86 87if __name__ == "__main__": 88 main() 89