From patchwork Thu Apr 18 23:38:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 790016 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 21E3F75801; Thu, 18 Apr 2024 23:38:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713483531; cv=none; b=EZkqgwnP2dhB7Rk20+7bpTR56i8GSoTMVKbQR0nsnuYqhihVt8NIBGlMUaFzmekyNQEi3ayyYCVDgIaxqRCtdXB2YjIf8XsPkfzcIbh+0hlLpyTS7yOqpAfP4ZZhW5/r5KIfT4t/8mnRUlKUat5ZY/pOqNHqqugEOuDycakjE4Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713483531; c=relaxed/simple; bh=QlfTl8B8VBNZuMl0BUCRJD+FfKSrEnCFkSc0QRCUxYg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KMXs1UAbzSP1G//VmUzDrcrKmVgriRu01JWKdIHP2xEApYjYOlgswqgGt0T8eNuq2c2o3cxl3yVWUyqsOtMDC1TWxDqMaXBLIjUhZTXC4ghr30GeBUpCFy9AZ+nUlxfqrHCYfwwe0NCifl2bWsAxkcdi4WEChcCMIQaSA1fd/PY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FgLMfbs1; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FgLMfbs1" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3FA33C3277B; Thu, 18 Apr 2024 23:38:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713483530; bh=QlfTl8B8VBNZuMl0BUCRJD+FfKSrEnCFkSc0QRCUxYg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FgLMfbs1E7go1Aq3ZbpBS7vCfSd4EOk2+Ea7AbV5Yz1itwJrVUrDjvtgmTGBHBvzh aGNzDtfLr3hsv8fgLux/H6aN8ze1WfEn8eM+qI5dXJc6N0kW48t96MaUxMmMOfThOb bxuuI3K9fakP5/j+WzdLlV1eAK8d1TQZ8uWydvviafWD+SuS5TMuXuztcb9mRUyIzT WgpeiOg6MuwzFM9lJ7F98HFTb+8BvN3eGxXg+eVD82D2mn5i/KW1k40vwGkU5LUUuJ mjPSriPtj/VXi2bazAAw1E8VqPytkUZ1529ZkF1WvNBFk+MbQo+jAh2+sbQVvqelSQ MZFFmV9cSyuTQ== From: Jakub Kicinski To: davem@davemloft.net Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com, shuah@kernel.org, petrm@nvidia.com, linux-kselftest@vger.kernel.org, willemdebruijn.kernel@gmail.com, Jakub Kicinski Subject: [PATCH net-next v4 1/7] selftests: drv-net: define endpoint structures Date: Thu, 18 Apr 2024 16:38:38 -0700 Message-ID: <20240418233844.2762396-2-kuba@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240418233844.2762396-1-kuba@kernel.org> References: <20240418233844.2762396-1-kuba@kernel.org> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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) Signed-off-by: Jakub Kicinski --- v4: - bump timeout to 5 sec, in case of really slow SSH v3: - make Remote() return Popen() object - always operate on absolute paths v2: - rename Endpoint -> Remote --- .../selftests/drivers/net/lib/py/__init__.py | 1 + .../selftests/drivers/net/lib/py/remote.py | 15 +++++++ .../drivers/net/lib/py/remote_netns.py | 21 ++++++++++ .../drivers/net/lib/py/remote_ssh.py | 39 +++++++++++++++++++ tools/testing/selftests/net/lib/py/utils.py | 17 ++++---- 5 files changed, 85 insertions(+), 8 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 diff --git a/tools/testing/selftests/drivers/net/lib/py/__init__.py b/tools/testing/selftests/drivers/net/lib/py/__init__.py index 4653dffcd962..4789c1a4282d 100644 --- a/tools/testing/selftests/drivers/net/lib/py/__init__.py +++ b/tools/testing/selftests/drivers/net/lib/py/__init__.py @@ -15,3 +15,4 @@ KSFT_DIR = (Path(__file__).parent / "../../../..").resolve() sys.exit(4) from .env import * +from .remote import Remote diff --git a/tools/testing/selftests/drivers/net/lib/py/remote.py b/tools/testing/selftests/drivers/net/lib/py/remote.py new file mode 100644 index 000000000000..b1780b987722 --- /dev/null +++ b/tools/testing/selftests/drivers/net/lib/py/remote.py @@ -0,0 +1,15 @@ +# SPDX-License-Identifier: GPL-2.0 + +import os +import importlib + +_modules = {} + +def Remote(kind, args, src_path): + global _modules + + if kind not in _modules: + _modules[kind] = importlib.import_module("..remote_" + kind, __name__) + + dir_path = os.path.abspath(src_path + "/../") + return getattr(_modules[kind], "Remote")(args, dir_path) diff --git a/tools/testing/selftests/drivers/net/lib/py/remote_netns.py b/tools/testing/selftests/drivers/net/lib/py/remote_netns.py new file mode 100644 index 000000000000..7d5eeb0271bc --- /dev/null +++ b/tools/testing/selftests/drivers/net/lib/py/remote_netns.py @@ -0,0 +1,21 @@ +# SPDX-License-Identifier: GPL-2.0 + +import os +import subprocess + +from lib.py import cmd + + +class Remote: + def __init__(self, name, dir_path): + self.name = name + self.dir_path = dir_path + + def cmd(self, comm): + return subprocess.Popen(["ip", "netns", "exec", self.name, "bash", "-c", comm], + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + + def deploy(self, what): + if os.path.isabs(what): + return what + return os.path.abspath(self.dir_path + "/" + what) diff --git a/tools/testing/selftests/drivers/net/lib/py/remote_ssh.py b/tools/testing/selftests/drivers/net/lib/py/remote_ssh.py new file mode 100644 index 000000000000..924addde19a3 --- /dev/null +++ b/tools/testing/selftests/drivers/net/lib/py/remote_ssh.py @@ -0,0 +1,39 @@ +# SPDX-License-Identifier: GPL-2.0 + +import os +import string +import subprocess +import random + +from lib.py import cmd + + +class Remote: + def __init__(self, name, dir_path): + self.name = name + self.dir_path = dir_path + self._tmpdir = None + + def __del__(self): + if self._tmpdir: + cmd("rm -rf " + self._tmpdir, host=self) + self._tmpdir = None + + def cmd(self, comm): + return subprocess.Popen(["ssh", "-q", self.name, comm], + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + + 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() + cmd("mkdir " + self._tmpdir, host=self) + file_name = self._tmpdir + "/" + self._mktmp() + os.path.basename(what) + + if not os.path.isabs(what): + what = os.path.abspath(self.dir_path + "/" + what) + + cmd(f"scp {what} {self.name}:{file_name}") + return file_name diff --git a/tools/testing/selftests/net/lib/py/utils.py b/tools/testing/selftests/net/lib/py/utils.py index 19612348c30d..7347d0c0ff05 100644 --- a/tools/testing/selftests/net/lib/py/utils.py +++ b/tools/testing/selftests/net/lib/py/utils.py @@ -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,15 +13,18 @@ import subprocess self.ret = None self.comm = comm - self.proc = subprocess.Popen(comm, shell=shell, stdout=subprocess.PIPE, - stderr=subprocess.PIPE) + if host: + self.proc = 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: self.proc.terminate() - stdout, stderr = self.proc.communicate() + stdout, stderr = self.proc.communicate(timeout=5) self.stdout = stdout.decode("utf-8") self.stderr = stderr.decode("utf-8") self.proc.stdout.close() @@ -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 From patchwork Thu Apr 18 23:38:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 790395 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C407379B96; Thu, 18 Apr 2024 23:38:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713483531; cv=none; b=OtHrEU44sNJiIC5AaOeRQ1PrrdZCzr8bzcFXhCLXcpGR6gzS+7na6d6HiFqHnbBzn3RV9HJ5+3Rn+hYKHNUiIOdApYk1coJS2Y/yGscDcwEpsKDpay/wmPAsKYUc+11iVhvTKsZLvBei30WZEfLllEQRSzUXJvWK2P4z2RHa3Kk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713483531; c=relaxed/simple; bh=U5+EVNFryCl6AbqRDTDECqncYZkgnEzOhvv/pOI/KNg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=avGoh97eSuo/USVo1jr7FeFBwodfe1nmyamOnDkinSGyWdrGXn2dtDJidhbTGVnAmzMVCOFlrMV/G8mLZAdqrn2XcXwXc4qkrUIGyU1m8Rz7dypSq8BLwZn8T6opH5n8892eVJyzT2skO/t5Dzjv8wyRMMr/CzEhh1mY7qYaPKM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=d81ZiJjI; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="d81ZiJjI" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D032CC32781; Thu, 18 Apr 2024 23:38:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713483531; bh=U5+EVNFryCl6AbqRDTDECqncYZkgnEzOhvv/pOI/KNg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=d81ZiJjI4B0kTsUCc8RXTi+4BefSMrM0D01lQAgXhaQiKwcpjgceENTQfIG/TRVIO bbFZvdoP6QbhVMYG9ex7WpildHhGuDvZSFEjTHhkm8+TP9vo/WsTntvurXlZ2+Ag+i QnEyHXMVm7+H2AjuuUMjcyZCVWE2IdoW0Ie/tq0dXnjpqLsISSOQRk8WrAxcluxtWl Hipzu9IQZ4miAeEBBsw+d6zoHaGdK5rJ1UwOblXiwkHoOsDt/MiN47g69APqaoCtj8 bOd2/+YHmabuDU2L1e70mUtytgb8l4hrLBsaNaYBha8s1AU4YLlQ4HmMrdOGbJs6wc mdzDs5qV7rjqw== From: Jakub Kicinski To: davem@davemloft.net Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com, shuah@kernel.org, petrm@nvidia.com, linux-kselftest@vger.kernel.org, willemdebruijn.kernel@gmail.com, Jakub Kicinski Subject: [PATCH net-next v4 2/7] selftests: drv-net: factor out parsing of the env Date: Thu, 18 Apr 2024 16:38:39 -0700 Message-ID: <20240418233844.2762396-3-kuba@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240418233844.2762396-1-kuba@kernel.org> References: <20240418233844.2762396-1-kuba@kernel.org> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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. Signed-off-by: Jakub Kicinski --- .../selftests/drivers/net/lib/py/env.py | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/tools/testing/selftests/drivers/net/lib/py/env.py b/tools/testing/selftests/drivers/net/lib/py/env.py index e1abe9491daf..a081e168f3db 100644 --- a/tools/testing/selftests/drivers/net/lib/py/env.py +++ b/tools/testing/selftests/drivers/net/lib/py/env.py @@ -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 From patchwork Thu Apr 18 23:38:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 790015 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4F7087BB07; Thu, 18 Apr 2024 23:38:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713483532; cv=none; b=oW4cxGdBhMuGPnhp+TEs3yTRzda8mYup49KZK1s9qM8rNHRwyFCrZdhijaWvO5wTZel7J8qYUMp44/z1JpESpWjWx/BpjytP1+RaJgkU2YzF13k7q6ySgHYWnLqitrwSB2TTWwPs2HwlwkiwKd9zIfQwcsj76pXpQ/QbN6COuog= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713483532; c=relaxed/simple; bh=LWouE9X7rzpJYEbBLAu3hDxweSMYx1hX1X8QWpU0+sk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dQq1ksJGJ1CwKJ/Zb8XMfbzib2sbdeboqYt7jqghQwdY0xKfQXI+dEat0g8YJsSmxwtKWa9FxfTSPeHtN0V8PR7e41qiwRGUk1IvSFkhVSghjmKiVz3xFF9YbwMUME5I9VlZMUnfwyHreg+PjoGtd5hwdDqxX6QNBRRr7Mkcplg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ApglZY4e; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ApglZY4e" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 69A6BC32786; Thu, 18 Apr 2024 23:38:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713483531; bh=LWouE9X7rzpJYEbBLAu3hDxweSMYx1hX1X8QWpU0+sk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ApglZY4e7OtH0625JeBLjoLJbNpLoRtLwwS/lPw3ysv09c6Jkapw+VtNUFyEXJXfY C6Kqp6Tnj/s3FM8Fvx3COEXUXHj1GHgiTkGm66p/Y8UMPrkyz6mwWMVvulTvAIDXHk JYgjHOA4lmySHMHqFPq/4Z568mTMs8/vmfwr56lCrdGOEDD1rOofKY4kbrCbI0Z6zs UuGnx/T1jhyKA4pOfYjxalRtZ6vlPpuhpQ3G72fNp7xnLCSDucvNL0f+DaqY90VKan zucafzNUd8J7pXhrILO0RSL39xB5t5HB3ngVK3fw/rNTqHNjED+b7IjfQi6Zi/czF/ 4MfeZPJNetPdA== From: Jakub Kicinski To: davem@davemloft.net Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com, shuah@kernel.org, petrm@nvidia.com, linux-kselftest@vger.kernel.org, willemdebruijn.kernel@gmail.com, Jakub Kicinski Subject: [PATCH net-next v4 3/7] selftests: drv-net: construct environment for running tests which require an endpoint Date: Thu, 18 Apr 2024 16:38:40 -0700 Message-ID: <20240418233844.2762396-4-kuba@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240418233844.2762396-1-kuba@kernel.org> References: <20240418233844.2762396-1-kuba@kernel.org> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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. Signed-off-by: Jakub Kicinski --- .../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 diff --git a/tools/testing/selftests/drivers/net/README.rst b/tools/testing/selftests/drivers/net/README.rst index 5ef7c417d431..0cbab33dad1f 100644 --- a/tools/testing/selftests/drivers/net/README.rst +++ b/tools/testing/selftests/drivers/net/README.rst @@ -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 diff --git a/tools/testing/selftests/drivers/net/lib/py/env.py b/tools/testing/selftests/drivers/net/lib/py/env.py index a081e168f3db..579c5b34e6fd 100644 --- a/tools/testing/selftests/drivers/net/lib/py/env.py +++ b/tools/testing/selftests/drivers/net/lib/py/env.py @@ -4,7 +4,8 @@ import os import shlex from pathlib import Path from lib.py import ip -from lib.py import NetdevSimDev +from lib.py import NetNS, NetdevSimDev +from .remote import Remote def _load_env_file(src_path): @@ -59,3 +60,98 @@ from lib.py import NetdevSimDev self._ns = None +class NetDrvEpEnv: + """ + Class for an environment with a local device and "remote endpoint" + which can be used to send traffic in. + + For local testing it creates two network namespaces and a pair + of netdevsim devices. + """ + + # Network prefixes used for local tests + nsim_v4_pfx = "192.0.2." + nsim_v6_pfx = "2001:db8::" + + def __init__(self, src_path): + + self.env = _load_env_file(src_path) + + # Things we try to destroy + self.remote = None + # These are for local testing state + self._netns = None + self._ns = None + self._ns_peer = None + + if "NETIF" in self.env: + self.dev = ip("link show dev " + self.env['NETIF'], json=True)[0] + + self.v4 = self.env.get("LOCAL_V4") + self.v6 = self.env.get("LOCAL_V6") + self.remote_v4 = self.env.get("REMOTE_V4") + self.remote_v6 = self.env.get("REMOTE_V6") + kind = self.env["REMOTE_TYPE"] + args = self.env["REMOTE_ARGS"] + else: + self.create_local() + + self.dev = self._ns.nsims[0].dev + + self.v4 = self.nsim_v4_pfx + "1" + self.v6 = self.nsim_v6_pfx + "1" + self.remote_v4 = self.nsim_v4_pfx + "2" + self.remote_v6 = self.nsim_v6_pfx + "2" + kind = "netns" + args = self._netns.name + + self.remote = Remote(kind, args, src_path) + + self.addr = self.v6 if self.v6 else self.v4 + self.remote_addr = self.remote_v6 if self.remote_v6 else self.remote_v4 + + self.ifname = self.dev['ifname'] + self.ifindex = self.dev['ifindex'] + + def create_local(self): + self._netns = NetNS() + self._ns = NetdevSimDev() + self._ns_peer = NetdevSimDev(ns=self._netns) + + with open("/proc/self/ns/net") as nsfd0, \ + open("/var/run/netns/" + self._netns.name) as nsfd1: + ifi0 = self._ns.nsims[0].ifindex + ifi1 = self._ns_peer.nsims[0].ifindex + NetdevSimDev.ctrl_write('link_device', + f'{nsfd0.fileno()}:{ifi0} {nsfd1.fileno()}:{ifi1}') + + ip(f" addr add dev {self._ns.nsims[0].ifname} {self.nsim_v4_pfx}1/24") + ip(f"-6 addr add dev {self._ns.nsims[0].ifname} {self.nsim_v6_pfx}1/64 nodad") + ip(f" link set dev {self._ns.nsims[0].ifname} up") + + ip(f" addr add dev {self._ns_peer.nsims[0].ifname} {self.nsim_v4_pfx}2/24", ns=self._netns) + ip(f"-6 addr add dev {self._ns_peer.nsims[0].ifname} {self.nsim_v6_pfx}2/64 nodad", ns=self._netns) + ip(f" link set dev {self._ns_peer.nsims[0].ifname} up", ns=self._netns) + + def __enter__(self): + return self + + def __exit__(self, ex_type, ex_value, ex_tb): + """ + __exit__ gets called at the end of a "with" block. + """ + self.__del__() + + def __del__(self): + if self._ns: + self._ns.remove() + self._ns = None + if self._ns_peer: + self._ns_peer.remove() + self._ns_peer = None + if self._netns: + del self._netns + self._netns = None + if self.remote: + del self.remote + self.remote = None diff --git a/tools/testing/selftests/net/lib/py/__init__.py b/tools/testing/selftests/net/lib/py/__init__.py index ded7102df18a..b6d498d125fe 100644 --- a/tools/testing/selftests/net/lib/py/__init__.py +++ b/tools/testing/selftests/net/lib/py/__init__.py @@ -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 diff --git a/tools/testing/selftests/net/lib/py/netns.py b/tools/testing/selftests/net/lib/py/netns.py new file mode 100644 index 000000000000..ecff85f9074f --- /dev/null +++ b/tools/testing/selftests/net/lib/py/netns.py @@ -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})" From patchwork Thu Apr 18 23:38:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 790394 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C99137C08D; Thu, 18 Apr 2024 23:38:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713483532; cv=none; b=MzGKxOYJWIz/dtvJ3b5U2TZLNAZ1ePDq1BAhTPIJDWl3OiMIIh5Ee+9WlAi0D4hsrizWure9QGONL0bMeo1Hg6CzqPK7jr17MbuZwC6T/wMLTiLHSCNtQTO6L7TYo0ns0ubLV6dwdCmxTNWdAl+h2C9pFs0mXe4u7bogs4QfOBs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713483532; c=relaxed/simple; bh=Xcg/4YWNTZU4GPQmaDdLsBXVgUTYXSPrMsnia87nxsg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QMrkkmU7LrYTG6hLwCrR0BP1YbylMeX3QNAnxVLsPMnhJHRbF8kgcx1aEVERHVNZN80f0ifxNBqEs4YJ3ogABA2SMITkLxdzfCjnsmTS6oQa80SGoC8oFOztLPZ+W7XYbuYLoZi4vy8cKYZ/jMtBjOSQbBVthCevuPs7/jyPY5w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=h5RRVIXw; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="h5RRVIXw" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 011F9C32781; Thu, 18 Apr 2024 23:38:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713483532; bh=Xcg/4YWNTZU4GPQmaDdLsBXVgUTYXSPrMsnia87nxsg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=h5RRVIXwDTVvjr2y5c8cDdIXV6TOjgZcRWbTKs6QNhqv1FSL4oLsDYZEhQBwdWcQO eiXuMovgK/7dlZC0SC58rPPB64vplgjbEXbNKSYrEIQJwGaY+QXYvO/udxsE8l5+a9 esH6J0nhcIsrGlaa2EyE8eeohhE6grJCW5e1L1M9YMp1gaICagYh5iqLE34VMD3AYr P+GTt/OEWGM6jSnK3s5Phq9UUO06eUYfsCC/O5JMwUoR2bwo5tKg8zDu7jGDq4JDPl sziYaJvdmpXaunwe/6YzkpqxMRAj82fM5KaGhxo/an2MxlPPc/7ByN5X9PlfYPVatv IR1hjmqiDLAWw== From: Jakub Kicinski To: davem@davemloft.net Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com, shuah@kernel.org, petrm@nvidia.com, linux-kselftest@vger.kernel.org, willemdebruijn.kernel@gmail.com, Jakub Kicinski Subject: [PATCH net-next v4 4/7] selftests: drv-net: add a trivial ping test Date: Thu, 18 Apr 2024 16:38:41 -0700 Message-ID: <20240418233844.2762396-5-kuba@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240418233844.2762396-1-kuba@kernel.org> References: <20240418233844.2762396-1-kuba@kernel.org> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add a very simple test for testing with a remote system. Both IPv4 and IPv6 connectivity is optional, later change will add checks to skip tests based on available addresses. 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.test_v4 # ok 2 ping.test_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 REMOTE_ARGS=root@192.168.122.123 \ 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.test_v4 ok 2 ping.test_v6 # SKIP Test requires IPv6 connectivity # 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.test_v4 # ok 2 ping.test_v6 # SKIP Test requires IPv6 connectivity # # Totals: pass:1 fail:0 xfail:1 xpass:0 skip:0 error:0 Signed-off-by: Jakub Kicinski --- v4: - move adding the "require_XYZ()" checks to last patch --- tools/testing/selftests/drivers/net/Makefile | 5 +++- tools/testing/selftests/drivers/net/ping.py | 27 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/drivers/net/ping.py diff --git a/tools/testing/selftests/drivers/net/Makefile b/tools/testing/selftests/drivers/net/Makefile index 379cdb1960a7..754ec643768a 100644 --- a/tools/testing/selftests/drivers/net/Makefile +++ b/tools/testing/selftests/drivers/net/Makefile @@ -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 diff --git a/tools/testing/selftests/drivers/net/ping.py b/tools/testing/selftests/drivers/net/ping.py new file mode 100755 index 000000000000..e75908d7c558 --- /dev/null +++ b/tools/testing/selftests/drivers/net/ping.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 + +from lib.py import ksft_run, ksft_exit +from lib.py import NetDrvEpEnv +from lib.py import cmd + + +def test_v4(cfg) -> None: + cmd(f"ping -c 1 -W0.5 {cfg.remote_v4}") + cmd(f"ping -c 1 -W0.5 {cfg.v4}", host=cfg.remote) + + +def test_v6(cfg) -> None: + 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([test_v4, test_v6], + args=(cfg, )) + ksft_exit() + + +if __name__ == "__main__": + main() From patchwork Thu Apr 18 23:38:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 790014 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 718087CF0F; Thu, 18 Apr 2024 23:38:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713483533; cv=none; b=qpioEjTeyoCL3AT4hhSV0VSMk95w/5sYIKic8XyeNUyPC2/AWxDRTl9qT1usaX355+WA2O0Z3g3jBN2BxcY6tI52v021BgSt7hfS7uDTg43Y/d3GZnz2BB+YatXeior+gmKR2eFTZPeFkTyyH62u1LZoDPUghUxAD5WO/EedLPM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713483533; c=relaxed/simple; bh=3EYmbp2B4r9MUqRbdnAXASyZmfi5Yfa/llFet9lNHBA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Q+wjZwnhqh5xMNG1CXqNiWj7NgqD4jwVXQLMsbEq+uTO7aFenvAg6I0ds06uVUFGnWdOE50bCvlOwHkKSU3PJ+NfwbJK9VW3BdOvNnlJ30dXFI9ZUjpK9kzZarT76TcgnO0eSYIEuMh2aNwqc34ok9aiHzLJ4TwPElKHIPZTAC0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=uLkVfJDC; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="uLkVfJDC" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8FA96C32783; Thu, 18 Apr 2024 23:38:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713483533; bh=3EYmbp2B4r9MUqRbdnAXASyZmfi5Yfa/llFet9lNHBA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uLkVfJDC5Ip17lA/UwxLnd22mkj2jbp9e9HqizPlqkk57cinD0FkE9GqUPwmsKrEY Zl3GUxFaL9ElyVj6udH+FbmqIwVkDasYQVkXZp/MrspGKUOu7qH220H90nWVBNgspx UisS+C5ks/+OHG2YcKQjJBL8ZEpHpXgl7Cr1wq3SM0omTxxVCTLOMqRrxLdGQ1Stzt pUXs8GKRqtzkaex/moM+TDI8rwWE2aHg1G5LTSFW8stJzzJEm85ARjkuD+1IeI+D79 O2ffFYk/UN3U7H93NfAvpCJePssRFmCq/jqzNnQgPY+33KrPm9KnmQp9Eu9Z4bnMgr zCQ/4Rwv5Xsxw== From: Jakub Kicinski To: davem@davemloft.net Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com, shuah@kernel.org, petrm@nvidia.com, linux-kselftest@vger.kernel.org, willemdebruijn.kernel@gmail.com, Jakub Kicinski Subject: [PATCH net-next v4 5/7] selftests: net: support matching cases by name prefix Date: Thu, 18 Apr 2024 16:38:42 -0700 Message-ID: <20240418233844.2762396-6-kuba@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240418233844.2762396-1-kuba@kernel.org> References: <20240418233844.2762396-1-kuba@kernel.org> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 While writing tests with a lot more cases I got tired of having to jump back and forth to add the name of the test to the ksft_run() list. Most unittest frameworks do some name matching, e.g. assume that functions with names starting with test_ are test cases. Support similar flow in ksft_run(). Let the author list the desired prefixes. globals() need to be passed explicitly, IDK how to work around that. Signed-off-by: Jakub Kicinski --- v4: - spell the code out a little to make it clearer --- tools/testing/selftests/drivers/net/ping.py | 3 +-- tools/testing/selftests/net/lib/py/ksft.py | 13 ++++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/drivers/net/ping.py b/tools/testing/selftests/drivers/net/ping.py index e75908d7c558..9f65a0764aab 100755 --- a/tools/testing/selftests/drivers/net/ping.py +++ b/tools/testing/selftests/drivers/net/ping.py @@ -18,8 +18,7 @@ from lib.py import cmd def main() -> None: with NetDrvEpEnv(__file__) as cfg: - ksft_run([test_v4, test_v6], - args=(cfg, )) + ksft_run(globs=globals(), case_pfx={"test_"}, args=(cfg, )) ksft_exit() diff --git a/tools/testing/selftests/net/lib/py/ksft.py b/tools/testing/selftests/net/lib/py/ksft.py index 25f2572fa540..8018bf98a9d2 100644 --- a/tools/testing/selftests/net/lib/py/ksft.py +++ b/tools/testing/selftests/net/lib/py/ksft.py @@ -81,7 +81,18 @@ KSFT_RESULT_ALL = True print(res) -def ksft_run(cases, args=()): +def ksft_run(cases=None, globs=None, case_pfx=None, args=()): + cases = cases or [] + + if globs and case_pfx: + for key, value in globs.items(): + if not callable(value): + continue + for prefix in case_pfx: + if key.startswith(prefix): + cases.append(value) + break + totals = {"pass": 0, "fail": 0, "skip": 0, "xfail": 0} print("KTAP version 1") From patchwork Thu Apr 18 23:38:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 790393 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B1A657BB07; Thu, 18 Apr 2024 23:38:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713483533; cv=none; b=EJ4atU7o5NIk0kQN/GnXNawUcKQy9I5rYvuvjhLmUeo+7QYZ/f/FKfwCQp48tNyPKivXftjVI5vLoVcepn5swOI0yIqfP+OnszaT0PhKodLdqU3/iNnfA5SxzhdYkJ3GDAWIG8U5ybsp3LhE6L5GwW7cqPmfFV/H7YkELBEq3XQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713483533; c=relaxed/simple; bh=w3MXqPbZyYCxxCj3daodZExLko5+c/ZSVMOfwTMEBwk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kWc7fkOuxueiSgB7HquwHmNLR5xwqhORk/1rtF2+UD6m6npB1PYzSWJsKG/hLfcFPGVo6fCXerJBGVYFEyis4JdkJqMbo0V6Lcf++Hl2YutrhlQiD8XNEvmJV9+Lk2CpFevPTUwXINtvZYYgJSx3XyAabtGcyOXOjSjWEp3a9SI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pgnBjMH+; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="pgnBjMH+" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2C16DC4AF08; Thu, 18 Apr 2024 23:38:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713483533; bh=w3MXqPbZyYCxxCj3daodZExLko5+c/ZSVMOfwTMEBwk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pgnBjMH+Qa7BNAb+XgrVJBSib0YRKHLSOCXeJD34At6VxdkdPzu9C3nNPIW9vXm0b 1aS3+u/5ZOcD675cRyCmCnEddHn/doTnfoxqrK7STXCO6oyRpOKduWyu0uMbAR0XMn ipcDEkGcIXLBwNhqCsHkAqxNM2UJ/b59nxQe+Z37PsfBzxqWKJWU2Q3n7PxISsMH93 hjg+vz09uH9cVb5DfZSKh7lu+rzGpPuYLiMye1uFaUsGGvWHM+ZiUVMU+okwgP3YPa nzmEjGyc+rh8wRZ9Ic6IwTzXmEio5oXP83Fa9SVU6tSuPuPeaDSaDqCMyTINsmLmOo ZzHaRufsupd0g== From: Jakub Kicinski To: davem@davemloft.net Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com, shuah@kernel.org, petrm@nvidia.com, linux-kselftest@vger.kernel.org, willemdebruijn.kernel@gmail.com, Jakub Kicinski Subject: [PATCH net-next v4 6/7] selftests: drv-net: add a TCP ping test case (and useful helpers) Date: Thu, 18 Apr 2024 16:38:43 -0700 Message-ID: <20240418233844.2762396-7-kuba@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240418233844.2762396-1-kuba@kernel.org> References: <20240418233844.2762396-1-kuba@kernel.org> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 More complex tests often have to spawn a background process, like a server which will respond to requests or tcpdump. Add support for creating such processes using the with keyword: with bkg("my-daemon", ..): # my-daemon is alive in this block My initial thought was to add this support to cmd() directly but it runs the command in the constructor, so by the time we __enter__ it's too late to make sure we used "background=True". Second useful helper transplanted from net_helper.sh is wait_port_listen(). The test itself uses socat, which insists on v6 addresses being wrapped in [], it's not the only command which requires this format, so add the wrapped address to env. The hope is to save test code from checking if address is v6. Signed-off-by: Jakub Kicinski --- .../selftests/drivers/net/lib/py/env.py | 4 +++ tools/testing/selftests/drivers/net/ping.py | 24 ++++++++++++- tools/testing/selftests/net/lib/py/utils.py | 35 +++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/drivers/net/lib/py/env.py b/tools/testing/selftests/drivers/net/lib/py/env.py index 579c5b34e6fd..2f62270d59fa 100644 --- a/tools/testing/selftests/drivers/net/lib/py/env.py +++ b/tools/testing/selftests/drivers/net/lib/py/env.py @@ -110,6 +110,10 @@ from .remote import Remote self.addr = self.v6 if self.v6 else self.v4 self.remote_addr = self.remote_v6 if self.remote_v6 else self.remote_v4 + # Bracketed addresses, some commands need IPv6 to be inside [] + self.baddr = f"[{self.v6}]" if self.v6 else self.v4 + self.remote_baddr = f"[{self.remote_v6}]" if self.remote_v6 else self.remote_v4 + self.ifname = self.dev['ifname'] self.ifindex = self.dev['ifindex'] diff --git a/tools/testing/selftests/drivers/net/ping.py b/tools/testing/selftests/drivers/net/ping.py index 9f65a0764aab..15a0bdcb46e2 100755 --- a/tools/testing/selftests/drivers/net/ping.py +++ b/tools/testing/selftests/drivers/net/ping.py @@ -1,9 +1,12 @@ #!/usr/bin/env python3 # SPDX-License-Identifier: GPL-2.0 +import random + from lib.py import ksft_run, ksft_exit +from lib.py import ksft_eq from lib.py import NetDrvEpEnv -from lib.py import cmd +from lib.py import bkg, cmd, wait_port_listen def test_v4(cfg) -> None: @@ -16,6 +19,25 @@ from lib.py import cmd cmd(f"ping -c 1 -W0.5 {cfg.v6}", host=cfg.remote) +def test_tcp(cfg) -> None: + port = random.randrange(1024 + (1 << 15)) + + with bkg(f"socat -t 2 -u TCP-LISTEN:{port} STDOUT", exit_wait=True) as nc: + wait_port_listen(port) + + cmd(f"echo ping | socat -t 2 -u STDIN TCP:{cfg.baddr}:{port}", + shell=True, host=cfg.remote) + ksft_eq(nc.stdout.strip(), "ping") + + port = random.randrange(1024 + (1 << 15)) + with bkg(f"socat -t 2 -u TCP-LISTEN:{port} STDOUT", host=cfg.remote, + exit_wait=True) as nc: + wait_port_listen(port, host=cfg.remote) + + cmd(f"echo ping | socat -t 2 -u STDIN TCP:{cfg.remote_baddr}:{port}", shell=True) + ksft_eq(nc.stdout.strip(), "ping") + + def main() -> None: with NetDrvEpEnv(__file__) as cfg: ksft_run(globs=globals(), case_pfx={"test_"}, args=(cfg, )) diff --git a/tools/testing/selftests/net/lib/py/utils.py b/tools/testing/selftests/net/lib/py/utils.py index 7347d0c0ff05..92d9078c164a 100644 --- a/tools/testing/selftests/net/lib/py/utils.py +++ b/tools/testing/selftests/net/lib/py/utils.py @@ -1,7 +1,10 @@ # SPDX-License-Identifier: GPL-2.0 import json as _json +import re import subprocess +import time + class cmd: def __init__(self, comm, shell=True, fail=True, ns=None, background=False, host=None): @@ -38,6 +41,20 @@ import subprocess (self.proc.args, stdout, stderr)) +class bkg(cmd): + def __init__(self, comm, shell=True, fail=True, ns=None, host=None, + exit_wait=False): + super().__init__(comm, background=True, + shell=shell, fail=fail, ns=ns, host=host) + self.terminate = not exit_wait + + def __enter__(self): + return self + + def __exit__(self, ex_type, ex_value, ex_tb): + return self.process(terminate=self.terminate) + + def ip(args, json=None, ns=None, host=None): cmd_str = "ip " if json: @@ -47,3 +64,21 @@ import subprocess if json: return _json.loads(cmd_obj.stdout) return cmd_obj + + +def wait_port_listen(port, proto="tcp", ns=None, host=None, sleep=0.005, deadline=5): + end = time.monotonic() + deadline + + pattern = f":{port:04X} .* " + if proto == "tcp": # for tcp protocol additionally check the socket state + pattern += "0A" + pattern = re.compile(pattern) + + while True: + data = cmd(f'cat /proc/net/{proto}*', ns=ns, host=host, shell=True).stdout + for row in data.split("\n"): + if pattern.search(row): + return + if time.monotonic() > end: + raise Exception("Waiting for port listen timed out") + time.sleep(sleep) From patchwork Thu Apr 18 23:38:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 790013 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4A1707C08D; Thu, 18 Apr 2024 23:38:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713483534; cv=none; b=HkH3KkI6L84MRWgvo116d1OgISKiCd/wdarpvxYNLZZRaAQdUUzf3l4mgLKzApxc3So37cSd0LorOY36g+Tmlx4NdOA09cLsctmXh4IZX/XzZPbcXTg43Lw57ZhPdivoBg79bFtr4/0U1i1CAFuTo5upDuKocqI1Nhppkpt0sdI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713483534; c=relaxed/simple; bh=jbYvr0fYhhl4ZoloS4Czl6L/Y3rRVKWllDR1T9XXXrE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gFaLyn39/nrcw/OL3F4DqRgM956UTbqkPZ0FGn5Hx+VKVpKZ1ohnJc4S30eLGIQ+ZQG2biORULgCZ9DiQzE5VOMKKH3r/Ieixwf+L99kStEkVMhfkE34WlVthNYVPCEQos05DAa783ANPfck+zIzcKMO4hvsOxGr4TMJ+mtGXxo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gVp/OBpU; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="gVp/OBpU" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BA340C32781; Thu, 18 Apr 2024 23:38:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713483534; bh=jbYvr0fYhhl4ZoloS4Czl6L/Y3rRVKWllDR1T9XXXrE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gVp/OBpUD5ZM36z7sz9GrRl44T/11Xhc4F3cSqKS+iCAKWrT9LhGFOXwbWnyCV72X ULWmNGivWI/4kiZCqjTdWHMy0/5A1zC4zK8UaCiZGgIW7KPgPDgJWcK9090BFoLbbl kDa4Xfs5Xo5NwTO6xyqxolPkvN69AuplAtAwjv8dytRwLCi1uz1MJJo1L2pID3tYak N4YMxt9e4ozL9cRkuW52FGMQzpi13AG4VWWiWeRuMTG5L2+7rzzdKGkorDsAhq2pn3 JMrDBFjd0G5cNx9fmNIi20IOfZfLbucFptIBoU9SWWFOpjiFhYKOwbF+sQeKN9uFSH uTPBJ/1fXhjjw== From: Jakub Kicinski To: davem@davemloft.net Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com, shuah@kernel.org, petrm@nvidia.com, linux-kselftest@vger.kernel.org, willemdebruijn.kernel@gmail.com, Jakub Kicinski Subject: [PATCH net-next v4 7/7] selftests: drv-net: add require_XYZ() helpers for validating env Date: Thu, 18 Apr 2024 16:38:44 -0700 Message-ID: <20240418233844.2762396-8-kuba@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240418233844.2762396-1-kuba@kernel.org> References: <20240418233844.2762396-1-kuba@kernel.org> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Wrap typical checks like whether given command used by the test is available in helpers. Signed-off-by: Jakub Kicinski --- .../selftests/drivers/net/lib/py/env.py | 29 ++++++++++++++++++- tools/testing/selftests/drivers/net/ping.py | 6 ++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/drivers/net/lib/py/env.py b/tools/testing/selftests/drivers/net/lib/py/env.py index 2f62270d59fa..619dd214c095 100644 --- a/tools/testing/selftests/drivers/net/lib/py/env.py +++ b/tools/testing/selftests/drivers/net/lib/py/env.py @@ -3,7 +3,8 @@ import os import shlex from pathlib import Path -from lib.py import ip +from lib.py import KsftSkipEx +from lib.py import cmd, ip from lib.py import NetNS, NetdevSimDev from .remote import Remote @@ -117,6 +118,8 @@ from .remote import Remote self.ifname = self.dev['ifname'] self.ifindex = self.dev['ifindex'] + self._required_cmd = {} + def create_local(self): self._netns = NetNS() self._ns = NetdevSimDev() @@ -159,3 +162,27 @@ from .remote import Remote if self.remote: del self.remote self.remote = None + + def require_v4(self): + if not self.v4 or not self.remote_v4: + raise KsftSkipEx("Test requires IPv4 connectivity") + + def require_v6(self): + if not self.v6 or not self.remote_v6: + raise KsftSkipEx("Test requires IPv6 connectivity") + + def _require_cmd(self, comm, key, host=None): + cached = self._required_cmd.get(comm, {}) + if cached.get(key) is None: + cached[key] = cmd("command -v -- " + comm, fail=False, + shell=True, host=host).ret == 0 + self._required_cmd[comm] = cached + return cached[key] + + def require_cmd(self, comm, local=True, remote=False): + if local: + if not self._require_cmd(comm, "local"): + raise KsftSkipEx("Test requires command: " + comm) + if remote: + if not self._require_cmd(comm, "remote"): + raise KsftSkipEx("Test requires (remote) command: " + comm) diff --git a/tools/testing/selftests/drivers/net/ping.py b/tools/testing/selftests/drivers/net/ping.py index 15a0bdcb46e2..a933a154b341 100755 --- a/tools/testing/selftests/drivers/net/ping.py +++ b/tools/testing/selftests/drivers/net/ping.py @@ -10,16 +10,22 @@ from lib.py import bkg, cmd, wait_port_listen def test_v4(cfg) -> None: + cfg.require_v4() + cmd(f"ping -c 1 -W0.5 {cfg.remote_v4}") cmd(f"ping -c 1 -W0.5 {cfg.v4}", host=cfg.remote) def test_v6(cfg) -> None: + cfg.require_v6() + cmd(f"ping -c 1 -W0.5 {cfg.remote_v6}") cmd(f"ping -c 1 -W0.5 {cfg.v6}", host=cfg.remote) def test_tcp(cfg) -> None: + cfg.require_cmd("socat", remote=True) + port = random.randrange(1024 + (1 << 15)) with bkg(f"socat -t 2 -u TCP-LISTEN:{port} STDOUT", exit_wait=True) as nc: