Path: blob/master/tools/testing/selftests/drivers/net/napi_threaded.py
29270 views
#!/usr/bin/env python31# SPDX-License-Identifier: GPL-2.023"""4Test napi threaded states.5"""67from lib.py import ksft_run, ksft_exit8from lib.py import ksft_eq, ksft_ne, ksft_ge9from lib.py import NetDrvEnv, NetdevFamily10from lib.py import cmd, defer, ethtool111213def _assert_napi_threaded_enabled(nl, napi_id) -> None:14napi = nl.napi_get({'id': napi_id})15ksft_eq(napi['threaded'], 'enabled')16ksft_ne(napi.get('pid'), None)171819def _assert_napi_threaded_disabled(nl, napi_id) -> None:20napi = nl.napi_get({'id': napi_id})21ksft_eq(napi['threaded'], 'disabled')22ksft_eq(napi.get('pid'), None)232425def _set_threaded_state(cfg, threaded) -> None:26with open(f"/sys/class/net/{cfg.ifname}/threaded", "wb") as fp:27fp.write(str(threaded).encode('utf-8'))282930def _setup_deferred_cleanup(cfg) -> None:31combined = ethtool(f"-l {cfg.ifname}", json=True)[0].get("combined", 0)32ksft_ge(combined, 2)33defer(ethtool, f"-L {cfg.ifname} combined {combined}")3435threaded = cmd(f"cat /sys/class/net/{cfg.ifname}/threaded").stdout36defer(_set_threaded_state, cfg, threaded)3738return combined394041def napi_init(cfg, nl) -> None:42"""43Test that threaded state (in the persistent NAPI config) gets updated44even when NAPI with given ID is not allocated at the time.45"""4647qcnt = _setup_deferred_cleanup(cfg)4849_set_threaded_state(cfg, 1)50cmd(f"ethtool -L {cfg.ifname} combined 1")51_set_threaded_state(cfg, 0)52cmd(f"ethtool -L {cfg.ifname} combined {qcnt}")5354napis = nl.napi_get({'ifindex': cfg.ifindex}, dump=True)55for napi in napis:56ksft_eq(napi['threaded'], 'disabled')57ksft_eq(napi.get('pid'), None)5859cmd(f"ethtool -L {cfg.ifname} combined 1")60_set_threaded_state(cfg, 1)61cmd(f"ethtool -L {cfg.ifname} combined {qcnt}")6263napis = nl.napi_get({'ifindex': cfg.ifindex}, dump=True)64for napi in napis:65ksft_eq(napi['threaded'], 'enabled')66ksft_ne(napi.get('pid'), None)676869def enable_dev_threaded_disable_napi_threaded(cfg, nl) -> None:70"""71Test that when napi threaded is enabled at device level and72then disabled at napi level for one napi, the threaded state73of all napis is preserved after a change in number of queues.74"""7576napis = nl.napi_get({'ifindex': cfg.ifindex}, dump=True)77ksft_ge(len(napis), 2)7879napi0_id = napis[0]['id']80napi1_id = napis[1]['id']8182qcnt = _setup_deferred_cleanup(cfg)8384# set threaded85_set_threaded_state(cfg, 1)8687# check napi threaded is set for both napis88_assert_napi_threaded_enabled(nl, napi0_id)89_assert_napi_threaded_enabled(nl, napi1_id)9091# disable threaded for napi192nl.napi_set({'id': napi1_id, 'threaded': 'disabled'})9394cmd(f"ethtool -L {cfg.ifname} combined 1")95cmd(f"ethtool -L {cfg.ifname} combined {qcnt}")96_assert_napi_threaded_enabled(nl, napi0_id)97_assert_napi_threaded_disabled(nl, napi1_id)9899100def change_num_queues(cfg, nl) -> None:101"""102Test that when napi threaded is enabled at device level,103the napi threaded state is preserved after a change in104number of queues.105"""106107napis = nl.napi_get({'ifindex': cfg.ifindex}, dump=True)108ksft_ge(len(napis), 2)109110napi0_id = napis[0]['id']111napi1_id = napis[1]['id']112113qcnt = _setup_deferred_cleanup(cfg)114115# set threaded116_set_threaded_state(cfg, 1)117118# check napi threaded is set for both napis119_assert_napi_threaded_enabled(nl, napi0_id)120_assert_napi_threaded_enabled(nl, napi1_id)121122cmd(f"ethtool -L {cfg.ifname} combined 1")123cmd(f"ethtool -L {cfg.ifname} combined {qcnt}")124125# check napi threaded is set for both napis126_assert_napi_threaded_enabled(nl, napi0_id)127_assert_napi_threaded_enabled(nl, napi1_id)128129130def main() -> None:131""" Ksft boiler plate main """132133with NetDrvEnv(__file__, queue_count=2) as cfg:134ksft_run([napi_init,135change_num_queues,136enable_dev_threaded_disable_napi_threaded],137args=(cfg, NetdevFamily()))138ksft_exit()139140141if __name__ == "__main__":142main()143144145