|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-License-Identifier: GPL-2.0 |
| 3 | + |
| 4 | +""" |
| 5 | +Test napi threaded states. |
| 6 | +""" |
| 7 | + |
| 8 | +from lib.py import ksft_run, ksft_exit |
| 9 | +from lib.py import ksft_eq, ksft_ne, ksft_ge |
| 10 | +from lib.py import NetDrvEnv, NetdevFamily |
| 11 | +from lib.py import cmd, defer, ethtool |
| 12 | + |
| 13 | + |
| 14 | +def _assert_napi_threaded_enabled(nl, napi_id) -> None: |
| 15 | + napi = nl.napi_get({'id': napi_id}) |
| 16 | + ksft_eq(napi['threaded'], 'enabled') |
| 17 | + ksft_ne(napi.get('pid'), None) |
| 18 | + |
| 19 | + |
| 20 | +def _assert_napi_threaded_disabled(nl, napi_id) -> None: |
| 21 | + napi = nl.napi_get({'id': napi_id}) |
| 22 | + ksft_eq(napi['threaded'], 'disabled') |
| 23 | + ksft_eq(napi.get('pid'), None) |
| 24 | + |
| 25 | + |
| 26 | +def _set_threaded_state(cfg, threaded) -> None: |
| 27 | + cmd(f"echo {threaded} > /sys/class/net/{cfg.ifname}/threaded") |
| 28 | + |
| 29 | + |
| 30 | +def _setup_deferred_cleanup(cfg) -> None: |
| 31 | + combined = ethtool(f"-l {cfg.ifname}", json=True)[0].get("combined", 0) |
| 32 | + ksft_ge(combined, 2) |
| 33 | + defer(ethtool, f"-L {cfg.ifname} combined {combined}") |
| 34 | + |
| 35 | + threaded = cmd(f"cat /sys/class/net/{cfg.ifname}/threaded").stdout |
| 36 | + defer(_set_threaded_state, cfg, threaded) |
| 37 | + |
| 38 | + |
| 39 | +def enable_dev_threaded_disable_napi_threaded(cfg, nl) -> None: |
| 40 | + """ |
| 41 | + Test that when napi threaded is enabled at device level and |
| 42 | + then disabled at napi level for one napi, the threaded state |
| 43 | + of all napis is preserved after a change in number of queues. |
| 44 | + """ |
| 45 | + |
| 46 | + napis = nl.napi_get({'ifindex': cfg.ifindex}, dump=True) |
| 47 | + ksft_ge(len(napis), 2) |
| 48 | + |
| 49 | + napi0_id = napis[0]['id'] |
| 50 | + napi1_id = napis[1]['id'] |
| 51 | + |
| 52 | + _setup_deferred_cleanup(cfg) |
| 53 | + |
| 54 | + # set threaded |
| 55 | + _set_threaded_state(cfg, 1) |
| 56 | + |
| 57 | + # check napi threaded is set for both napis |
| 58 | + _assert_napi_threaded_enabled(nl, napi0_id) |
| 59 | + _assert_napi_threaded_enabled(nl, napi1_id) |
| 60 | + |
| 61 | + # disable threaded for napi1 |
| 62 | + nl.napi_set({'id': napi1_id, 'threaded': 'disabled'}) |
| 63 | + |
| 64 | + cmd(f"ethtool -L {cfg.ifname} combined 1") |
| 65 | + cmd(f"ethtool -L {cfg.ifname} combined 2") |
| 66 | + _assert_napi_threaded_enabled(nl, napi0_id) |
| 67 | + _assert_napi_threaded_disabled(nl, napi1_id) |
| 68 | + |
| 69 | + |
| 70 | +def change_num_queues(cfg, nl) -> None: |
| 71 | + """ |
| 72 | + Test that when napi threaded is enabled at device level, |
| 73 | + the napi threaded state is preserved after a change in |
| 74 | + number of queues. |
| 75 | + """ |
| 76 | + |
| 77 | + napis = nl.napi_get({'ifindex': cfg.ifindex}, dump=True) |
| 78 | + ksft_ge(len(napis), 2) |
| 79 | + |
| 80 | + napi0_id = napis[0]['id'] |
| 81 | + napi1_id = napis[1]['id'] |
| 82 | + |
| 83 | + _setup_deferred_cleanup(cfg) |
| 84 | + |
| 85 | + # set threaded |
| 86 | + _set_threaded_state(cfg, 1) |
| 87 | + |
| 88 | + # check napi threaded is set for both napis |
| 89 | + _assert_napi_threaded_enabled(nl, napi0_id) |
| 90 | + _assert_napi_threaded_enabled(nl, napi1_id) |
| 91 | + |
| 92 | + cmd(f"ethtool -L {cfg.ifname} combined 1") |
| 93 | + cmd(f"ethtool -L {cfg.ifname} combined 2") |
| 94 | + |
| 95 | + # check napi threaded is set for both napis |
| 96 | + _assert_napi_threaded_enabled(nl, napi0_id) |
| 97 | + _assert_napi_threaded_enabled(nl, napi1_id) |
| 98 | + |
| 99 | + |
| 100 | +def main() -> None: |
| 101 | + """ Ksft boiler plate main """ |
| 102 | + |
| 103 | + with NetDrvEnv(__file__, queue_count=2) as cfg: |
| 104 | + ksft_run([change_num_queues, |
| 105 | + enable_dev_threaded_disable_napi_threaded], |
| 106 | + args=(cfg, NetdevFamily())) |
| 107 | + ksft_exit() |
| 108 | + |
| 109 | + |
| 110 | +if __name__ == "__main__": |
| 111 | + main() |
0 commit comments