xref: /linux/tools/testing/selftests/drivers/net/queues.py (revision 94901b7a74d82bfd30420f1d9d00898278fdc8bf)
1#!/usr/bin/env python3
2# SPDX-License-Identifier: GPL-2.0
3
4from lib.py import ksft_run, ksft_exit, ksft_eq, KsftSkipEx
5from lib.py import EthtoolFamily, NetdevFamily
6from lib.py import NetDrvEnv
7from lib.py import cmd
8import glob
9
10
11def sys_get_queues(ifname, qtype='rx') -> int:
12    folders = glob.glob(f'/sys/class/net/{ifname}/queues/{qtype}-*')
13    return len(folders)
14
15
16def nl_get_queues(cfg, nl, qtype='rx'):
17    queues = nl.queue_get({'ifindex': cfg.ifindex}, dump=True)
18    if queues:
19        return len([q for q in queues if q['type'] == qtype])
20    return None
21
22
23def get_queues(cfg, nl) -> None:
24    snl = NetdevFamily(recv_size=4096)
25
26    for qtype in ['rx', 'tx']:
27        queues = nl_get_queues(cfg, snl, qtype)
28        if not queues:
29            raise KsftSkipEx('queue-get not supported by device')
30
31        expected = sys_get_queues(cfg.dev['ifname'], qtype)
32        ksft_eq(queues, expected)
33
34
35def addremove_queues(cfg, nl) -> None:
36    queues = nl_get_queues(cfg, nl)
37    if not queues:
38        raise KsftSkipEx('queue-get not supported by device')
39
40    curr_queues = sys_get_queues(cfg.dev['ifname'])
41    if curr_queues == 1:
42        raise KsftSkipEx('cannot decrement queue: already at 1')
43
44    netnl = EthtoolFamily()
45    channels = netnl.channels_get({'header': {'dev-index': cfg.ifindex}})
46    if channels['combined-count'] == 0:
47        rx_type = 'rx'
48    else:
49        rx_type = 'combined'
50
51    expected = curr_queues - 1
52    cmd(f"ethtool -L {cfg.dev['ifname']} {rx_type} {expected}", timeout=10)
53    queues = nl_get_queues(cfg, nl)
54    ksft_eq(queues, expected)
55
56    expected = curr_queues
57    cmd(f"ethtool -L {cfg.dev['ifname']} {rx_type} {expected}", timeout=10)
58    queues = nl_get_queues(cfg, nl)
59    ksft_eq(queues, expected)
60
61
62def main() -> None:
63    with NetDrvEnv(__file__, queue_count=100) as cfg:
64        ksft_run([get_queues, addremove_queues], args=(cfg, NetdevFamily()))
65    ksft_exit()
66
67
68if __name__ == "__main__":
69    main()
70