diff mbox series

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

Message ID 20240419220857.2065615-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 shuah@kernel.org linux-kselftest@vger.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 fail net-next-2024-04-20--00-00 (tests: 957)

Commit Message

David Wei April 19, 2024, 10:08 p.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.

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 | 59 +++++++++++++++++++
 tools/testing/selftests/net/lib/py/nsim.py    |  4 +-
 4 files changed, 66 insertions(+), 4 deletions(-)
 create mode 100755 tools/testing/selftests/drivers/net/queues.py

Comments

Willem de Bruijn April 21, 2024, 2:56 p.m. UTC | #1
David Wei wrote:
> 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.
> 
> 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 | 59 +++++++++++++++++++
>  tools/testing/selftests/net/lib/py/nsim.py    |  4 +-
>  4 files changed, 66 insertions(+), 4 deletions(-)
>  create mode 100755 tools/testing/selftests/drivers/net/queues.py
> 
> diff --git a/tools/testing/selftests/drivers/net/Makefile b/tools/testing/selftests/drivers/net/Makefile
> index 379cdb1960a7..118a73650dbc 100644
> --- a/tools/testing/selftests/drivers/net/Makefile
> +++ b/tools/testing/selftests/drivers/net/Makefile
> @@ -3,5 +3,6 @@
>  TEST_INCLUDES := $(wildcard lib/py/*.py)
>  
>  TEST_PROGS := stats.py
> +TEST_PROGS += queues.py
>  
>  include ../../lib.mk
> diff --git a/tools/testing/selftests/drivers/net/lib/py/env.py b/tools/testing/selftests/drivers/net/lib/py/env.py
> index e1abe9491daf..0ac4e9e6cd84 100644
> --- a/tools/testing/selftests/drivers/net/lib/py/env.py
> +++ b/tools/testing/selftests/drivers/net/lib/py/env.py
> @@ -7,7 +7,7 @@ from lib.py import ip
>  from lib.py import NetdevSimDev
>  
>  class NetDrvEnv:
> -    def __init__(self, src_path):
> +    def __init__(self, src_path, **kwargs):
>          self._ns = None
>  
>          self.env = os.environ.copy()
> @@ -16,11 +16,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..c23cd5a932cb
> --- /dev/null
> +++ b/tools/testing/selftests/drivers/net/queues.py
> @@ -0,0 +1,59 @@
> +#!/usr/bin/env python3
> +# SPDX-License-Identifier: GPL-2.0
> +
> +from lib.py import ksft_run, ksft_eq, KsftSkipEx
> +from lib.py import 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:
> +    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)
> +

This is a copy of get_queues() above

> +    # reduce queue count by 1
> +    expected = expected - 1

Verify first that queue count > 1. Which it isn't in the test setup.

> +    cmd(f"ethtool -L {cfg.dev['ifname']} combined {expected}")
> +    queues = nl_get_queues(cfg, nl)
> +    ksft_eq(queues, expected)
> +
> +    # increase queue count by 1
> +    expected = expected + 1
> +    cmd(f"ethtool -L {cfg.dev['ifname']} combined {expected}")
> +    queues = nl_get_queues(cfg, nl)
> +    ksft_eq(queues, expected)
> +
> +
> +def main() -> None:
> +    with NetDrvEnv(__file__, queue_count=3) as cfg:
> +        ksft_run([get_queues, addremove_queues], args=(cfg, NetdevFamily()))
> +
> +
David Wei April 23, 2024, 8:04 p.m. UTC | #2
On 2024-04-21 7:56 am, Willem de Bruijn wrote:
> David Wei wrote:
>> 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.
>>
>> 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 | 59 +++++++++++++++++++
>>  tools/testing/selftests/net/lib/py/nsim.py    |  4 +-
>>  4 files changed, 66 insertions(+), 4 deletions(-)
>>  create mode 100755 tools/testing/selftests/drivers/net/queues.py
>>
>> diff --git a/tools/testing/selftests/drivers/net/Makefile b/tools/testing/selftests/drivers/net/Makefile
>> index 379cdb1960a7..118a73650dbc 100644
>> --- a/tools/testing/selftests/drivers/net/Makefile
>> +++ b/tools/testing/selftests/drivers/net/Makefile
>> @@ -3,5 +3,6 @@
>>  TEST_INCLUDES := $(wildcard lib/py/*.py)
>>  
>>  TEST_PROGS := stats.py
>> +TEST_PROGS += queues.py
>>  
>>  include ../../lib.mk
>> diff --git a/tools/testing/selftests/drivers/net/lib/py/env.py b/tools/testing/selftests/drivers/net/lib/py/env.py
>> index e1abe9491daf..0ac4e9e6cd84 100644
>> --- a/tools/testing/selftests/drivers/net/lib/py/env.py
>> +++ b/tools/testing/selftests/drivers/net/lib/py/env.py
>> @@ -7,7 +7,7 @@ from lib.py import ip
>>  from lib.py import NetdevSimDev
>>  
>>  class NetDrvEnv:
>> -    def __init__(self, src_path):
>> +    def __init__(self, src_path, **kwargs):
>>          self._ns = None
>>  
>>          self.env = os.environ.copy()
>> @@ -16,11 +16,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..c23cd5a932cb
>> --- /dev/null
>> +++ b/tools/testing/selftests/drivers/net/queues.py
>> @@ -0,0 +1,59 @@
>> +#!/usr/bin/env python3
>> +# SPDX-License-Identifier: GPL-2.0
>> +
>> +from lib.py import ksft_run, ksft_eq, KsftSkipEx
>> +from lib.py import 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:
>> +    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)
>> +
> 
> This is a copy of get_queues() above

I'll remove this part of the test case.

> 
>> +    # reduce queue count by 1
>> +    expected = expected - 1
> 
> Verify first that queue count > 1. Which it isn't in the test setup.

Thanks, I'll make sure changing the queues for a real device doesn't go
out of bounds.

> 
>> +    cmd(f"ethtool -L {cfg.dev['ifname']} combined {expected}")
>> +    queues = nl_get_queues(cfg, nl)
>> +    ksft_eq(queues, expected)
>> +
>> +    # increase queue count by 1
>> +    expected = expected + 1
>> +    cmd(f"ethtool -L {cfg.dev['ifname']} combined {expected}")
>> +    queues = nl_get_queues(cfg, nl)
>> +    ksft_eq(queues, expected)
>> +
>> +
>> +def main() -> None:
>> +    with NetDrvEnv(__file__, queue_count=3) as cfg:
>> +        ksft_run([get_queues, addremove_queues], args=(cfg, NetdevFamily()))
>> +
>> +
> 
>
diff mbox series

Patch

diff --git a/tools/testing/selftests/drivers/net/Makefile b/tools/testing/selftests/drivers/net/Makefile
index 379cdb1960a7..118a73650dbc 100644
--- a/tools/testing/selftests/drivers/net/Makefile
+++ b/tools/testing/selftests/drivers/net/Makefile
@@ -3,5 +3,6 @@ 
 TEST_INCLUDES := $(wildcard lib/py/*.py)
 
 TEST_PROGS := stats.py
+TEST_PROGS += queues.py
 
 include ../../lib.mk
diff --git a/tools/testing/selftests/drivers/net/lib/py/env.py b/tools/testing/selftests/drivers/net/lib/py/env.py
index e1abe9491daf..0ac4e9e6cd84 100644
--- a/tools/testing/selftests/drivers/net/lib/py/env.py
+++ b/tools/testing/selftests/drivers/net/lib/py/env.py
@@ -7,7 +7,7 @@  from lib.py import ip
 from lib.py import NetdevSimDev
 
 class NetDrvEnv:
-    def __init__(self, src_path):
+    def __init__(self, src_path, **kwargs):
         self._ns = None
 
         self.env = os.environ.copy()
@@ -16,11 +16,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..c23cd5a932cb
--- /dev/null
+++ b/tools/testing/selftests/drivers/net/queues.py
@@ -0,0 +1,59 @@ 
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+
+from lib.py import ksft_run, ksft_eq, KsftSkipEx
+from lib.py import 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:
+    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)
+
+    # reduce queue count by 1
+    expected = expected - 1
+    cmd(f"ethtool -L {cfg.dev['ifname']} combined {expected}")
+    queues = nl_get_queues(cfg, nl)
+    ksft_eq(queues, expected)
+
+    # increase queue count by 1
+    expected = expected + 1
+    cmd(f"ethtool -L {cfg.dev['ifname']} combined {expected}")
+    queues = nl_get_queues(cfg, nl)
+    ksft_eq(queues, expected)
+
+
+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)