Skip to contentSkip to conversation

selftests: drv-net: support testing with a remote system

PATCH Superseded Jakub Kicinski 3 participants 10 messages Apr 16, 2024 – Apr 17, 2024

Subsystems (3)

v2 of 5 Superseded 2 of 6 applied Latest is v5 →

lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

v2 cover selftests: drv-net: support testing with a remote system

Hi!

Implement support for tests which require access to a remote system / endpoint which can generate traffic.
This series concludes the "groundwork" for upstream driver tests.

I wanted to support the three models which came up in discussions:
- SW testing with netdevsim
- "local" testing with two ports on the same system in a loopback
- "remote" testing via SSH
so there is a tiny bit of an abstraction which wraps up how "remote" commands are executed. Otherwise hopefully there's nothing surprising.

I'm only adding a ping test. I had a bigger one written but I was worried we'll get into discussing the details of the test itself and how I chose to hack up netdevsim, instead of the test infra... So that test will be a follow up :)

v2:
- rename endpoint -> remote
- use 2001:db8:: v6 prefix
- add a note about persistent SSH connections
- add the kernel config
v1: https://lore.kernel.org/all/[email protected]

Jakub Kicinski (6):
selftests: drv-net: add stdout to the command failed exception
selftests: drv-net: add config for netdevsim
selftests: drv-net: define endpoint structures
selftests: drv-net: factor out parsing of the env
selftests: drv-net: construct environment for running tests which
require an endpoint
selftests: drv-net: add a trivial ping test

 tools/testing/selftests/drivers/net/Makefile  |   5 +-
 .../testing/selftests/drivers/net/README.rst  |  33 ++++
 tools/testing/selftests/drivers/net/config    |   2 +
 .../selftests/drivers/net/lib/py/__init__.py  |   1 +
 .../selftests/drivers/net/lib/py/env.py       | 141 +++++++++++++++---
 .../selftests/drivers/net/lib/py/remote.py    |  13 ++
 .../drivers/net/lib/py/remote_netns.py        |  15 ++
 .../drivers/net/lib/py/remote_ssh.py          |  34 +++++
 tools/testing/selftests/drivers/net/ping.py   |  32 ++++
 .../testing/selftests/net/lib/py/__init__.py  |   1 +
 tools/testing/selftests/net/lib/py/netns.py   |  31 ++++
 tools/testing/selftests/net/lib/py/utils.py   |  22 +--
 12 files changed, 301 insertions(+), 29 deletions(-)
 create mode 100644 tools/testing/selftests/drivers/net/config
 create mode 100644 tools/testing/selftests/drivers/net/lib/py/remote.py
 create mode 100644 tools/testing/selftests/drivers/net/lib/py/remote_netns.py
 create mode 100644 tools/testing/selftests/drivers/net/lib/py/remote_ssh.py
 create mode 100755 tools/testing/selftests/drivers/net/ping.py
 create mode 100644 tools/testing/selftests/net/lib/py/netns.py
Signature · 1 line
2.44.0
author maintainer
#1 lore raw

PATCH v2 1/6 selftests: drv-net: add stdout to the command failed exception

ping prints all the info to stdout. To make debug easier capture stdout in the Exception raised when command unexpectedly fails.

---
 tools/testing/selftests/net/lib/py/utils.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/net/lib/py/utils.py +2 -1
index f0d425731fd4..19612348c30d 100644
@@ -33,7 +33,8 @@ import subprocess
         if self.proc.returncode != 0 and fail:
             if len(stderr) > 0 and stderr[-1] == "\n":
                 stderr = stderr[:-1]
-            raise Exception("Command failed: %s\n%s" % (self.proc.args, stderr))
+            raise Exception("Command failed: %s\nSTDOUT: %s\nSTDERR: %s" %
+                            (self.proc.args, stdout, stderr))
 
 
 def ip(args, json=None, ns=None):
Signature · 1 line
2.44.0
author maintainer
#2 lore raw

PATCH v2 2/6 selftests: drv-net: add config for netdevsim

In reply to Jakub Kicinski

Real driver testing will obviously require enabling more options, but will require more manual setup in the first place. For CIs running purely software tests we need to enable netdevsim.

---
 tools/testing/selftests/drivers/net/config | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 tools/testing/selftests/drivers/net/config
tools/testing/selftests/drivers/net/config new file +2 -0
new file mode 100644
index 000000000000..f6a58ce8a230
@@ -0,0 +1,2 @@
+CONFIG_IPV6=y
+CONFIG_NETDEVSIM=m
Signature · 1 line
2.44.0
author maintainer
#3 lore raw

PATCH v2 3/6 selftests: drv-net: define endpoint structures

In reply to Jakub Kicinski

Define the remote endpoint "model". To execute most meaningful device driver tests we need to be able to communicate with a remote system, and have it send traffic to the device under test.

Various test environments will have different requirements.

0) "Local" netdevsim-based testing can simply use net namespaces. netdevsim supports connecting two devices now, to form a veth-like construct.

1) Similarly on hosts with multiple NICs, the NICs may be connected together with a loopback cable or internal device loopback. One interface may be placed into separate netns, and tests would proceed much like in the netdevsim case. Note that the loopback config or the moving of one interface
into a netns is not expected to be part of selftest code.

2) Some systems may need to communicate with the remote endpoint via SSH.

3) Last but not least environment may have its own custom communication method.

Fundamentally we only need two operations:
- run a command remotely
- deploy a binary (if some tool we need is built as part of kselftests)

Wrap these two in a class. Use dynamic loading to load the Remote class. This will allow very easy definition of other communication methods without bothering upstream code base.

Stick to the "simple" / "no unnecessary abstractions" model for referring to the remote endpoints. The host / remote object are passed as an argument to the usual cmd() or ip() invocation. For example:

ip("link show", json=True, host=remote)

---
 .../selftests/drivers/net/lib/py/__init__.py  |  1 +
 .../selftests/drivers/net/lib/py/remote.py    | 13 +++++++
 .../drivers/net/lib/py/remote_netns.py        | 15 ++++++++
 .../drivers/net/lib/py/remote_ssh.py          | 34 +++++++++++++++++++
 tools/testing/selftests/net/lib/py/utils.py   | 19 ++++++-----
 5 files changed, 73 insertions(+), 9 deletions(-)
 create mode 100644 tools/testing/selftests/drivers/net/lib/py/remote.py
 create mode 100644 tools/testing/selftests/drivers/net/lib/py/remote_netns.py
 create mode 100644 tools/testing/selftests/drivers/net/lib/py/remote_ssh.py
tools/testing/selftests/drivers/net/lib/py/__init__.py +1 -0
index 4653dffcd962..4789c1a4282d 100644
@@ -15,3 +15,4 @@ KSFT_DIR = (Path(__file__).parent / "../../../..").resolve()
     sys.exit(4)
 
 from .env import *
+from .remote import Remote
tools/testing/selftests/drivers/net/lib/py/remote.py new file +13 -0
new file mode 100644
index 000000000000..d86b997d27d4
@@ -0,0 +1,13 @@
+# SPDX-License-Identifier: GPL-2.0
+
+import importlib
+
+_modules = {}
+
+def Remote(kind, args):
+    global _modules
+
+    if kind not in _modules:
+        _modules[kind] = importlib.import_module("..remote_" + kind, __name__)
+
+    return getattr(_modules[kind], "Remote")(args)
tools/testing/selftests/drivers/net/lib/py/remote_netns.py new file +15 -0
new file mode 100644
index 000000000000..7d8ab7a1bf92
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0
+
+from lib.py import cmd
+
+
+class Remote:
+    def __init__(self, name):
+        self.name = name
+
+    def cmd(self, *args):
+        c = cmd(*args, ns=self.name)
+        return c.stdout, c.stderr, c.ret
+
+    def deploy(self, what):
+        return what
tools/testing/selftests/drivers/net/lib/py/remote_ssh.py new file +34 -0
new file mode 100644
index 000000000000..c056b15991ff
@@ -0,0 +1,34 @@
+# SPDX-License-Identifier: GPL-2.0
+
+import os
+import shlex
+import string
+import random
+
+from lib.py import cmd
+
+
+class Remote:
+    def __init__(self, name):
+        self.name = name
+        self._tmpdir = None
+
+    def __del__(self):
+        if self._tmpdir:
+            self.cmd("rm -rf " + self._tmpdir)
+            self._tmpdir = None
+
+    def cmd(self, comm, *args):
+        c = cmd("ssh " + self.name + " " + shlex.quote(comm), *args)
+        return c.stdout, c.stderr, c.ret
+
+    def _mktmp(self):
+        return ''.join(random.choice(string.ascii_lowercase) for _ in range(8))
+
+    def deploy(self, what):
+        if not self._tmpdir:
+            self._tmpdir = "/tmp/" + self._mktmp()
+            self.cmd("mkdir " + self._tmpdir)
+        file_name = self._tmpdir + "/" + self._mktmp() + os.path.basename(what)
+        cmd(f"scp {what} {self.name}:{file_name}")
+        return file_name
tools/testing/selftests/net/lib/py/utils.py +10 -9
index 19612348c30d..d47d684d9e02 100644
@@ -4,10 +4,8 @@ import json as _json
 import subprocess
 
 class cmd:
-    def __init__(self, comm, shell=True, fail=True, ns=None, background=False):
+    def __init__(self, comm, shell=True, fail=True, ns=None, background=False, host=None):
         if ns:
-            if isinstance(ns, NetNS):
-                ns = ns.name
             comm = f'ip netns exec {ns} ' + comm
 
         self.stdout = None
@@ -15,10 +13,13 @@ import subprocess
         self.ret = None
 
         self.comm = comm
-        self.proc = subprocess.Popen(comm, shell=shell, stdout=subprocess.PIPE,
-                                     stderr=subprocess.PIPE)
-        if not background:
-            self.process(terminate=False, fail=fail)
+        if host:
+            self.stdout, self.stderr, self.ret = host.cmd(comm)
+        else:
+            self.proc = subprocess.Popen(comm, shell=shell, stdout=subprocess.PIPE,
+                                         stderr=subprocess.PIPE)
+            if not background:
+                self.process(terminate=False, fail=fail)
 
     def process(self, terminate=True, fail=None):
         if terminate:
@@ -37,12 +38,12 @@ import subprocess
                             (self.proc.args, stdout, stderr))
 
 
-def ip(args, json=None, ns=None):
+def ip(args, json=None, ns=None, host=None):
     cmd_str = "ip "
     if json:
         cmd_str += '-j '
     cmd_str += args
-    cmd_obj = cmd(cmd_str, ns=ns)
+    cmd_obj = cmd(cmd_str, ns=ns, host=host)
     if json:
         return _json.loads(cmd_obj.stdout)
     return cmd_obj
Signature · 1 line
2.44.0
author maintainer
#4 lore raw

PATCH v2 4/6 selftests: drv-net: factor out parsing of the env

In reply to Jakub Kicinski

The tests with a remote end will use a different class, for clarity, but will also need to parse the env. So factor parsing the env out to a function.

---
 .../selftests/drivers/net/lib/py/env.py       | 43 +++++++++++--------
 1 file changed, 26 insertions(+), 17 deletions(-)
tools/testing/selftests/drivers/net/lib/py/env.py +26 -17
index e1abe9491daf..a081e168f3db 100644
@@ -6,12 +6,36 @@ from pathlib import Path
 from lib.py import ip
 from lib.py import NetdevSimDev
 
+
+def _load_env_file(src_path):
+    env = os.environ.copy()
+
+    src_dir = Path(src_path).parent.resolve()
+    if not (src_dir / "net.config").exists():
+        return env
+
+    lexer = shlex.shlex(open((src_dir / "net.config").as_posix(), 'r').read())
+    k = None
+    for token in lexer:
+        if k is None:
+            k = token
+            env[k] = ""
+        elif token == "=":
+            pass
+        else:
+            env[k] = token
+            k = None
+    return env
+
+
 class NetDrvEnv:
+    """
+    Class for a single NIC / host env, with no remote end
+    """
     def __init__(self, src_path):
         self._ns = None
 
-        self.env = os.environ.copy()
-        self._load_env_file(src_path)
+        self.env = _load_env_file(src_path)
 
         if 'NETIF' in self.env:
             self.dev = ip("link show dev " + self.env['NETIF'], json=True)[0]
@@ -34,19 +58,4 @@ from lib.py import NetdevSimDev
             self._ns.remove()
             self._ns = None
 
-    def _load_env_file(self, src_path):
-        src_dir = Path(src_path).parent.resolve()
-        if not (src_dir / "net.config").exists():
-            return
 
-        lexer = shlex.shlex(open((src_dir / "net.config").as_posix(), 'r').read())
-        k = None
-        for token in lexer:
-            if k is None:
-                k = token
-                self.env[k] = ""
-            elif token == "=":
-                pass
-            else:
-                self.env[k] = token
-                k = None
Signature · 1 line
2.44.0
author maintainer
#5 lore raw

PATCH v2 5/6 selftests: drv-net: construct environment for running tests which require an endpoint

In reply to Jakub Kicinski

Nothing surprising here, hopefully. Wrap the variables from the environment into a class or spawn a netdevsim based env and pass it to the tests.

---
 .../testing/selftests/drivers/net/README.rst  | 33 +++++++
 .../selftests/drivers/net/lib/py/env.py       | 98 ++++++++++++++++++-
 .../testing/selftests/net/lib/py/__init__.py  |  1 +
 tools/testing/selftests/net/lib/py/netns.py   | 31 ++++++
 4 files changed, 162 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/net/lib/py/netns.py
tools/testing/selftests/drivers/net/README.rst +33 -0
index 5ef7c417d431..0cbab33dad1f 100644
@@ -23,8 +23,41 @@ Variables can be set in the environment or by creating a net.config
   # Variable set in a file
   NETIF=eth0
 
+Please note that the config parser is very simple, if there are
+any non-alphanumeric characters in the value it needs to be in
+double quotes.
+
 NETIF
 ~~~~~
 
 Name of the netdevice against which the test should be executed.
 When empty or not set software devices will be used.
+
+LOCAL_V4, LOCAL_V6, REMOTE_V4, REMOTE_V6
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Local and remote endpoint IP addresses.
+
+REMOTE_TYPE
+~~~~~~~~~~~
+
+Communication method used to run commands on the remote endpoint.
+Test framework has built-in support for ``netns`` and ``ssh`` channels.
+``netns`` assumes the "remote" interface is part of the same
+host, just moved to the specified netns.
+``ssh`` communicates with remote endpoint over ``ssh`` and ``scp``.
+Using persistent SSH connections is strongly encouraged to avoid
+the latency of SSH connection setup on every command.
+
+Communication methods are defined by classes in ``lib/py/remote_{name}.py``.
+It should be possible to add a new method without modifying any of
+the framework, by simply adding an appropriately named file to ``lib/py``.
+
+REMOTE_ARGS
+~~~~~~~~~~~
+
+Arguments used to construct the communication channel.
+Communication channel dependent::
+
+  for netns - name of the "remote" namespace
+  for ssh - name/address of the remote host
tools/testing/selftests/drivers/net/lib/py/env.py +97 -1

107 lines not on this page · raw message

tools/testing/selftests/net/lib/py/__init__.py +1 -0
index ded7102df18a..b6d498d125fe 100644
@@ -2,6 +2,7 @@
 
 from .consts import KSRC
 from .ksft import *
+from .netns import NetNS
 from .nsim import *
 from .utils import *
 from .ynl import NlError, YnlFamily, EthtoolFamily, NetdevFamily, RtnlFamily
tools/testing/selftests/net/lib/py/netns.py new file +31 -0
new file mode 100644
index 000000000000..ecff85f9074f
@@ -0,0 +1,31 @@
+# SPDX-License-Identifier: GPL-2.0
+
+from .utils import ip
+import random
+import string
+
+
+class NetNS:
+    def __init__(self, name=None):
+        if name:
+            self.name = name
+        else:
+            self.name = ''.join(random.choice(string.ascii_lowercase) for _ in range(8))
+        ip('netns add ' + self.name)
+
+    def __del__(self):
+        if self.name:
+            ip('netns del ' + self.name)
+            self.name = None
+
+    def __enter__(self):
+        return self
+
+    def __exit__(self, ex_type, ex_value, ex_tb):
+        self.__del__()
+
+    def __str__(self):
+        return self.name
+
+    def __repr__(self):
+        return f"NetNS({self.name})"
Signature · 1 line
2.44.0
author maintainer
#6 lore raw

PATCH v2 6/6 selftests: drv-net: add a trivial ping test

In reply to Jakub Kicinski

Add a very simple test for testing with a remote system. Both IPv4 and IPv6 connectivity is optional so tests will XFail is env doesn't define an address for the given family.

Using netdevsim:

 $ ./run_kselftest.sh -t drivers/net:ping.py
 TAP version 13
 1..1
 # timeout set to 45
 # selftests: drivers/net: ping.py
 # KTAP version 1
 # 1..2
 # ok 1 ping.ping_v4
 # ok 2 ping.ping_v6
 # # Totals: pass:2 fail:0 xfail:0 xpass:0 skip:0 error:0
 ok 1 selftests: drivers/net: ping.py

Command line SSH:

 $ NETIF=virbr0 REMOTE_TYPE=ssh [email protected] \
    LOCAL_V4=192.168.122.1 REMOTE_V4=192.168.122.123 \
    ./tools/testing/selftests/drivers/net/ping.py
 KTAP version 1
 1..2
 ok 1 ping.ping_v4
 ok 2 ping.ping_v6 # XFAIL
 # Totals: pass:1 fail:0 xfail:1 xpass:0 skip:0 error:0

Existing devices placed in netns (and using net.config):

 $ cat drivers/net/net.config
 NETIF=veth0
 REMOTE_TYPE=netns
 REMOTE_ARGS=red
 LOCAL_V4="192.168.1.1"
 REMOTE_V4="192.168.1.2"
 $ ./run_kselftest.sh -t drivers/net:ping.py
 TAP version 13
 1..1
 # timeout set to 45
 # selftests: drivers/net: ping.py
 # KTAP version 1
 # 1..2
 # ok 1 ping.ping_v4
 # ok 2 ping.ping_v6 # XFAIL
 # # Totals: pass:1 fail:0 xfail:1 xpass:0 skip:0 error:0
---
 tools/testing/selftests/drivers/net/Makefile |  5 ++-
 tools/testing/selftests/drivers/net/ping.py  | 32 ++++++++++++++++++++
 2 files changed, 36 insertions(+), 1 deletion(-)
 create mode 100755 tools/testing/selftests/drivers/net/ping.py
tools/testing/selftests/drivers/net/Makefile +4 -1
index 379cdb1960a7..754ec643768a 100644
@@ -2,6 +2,9 @@
 
 TEST_INCLUDES := $(wildcard lib/py/*.py)
 
-TEST_PROGS := stats.py
+TEST_PROGS := \
+	ping.py \
+	stats.py \
+# end of TEST_PROGS
 
 include ../../lib.mk
tools/testing/selftests/drivers/net/ping.py new file +32 -0
new file mode 100755
index 000000000000..2d74f15a52a0
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+
+from lib.py import ksft_run, KsftXfailEx
+from lib.py import NetDrvEpEnv
+from lib.py import cmd
+
+
+def ping_v4(cfg) -> None:
+    if not cfg.v4:
+        raise KsftXfailEx()
+
+    cmd(f"ping -c 1 -W0.5 {cfg.remote_v4}")
+    cmd(f"ping -c 1 -W0.5 {cfg.v4}", host=cfg.remote)
+
+
+def ping_v6(cfg) -> None:
+    if not cfg.v6:
+        raise KsftXfailEx()
+
+    cmd(f"ping -c 1 -W0.5 {cfg.remote_v6}")
+    cmd(f"ping -c 1 -W0.5 {cfg.v6}", host=cfg.remote)
+
+
+def main() -> None:
+    with NetDrvEpEnv(__file__) as cfg:
+        ksft_run([ping_v4, ping_v6],
+                 args=(cfg, ))
+
+
+if __name__ == "__main__":
+    main()
Signature · 1 line
2.44.0
Reviewed-by on the v2 series
#7 lore raw
In reply to Jakub Kicinski
Quoted 25 lines · Jakub Kicinski

Hi!

Implement support for tests which require access to a remote system / endpoint which can generate traffic.
This series concludes the "groundwork" for upstream driver tests.

I wanted to support the three models which came up in discussions:
- SW testing with netdevsim
- "local" testing with two ports on the same system in a loopback
- "remote" testing via SSH
so there is a tiny bit of an abstraction which wraps up how "remote" commands are executed. Otherwise hopefully there's nothing surprising.

I'm only adding a ping test. I had a bigger one written but I was worried we'll get into discussing the details of the test itself and how I chose to hack up netdevsim, instead of the test infra... So that test will be a follow up :)

v2:
- rename endpoint -> remote
- use 2001:db8:: v6 prefix
- add a note about persistent SSH connections
- add the kernel config
v1: https://lore.kernel.org/all/[email protected]

Quoted 8 lines

Jakub Kicinski (6):
selftests: drv-net: add stdout to the command failed exception
selftests: drv-net: add config for netdevsim
selftests: drv-net: define endpoint structures
selftests: drv-net: factor out parsing of the env
selftests: drv-net: construct environment for running tests which
require an endpoint
selftests: drv-net: add a trivial ping test

Thanks for humoring the naming suggestions. Exciting to have this infra.

author maintainer
#8 lore raw
Quoted 1 line · On Tue, 16 Apr 2024 11:07:08 -0400 Willem de Bruijn wrote:

Thanks for humoring the naming suggestions. Exciting to have this infra.

I wrote a couple more tests today and as you predicted already needed wait_port_listen()... I also discovered a few sharp edges with the way the commands are wrapped for the remote host. I'll apply the first two patches for now, and repost the rest tomorrow/Thu once I gain more confidence.

#9 lore raw
In reply to Jakub Kicinski
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 6 lines · On Mon, 15 Apr 2024 17:45:50 -0700 you wrote:

Hi!

Implement support for tests which require access to a remote system / endpoint which can generate traffic.
This series concludes the "groundwork" for upstream driver tests.

Quoted 8 lines

I wanted to support the three models which came up in discussions:
- SW testing with netdevsim
- "local" testing with two ports on the same system in a loopback
- "remote" testing via SSH
so there is a tiny bit of an abstraction which wraps up how "remote" commands are executed. Otherwise hopefully there's nothing surprising.

[...]

Here is the summary with links:
- [net-next,v2,1/6] selftests: drv-net: add stdout to the command failed exception

    https://git.kernel.org/netdev/net-next/c/232d79aaa781
  - [net-next,v2,2/6] selftests: drv-net: add config for netdevsim
    https://git.kernel.org/netdev/net-next/c/438ce84bae90

- [net-next,v2,3/6] selftests: drv-net: define endpoint structures
(no matching commit)
- [net-next,v2,4/6] selftests: drv-net: factor out parsing of the env
(no matching commit)
- [net-next,v2,5/6] selftests: drv-net: construct environment for running tests which require an endpoint (no matching commit)
- [net-next,v2,6/6] selftests: drv-net: add a trivial ping test
(no matching commit)

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html