Skip to content

Commit 1cf2704

Browse files
spikehkuba-moo
authored andcommitted
net: selftest: add test for netdev netlink queue-get API
Add a selftest for netdev generic netlink. For now there is only a single test that exercises the `queue-get` API. The test works with netdevsim by default or with a real device by setting NETIF. Add a timeout param to cmd() since ethtool -L can take a long time on real devices. Signed-off-by: David Wei <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 3762ec0 commit 1cf2704

File tree

5 files changed

+77
-8
lines changed

5 files changed

+77
-8
lines changed

tools/testing/selftests/drivers/net/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ TEST_INCLUDES := $(wildcard lib/py/*.py)
44

55
TEST_PROGS := \
66
ping.py \
7+
queues.py \
78
stats.py \
89
# end of TEST_PROGS
910

tools/testing/selftests/drivers/net/lib/py/env.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,21 @@ class NetDrvEnv:
3636
"""
3737
Class for a single NIC / host env, with no remote end
3838
"""
39-
def __init__(self, src_path):
39+
def __init__(self, src_path, **kwargs):
4040
self._ns = None
4141

4242
self.env = _load_env_file(src_path)
4343

4444
if 'NETIF' in self.env:
4545
self.dev = ip("link show dev " + self.env['NETIF'], json=True)[0]
4646
else:
47-
self._ns = NetdevSimDev()
47+
self._ns = NetdevSimDev(**kwargs)
4848
self.dev = self._ns.nsims[0].dev
4949
self.ifindex = self.dev['ifindex']
5050

5151
def __enter__(self):
52+
ip(f"link set dev {self.dev['ifname']} up")
53+
5254
return self
5355

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

tools/testing/selftests/net/lib/py/nsim.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def dfs_write(self, path, val):
4949
with open(fullpath, "w") as f:
5050
f.write(val)
5151

52-
def __init__(self, port_count=1, ns=None):
52+
def __init__(self, port_count=1, queue_count=1, ns=None):
5353
# nsim will spawn in init_net, we'll set to actual ns once we switch it there
5454
self.ns = None
5555

@@ -59,7 +59,7 @@ def __init__(self, port_count=1, ns=None):
5959
addr = random.randrange(1 << 15)
6060
while True:
6161
try:
62-
self.ctrl_write("new_device", "%u %u" % (addr, port_count))
62+
self.ctrl_write("new_device", "%u %u %u" % (addr, port_count, queue_count))
6363
except OSError as e:
6464
if e.errno == errno.ENOSPC:
6565
addr = random.randrange(1 << 15)

tools/testing/selftests/net/lib/py/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
class cmd:
11-
def __init__(self, comm, shell=True, fail=True, ns=None, background=False, host=None):
11+
def __init__(self, comm, shell=True, fail=True, ns=None, background=False, host=None, timeout=5):
1212
if ns:
1313
comm = f'ip netns exec {ns} ' + comm
1414

@@ -23,15 +23,15 @@ def __init__(self, comm, shell=True, fail=True, ns=None, background=False, host=
2323
self.proc = subprocess.Popen(comm, shell=shell, stdout=subprocess.PIPE,
2424
stderr=subprocess.PIPE)
2525
if not background:
26-
self.process(terminate=False, fail=fail)
26+
self.process(terminate=False, fail=fail, timeout=timeout)
2727

28-
def process(self, terminate=True, fail=None):
28+
def process(self, terminate=True, fail=None, timeout=5):
2929
if fail is None:
3030
fail = not terminate
3131

3232
if terminate:
3333
self.proc.terminate()
34-
stdout, stderr = self.proc.communicate(timeout=5)
34+
stdout, stderr = self.proc.communicate(timeout)
3535
self.stdout = stdout.decode("utf-8")
3636
self.stderr = stderr.decode("utf-8")
3737
self.proc.stdout.close()

0 commit comments

Comments
 (0)