diff mbox series

[net-next,v3,2/2] net: selftest: add test for netdev netlink queue-get API

Message ID 20240424023624.2320033-3-dw@davidwei.uk (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series netdevsim: add NAPI support | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 932 this patch: 932
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 3 maintainers not CCed: petrm@nvidia.com linux-kselftest@vger.kernel.org shuah@kernel.org
netdev/build_clang success Errors and warnings before: 938 this patch: 938
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 944 this patch: 944
netdev/checkpatch warning WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-04-24--15-00 (tests: 995)

Commit Message

David Wei April 24, 2024, 2:36 a.m. UTC
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 <dw@davidwei.uk>
---
 tools/testing/selftests/drivers/net/Makefile  |  1 +
 .../selftests/drivers/net/lib/py/env.py       |  6 +-
 tools/testing/selftests/drivers/net/queues.py | 66 +++++++++++++++++++
 tools/testing/selftests/net/lib/py/nsim.py    |  4 +-
 tools/testing/selftests/net/lib/py/utils.py   |  8 +--
 5 files changed, 77 insertions(+), 8 deletions(-)
 create mode 100755 tools/testing/selftests/drivers/net/queues.py

Comments

Jakub Kicinski April 26, 2024, 1:51 a.m. UTC | #1
On Tue, 23 Apr 2024 19:36:24 -0700 David Wei wrote:
> +    try:
> +        expected = curr_queues - 1
> +        cmd(f"ethtool -L {cfg.dev['ifname']} {rx_type} {expected}", timeout=10)
> +        queues = nl_get_queues(cfg, nl)
> +        if not queues:
> +            raise KsftSkipEx('queue-get not supported by device')
> +        ksft_eq(queues, expected)
> +
> +        expected = curr_queues
> +        cmd(f"ethtool -L {cfg.dev['ifname']} {rx_type} {expected}", timeout=10)
> +        queues = nl_get_queues(cfg, nl)
> +        ksft_eq(queues, expected)
> +    except Exception as ex:
> +        raise KsftSkipEx(ex)


Why convert all exceptions to skip? Don't we want the test to go red
if something is off?

> +def main() -> None:
> +    with NetDrvEnv(__file__, queue_count=3) as cfg:
> +        ksft_run([get_queues, addremove_queues], args=(cfg, NetdevFamily()))

gotta call ksft_exit() at the end explicitly. It's a bit annoying, 
I know :S
David Wei May 2, 2024, 5:04 a.m. UTC | #2
On 2024-04-25 6:51 pm, Jakub Kicinski wrote:
> On Tue, 23 Apr 2024 19:36:24 -0700 David Wei wrote:
>> +    try:
>> +        expected = curr_queues - 1
>> +        cmd(f"ethtool -L {cfg.dev['ifname']} {rx_type} {expected}", timeout=10)
>> +        queues = nl_get_queues(cfg, nl)
>> +        if not queues:
>> +            raise KsftSkipEx('queue-get not supported by device')
>> +        ksft_eq(queues, expected)
>> +
>> +        expected = curr_queues
>> +        cmd(f"ethtool -L {cfg.dev['ifname']} {rx_type} {expected}", timeout=10)
>> +        queues = nl_get_queues(cfg, nl)
>> +        ksft_eq(queues, expected)
>> +    except Exception as ex:
>> +        raise KsftSkipEx(ex)
> 
> 
> Why convert all exceptions to skip? Don't we want the test to go red
> if something is off?

I wanted to separate ethtool -L failures from test failures, but looking
at this code I'm swalling all exceptions here, sorry.

> 
>> +def main() -> None:
>> +    with NetDrvEnv(__file__, queue_count=3) as cfg:
>> +        ksft_run([get_queues, addremove_queues], args=(cfg, NetdevFamily()))
> 
> gotta call ksft_exit() at the end explicitly. It's a bit annoying, 
> I know :S

Ah, I didn't know. Will address in next version.
diff mbox series

Patch

diff --git a/tools/testing/selftests/drivers/net/Makefile b/tools/testing/selftests/drivers/net/Makefile
index 754ec643768a..e54f382bcb02 100644
--- a/tools/testing/selftests/drivers/net/Makefile
+++ b/tools/testing/selftests/drivers/net/Makefile
@@ -4,6 +4,7 @@  TEST_INCLUDES := $(wildcard lib/py/*.py)
 
 TEST_PROGS := \
 	ping.py \
+	queues.py \
 	stats.py \
 # end of TEST_PROGS
 
diff --git a/tools/testing/selftests/drivers/net/lib/py/env.py b/tools/testing/selftests/drivers/net/lib/py/env.py
index a3db1bb1afeb..04702b2298f9 100644
--- a/tools/testing/selftests/drivers/net/lib/py/env.py
+++ b/tools/testing/selftests/drivers/net/lib/py/env.py
@@ -34,7 +34,7 @@  class NetDrvEnv:
     """
     Class for a single NIC / host env, with no remote end
     """
-    def __init__(self, src_path):
+    def __init__(self, src_path, **kwargs):
         self._ns = None
 
         self.env = _load_env_file(src_path)
@@ -42,11 +42,13 @@  class NetDrvEnv:
         if 'NETIF' in self.env:
             self.dev = ip("link show dev " + self.env['NETIF'], json=True)[0]
         else:
-            self._ns = NetdevSimDev()
+            self._ns = NetdevSimDev(**kwargs)
             self.dev = self._ns.nsims[0].dev
         self.ifindex = self.dev['ifindex']
 
     def __enter__(self):
+        ip(f"link set dev {self.dev['ifname']} up")
+
         return self
 
     def __exit__(self, ex_type, ex_value, ex_tb):
diff --git a/tools/testing/selftests/drivers/net/queues.py b/tools/testing/selftests/drivers/net/queues.py
new file mode 100755
index 000000000000..35e5073bd1f9
--- /dev/null
+++ b/tools/testing/selftests/drivers/net/queues.py
@@ -0,0 +1,66 @@ 
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+
+from lib.py import ksft_run, ksft_eq, KsftSkipEx
+from lib.py import EthtoolFamily, NetdevFamily
+from lib.py import NetDrvEnv
+from lib.py import cmd
+import glob
+
+
+def sys_get_queues(ifname) -> int:
+    folders = glob.glob(f'/sys/class/net/{ifname}/queues/rx-*')
+    return len(folders)
+
+
+def nl_get_queues(cfg, nl):
+    queues = nl.queue_get({'ifindex': cfg.ifindex}, dump=True)
+    if queues:
+        return len([q for q in queues if q['type'] == 'rx'])
+    return None
+
+
+def get_queues(cfg, nl) -> None:
+    queues = nl_get_queues(cfg, nl)
+    if not queues:
+        raise KsftSkipEx('queue-get not supported by device')
+
+    expected = sys_get_queues(cfg.dev['ifname'])
+    ksft_eq(queues, expected)
+
+
+def addremove_queues(cfg, nl) -> None:
+    curr_queues = sys_get_queues(cfg.dev['ifname'])
+    if curr_queues == 1:
+        raise KsftSkipEx('cannot decrement queue: already at 1')
+
+    netnl = EthtoolFamily()
+    channels = netnl.channels_get({'header': {'dev-index': cfg.ifindex}})
+    if channels['combined-count'] == 0:
+        rx_type = 'rx'
+    else:
+        rx_type = 'combined'
+
+    try:
+        expected = curr_queues - 1
+        cmd(f"ethtool -L {cfg.dev['ifname']} {rx_type} {expected}", timeout=10)
+        queues = nl_get_queues(cfg, nl)
+        if not queues:
+            raise KsftSkipEx('queue-get not supported by device')
+        ksft_eq(queues, expected)
+
+        expected = curr_queues
+        cmd(f"ethtool -L {cfg.dev['ifname']} {rx_type} {expected}", timeout=10)
+        queues = nl_get_queues(cfg, nl)
+        ksft_eq(queues, expected)
+    except Exception as ex:
+        raise KsftSkipEx(ex)
+
+
+def main() -> None:
+    with NetDrvEnv(__file__, queue_count=3) as cfg:
+        ksft_run([get_queues, addremove_queues], args=(cfg, NetdevFamily()))
+
+
+if __name__ == "__main__":
+    main()
diff --git a/tools/testing/selftests/net/lib/py/nsim.py b/tools/testing/selftests/net/lib/py/nsim.py
index 06896cdf7c18..f571a8b3139b 100644
--- a/tools/testing/selftests/net/lib/py/nsim.py
+++ b/tools/testing/selftests/net/lib/py/nsim.py
@@ -49,7 +49,7 @@  class NetdevSimDev:
         with open(fullpath, "w") as f:
             f.write(val)
 
-    def __init__(self, port_count=1, ns=None):
+    def __init__(self, port_count=1, queue_count=1, ns=None):
         # nsim will spawn in init_net, we'll set to actual ns once we switch it there
         self.ns = None
 
@@ -59,7 +59,7 @@  class NetdevSimDev:
         addr = random.randrange(1 << 15)
         while True:
             try:
-                self.ctrl_write("new_device", "%u %u" % (addr, port_count))
+                self.ctrl_write("new_device", "%u %u %u" % (addr, port_count, queue_count))
             except OSError as e:
                 if e.errno == errno.ENOSPC:
                     addr = random.randrange(1 << 15)
diff --git a/tools/testing/selftests/net/lib/py/utils.py b/tools/testing/selftests/net/lib/py/utils.py
index d3715e6c21f2..77e4f5a00eda 100644
--- a/tools/testing/selftests/net/lib/py/utils.py
+++ b/tools/testing/selftests/net/lib/py/utils.py
@@ -8,7 +8,7 @@  import time
 
 
 class cmd:
-    def __init__(self, comm, shell=True, fail=True, ns=None, background=False, host=None):
+    def __init__(self, comm, shell=True, fail=True, ns=None, background=False, host=None, timeout=5):
         if ns:
             comm = f'ip netns exec {ns} ' + comm
 
@@ -23,12 +23,12 @@  class cmd:
             self.proc = subprocess.Popen(comm, shell=shell, stdout=subprocess.PIPE,
                                          stderr=subprocess.PIPE)
         if not background:
-            self.process(terminate=False, fail=fail)
+            self.process(terminate=False, fail=fail, timeout=timeout)
 
-    def process(self, terminate=True, fail=None):
+    def process(self, terminate=True, fail=None, timeout=5):
         if terminate:
             self.proc.terminate()
-        stdout, stderr = self.proc.communicate(timeout=5)
+        stdout, stderr = self.proc.communicate(timeout)
         self.stdout = stdout.decode("utf-8")
         self.stderr = stderr.decode("utf-8")
         self.proc.stdout.close()