Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests: drv-net: define endpoint structures

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)

Reviewed-by: Willem de Bruijn <willemb@google.com>
Link: https://lore.kernel.org/r/20240420025237.3309296-2-kuba@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

+85 -8
+1
tools/testing/selftests/drivers/net/lib/py/__init__.py
··· 15 15 sys.exit(4) 16 16 17 17 from .env import * 18 + from .remote import Remote
+15
tools/testing/selftests/drivers/net/lib/py/remote.py
··· 1 + # SPDX-License-Identifier: GPL-2.0 2 + 3 + import os 4 + import importlib 5 + 6 + _modules = {} 7 + 8 + def Remote(kind, args, src_path): 9 + global _modules 10 + 11 + if kind not in _modules: 12 + _modules[kind] = importlib.import_module("..remote_" + kind, __name__) 13 + 14 + dir_path = os.path.abspath(src_path + "/../") 15 + return getattr(_modules[kind], "Remote")(args, dir_path)
+21
tools/testing/selftests/drivers/net/lib/py/remote_netns.py
··· 1 + # SPDX-License-Identifier: GPL-2.0 2 + 3 + import os 4 + import subprocess 5 + 6 + from lib.py import cmd 7 + 8 + 9 + class Remote: 10 + def __init__(self, name, dir_path): 11 + self.name = name 12 + self.dir_path = dir_path 13 + 14 + def cmd(self, comm): 15 + return subprocess.Popen(["ip", "netns", "exec", self.name, "bash", "-c", comm], 16 + stdout=subprocess.PIPE, stderr=subprocess.PIPE) 17 + 18 + def deploy(self, what): 19 + if os.path.isabs(what): 20 + return what 21 + return os.path.abspath(self.dir_path + "/" + what)
+39
tools/testing/selftests/drivers/net/lib/py/remote_ssh.py
··· 1 + # SPDX-License-Identifier: GPL-2.0 2 + 3 + import os 4 + import string 5 + import subprocess 6 + import random 7 + 8 + from lib.py import cmd 9 + 10 + 11 + class Remote: 12 + def __init__(self, name, dir_path): 13 + self.name = name 14 + self.dir_path = dir_path 15 + self._tmpdir = None 16 + 17 + def __del__(self): 18 + if self._tmpdir: 19 + cmd("rm -rf " + self._tmpdir, host=self) 20 + self._tmpdir = None 21 + 22 + def cmd(self, comm): 23 + return subprocess.Popen(["ssh", "-q", self.name, comm], 24 + stdout=subprocess.PIPE, stderr=subprocess.PIPE) 25 + 26 + def _mktmp(self): 27 + return ''.join(random.choice(string.ascii_lowercase) for _ in range(8)) 28 + 29 + def deploy(self, what): 30 + if not self._tmpdir: 31 + self._tmpdir = "/tmp/" + self._mktmp() 32 + cmd("mkdir " + self._tmpdir, host=self) 33 + file_name = self._tmpdir + "/" + self._mktmp() + os.path.basename(what) 34 + 35 + if not os.path.isabs(what): 36 + what = os.path.abspath(self.dir_path + "/" + what) 37 + 38 + cmd(f"scp {what} {self.name}:{file_name}") 39 + return file_name
+9 -8
tools/testing/selftests/net/lib/py/utils.py
··· 4 4 import subprocess 5 5 6 6 class cmd: 7 - def __init__(self, comm, shell=True, fail=True, ns=None, background=False): 7 + def __init__(self, comm, shell=True, fail=True, ns=None, background=False, host=None): 8 8 if ns: 9 - if isinstance(ns, NetNS): 10 - ns = ns.name 11 9 comm = f'ip netns exec {ns} ' + comm 12 10 13 11 self.stdout = None ··· 13 15 self.ret = None 14 16 15 17 self.comm = comm 16 - self.proc = subprocess.Popen(comm, shell=shell, stdout=subprocess.PIPE, 17 - stderr=subprocess.PIPE) 18 + if host: 19 + self.proc = host.cmd(comm) 20 + else: 21 + self.proc = subprocess.Popen(comm, shell=shell, stdout=subprocess.PIPE, 22 + stderr=subprocess.PIPE) 18 23 if not background: 19 24 self.process(terminate=False, fail=fail) 20 25 21 26 def process(self, terminate=True, fail=None): 22 27 if terminate: 23 28 self.proc.terminate() 24 - stdout, stderr = self.proc.communicate() 29 + stdout, stderr = self.proc.communicate(timeout=5) 25 30 self.stdout = stdout.decode("utf-8") 26 31 self.stderr = stderr.decode("utf-8") 27 32 self.proc.stdout.close() ··· 38 37 (self.proc.args, stdout, stderr)) 39 38 40 39 41 - def ip(args, json=None, ns=None): 40 + def ip(args, json=None, ns=None, host=None): 42 41 cmd_str = "ip " 43 42 if json: 44 43 cmd_str += '-j ' 45 44 cmd_str += args 46 - cmd_obj = cmd(cmd_str, ns=ns) 45 + cmd_obj = cmd(cmd_str, ns=ns, host=host) 47 46 if json: 48 47 return _json.loads(cmd_obj.stdout) 49 48 return cmd_obj