OpenVZ / Virtuozzo kernel development (devel@openvz.org)
 help / color / mirror / Atom feed
* [Devel] [PATCH vz10 1/4] ms/selftests: net: Add python context manager for netns entering
@ 2026-08-31 22:50 Eva Kurchatova
  2026-08-31 22:50 ` [Devel] [PATCH vz10 2/4] selftests: net: py: add the missing ksft_is() and ksft_not_in() helpers Eva Kurchatova
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Eva Kurchatova @ 2026-08-31 22:50 UTC (permalink / raw)
  To: khorenko; +Cc: devel

From: Xiao Liang <shaw.leon@gmail.com>

Change netns of current thread and switch back on context exit.
For example:

    with NetNSEnter("ns1"):
        ip("link add dummy0 type dummy")

The command be executed in netns "ns1".

Signed-off-by: Xiao Liang <shaw.leon@gmail.com>
Link: https://patch.msgid.link/20250219125039.18024-13-shaw.leon@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
(cherry picked from commit 030329416232ff9f2e3e4bb065b29e6c9a7d5050)

https://virtuozzo.atlassian.net/browse/VSTOR-139651
Feature: fix selftests
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
 tools/testing/selftests/net/lib/py/__init__.py |  2 +-
 tools/testing/selftests/net/lib/py/netns.py    | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/net/lib/py/__init__.py b/tools/testing/selftests/net/lib/py/__init__.py
index bffe01d5ee2d..02be28dcc089 100644
--- a/tools/testing/selftests/net/lib/py/__init__.py
+++ b/tools/testing/selftests/net/lib/py/__init__.py
@@ -2,7 +2,7 @@
 
 from .consts import KSRC
 from .ksft import *
-from .netns import NetNS
+from .netns import NetNS, NetNSEnter
 from .nsim import *
 from .utils import *
 from .ynl import NlError, YnlFamily, EthtoolFamily, NetdevFamily, RtnlFamily, RtnlAddrFamily
diff --git a/tools/testing/selftests/net/lib/py/netns.py b/tools/testing/selftests/net/lib/py/netns.py
index ecff85f9074f..8e9317044eef 100644
--- a/tools/testing/selftests/net/lib/py/netns.py
+++ b/tools/testing/selftests/net/lib/py/netns.py
@@ -1,9 +1,12 @@
 # SPDX-License-Identifier: GPL-2.0
 
 from .utils import ip
+import ctypes
 import random
 import string
 
+libc = ctypes.cdll.LoadLibrary('libc.so.6')
+
 
 class NetNS:
     def __init__(self, name=None):
@@ -29,3 +32,18 @@ class NetNS:
 
     def __repr__(self):
         return f"NetNS({self.name})"
+
+
+class NetNSEnter:
+    def __init__(self, ns_name):
+        self.ns_path = f"/run/netns/{ns_name}"
+
+    def __enter__(self):
+        self.saved = open("/proc/thread-self/ns/net")
+        with open(self.ns_path) as ns_file:
+            libc.setns(ns_file.fileno(), 0)
+        return self
+
+    def __exit__(self, exc_type, exc_value, traceback):
+        libc.setns(self.saved.fileno(), 0)
+        self.saved.close()
-- 
2.55.0

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Devel] [PATCH vz10 2/4] selftests: net: py: add the missing ksft_is() and ksft_not_in() helpers
  2026-08-31 22:50 [Devel] [PATCH vz10 1/4] ms/selftests: net: Add python context manager for netns entering Eva Kurchatova
@ 2026-08-31 22:50 ` Eva Kurchatova
  2026-08-31 22:50 ` [Devel] [PATCH vz10 3/4] selftests: net: py: report a refused environment as a skip Eva Kurchatova
  2026-08-31 22:50 ` [Devel] [PATCH vz10 4/4] selftests: drv-net-hw: skip the tests that need a real interface Eva Kurchatova
  2 siblings, 0 replies; 4+ messages in thread
From: Eva Kurchatova @ 2026-08-31 22:50 UTC (permalink / raw)
  To: khorenko; +Cc: devel

rss_api.py and rss_flow_label.py call ksft_is() and ksft_not_in(), and
net/lib/py/__init__.py exports both, but ksft.py never defined them, so
importing the library fails and every test of the group goes down with
it:

  ImportError: cannot import name 'ksft_is' from 'net.lib.py'

The tests and the export came in with the RHEL10 import, commit
9f055df11343 ("rh10: import RHEL10 kernel-6.12.0-211.16.1.el10"), the
helpers did not. Take them from the commits that added them upstream,
commit 31eae6d99587 ("selftests: drv-net: test drivers sleeping in
ndo_get_stats64") for ksft_is() and commit 4fde8398462f ("selftests:
drv-net: improve the use of ksft helpers in XSK queue test") for
ksft_not_in(). The tests those commits add are left out.

https://virtuozzo.atlassian.net/browse/VSTOR-139651
Feature: fix vz selftests
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
 tools/testing/selftests/net/lib/py/ksft.py | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/tools/testing/selftests/net/lib/py/ksft.py b/tools/testing/selftests/net/lib/py/ksft.py
index 7b50111340bb..61287c203b6e 100644
--- a/tools/testing/selftests/net/lib/py/ksft.py
+++ b/tools/testing/selftests/net/lib/py/ksft.py
@@ -76,6 +76,16 @@ def ksft_in(a, b, comment=""):
         _fail("Check failed", a, "not in", b, comment)
 
 
+def ksft_not_in(a, b, comment=""):
+    if a in b:
+        _fail("Check failed", a, "in", b, comment)
+
+
+def ksft_is(a, b, comment=""):
+    if a is not b:
+        _fail("Check failed", a, "is not", b, comment)
+
+
 def ksft_ge(a, b, comment=""):
     if a < b:
         _fail("Check failed", a, "<", b, comment)
-- 
2.55.0

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Devel] [PATCH vz10 3/4] selftests: net: py: report a refused environment as a skip
  2026-08-31 22:50 [Devel] [PATCH vz10 1/4] ms/selftests: net: Add python context manager for netns entering Eva Kurchatova
  2026-08-31 22:50 ` [Devel] [PATCH vz10 2/4] selftests: net: py: add the missing ksft_is() and ksft_not_in() helpers Eva Kurchatova
@ 2026-08-31 22:50 ` Eva Kurchatova
  2026-08-31 22:50 ` [Devel] [PATCH vz10 4/4] selftests: drv-net-hw: skip the tests that need a real interface Eva Kurchatova
  2 siblings, 0 replies; 4+ messages in thread
From: Eva Kurchatova @ 2026-08-31 22:50 UTC (permalink / raw)
  To: khorenko; +Cc: devel

A KsftSkipEx or KsftXfailEx raised outside ksft_run(), which is where an
environment check belongs, reaches the interpreter and is printed as a
traceback with exit status 1. A test that only wanted to say that this
machine cannot run it is then reported as a failure.

Install an excepthook that turns those two into the "1..0 # SKIP" or
XFAIL line the runner expects and exits with the skip status.

https://virtuozzo.atlassian.net/browse/VSTOR-139651
Feature: fix selftests
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
 tools/testing/selftests/net/lib/py/ksft.py | 27 ++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/tools/testing/selftests/net/lib/py/ksft.py b/tools/testing/selftests/net/lib/py/ksft.py
index 61287c203b6e..332f8c105512 100644
--- a/tools/testing/selftests/net/lib/py/ksft.py
+++ b/tools/testing/selftests/net/lib/py/ksft.py
@@ -10,6 +10,8 @@ import traceback
 from .consts import KSFT_MAIN_NAME
 from .utils import global_defer_queue
 
+KSFT_SKIP = 4
+
 KSFT_RESULT = None
 KSFT_RESULT_ALL = True
 KSFT_DISRUPTIVE = True
@@ -278,3 +280,28 @@ def ksft_run(cases=None, globs=None, case_pfx=None, args=()):
 def ksft_exit():
     global KSFT_RESULT_ALL
     sys.exit(0 if KSFT_RESULT_ALL else 1)
+
+
+def _ksft_no_tests_ran(comment):
+    print("TAP version 13")
+    print("1..0 # " + comment)
+    sys.exit(KSFT_SKIP)
+
+
+def _ksft_excepthook(exc_type, exc_value, tb):
+    """
+    A test whose environment refuses it, e.g. one that needs a real
+    device and is handed netdevsim, raises while the environment is
+    being built, before there is a ksft_run() to catch anything.  The
+    traceback that then leaves main() is reported as a failed test,
+    although the test never ran.  Say what happened and exit the way
+    the kselftest runner expects for a test that did not run.
+    """
+    if issubclass(exc_type, KsftSkipEx):
+        _ksft_no_tests_ran("SKIP " + str(exc_value))
+    if issubclass(exc_type, KsftXfailEx):
+        _ksft_no_tests_ran("XFAIL " + str(exc_value))
+    sys.__excepthook__(exc_type, exc_value, tb)
+
+
+sys.excepthook = _ksft_excepthook
-- 
2.55.0

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Devel] [PATCH vz10 4/4] selftests: drv-net-hw: skip the tests that need a real interface
  2026-08-31 22:50 [Devel] [PATCH vz10 1/4] ms/selftests: net: Add python context manager for netns entering Eva Kurchatova
  2026-08-31 22:50 ` [Devel] [PATCH vz10 2/4] selftests: net: py: add the missing ksft_is() and ksft_not_in() helpers Eva Kurchatova
  2026-08-31 22:50 ` [Devel] [PATCH vz10 3/4] selftests: net: py: report a refused environment as a skip Eva Kurchatova
@ 2026-08-31 22:50 ` Eva Kurchatova
  2 siblings, 0 replies; 4+ messages in thread
From: Eva Kurchatova @ 2026-08-31 22:50 UTC (permalink / raw)
  To: khorenko; +Cc: devel

ethtool, ethtool_extended_state, hw_stats_l3 and hw_stats_l3_gre need a
device that can set link speed and autonegotiation, report an extended
link state, or offload L3 statistics. Where none is configured, lib.sh
creates a veth pair instead, which supports none of that, so every case
fails on something the kernel correctly refuses.

Upstream answers this by keeping such tests in drivers/net/hw and not
running them on veth at all: commit 0c499a351777 ("selftests:
forwarding: Ditch skip_on_veth()") removed the equivalent check once the
directory was split out. This guard is therefore not for upstream, it
only stops a run that substitutes veth pairs from reporting failures.

Skip before sourcing lib.sh, because lib.sh makes the veth pair as it is
sourced. Exiting afterwards leaves the devices behind - vrf_cleanup()
removes them and a test that skips never gets there - and a later test
that adds a veth by one of those names then cannot. hw_stats_l3_gre
asks for six interfaces, so one run left veth0 to veth5 in the initial
namespace and the netfilter suite failed on it.

https://virtuozzo.atlassian.net/browse/VSTOR-139651
Feature: fix vz selftests
Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
 .../testing/selftests/drivers/net/hw/ethtool.sh  | 16 ++++++++++++++++
 .../drivers/net/hw/ethtool_extended_state.sh     | 16 ++++++++++++++++
 .../selftests/drivers/net/hw/hw_stats_l3.sh      | 16 ++++++++++++++++
 .../selftests/drivers/net/hw/hw_stats_l3_gre.sh  | 16 ++++++++++++++++
 4 files changed, 64 insertions(+)

diff --git a/tools/testing/selftests/drivers/net/hw/ethtool.sh b/tools/testing/selftests/drivers/net/hw/ethtool.sh
index fa6953de6b6d..b1744758627c 100755
--- a/tools/testing/selftests/drivers/net/hw/ethtool.sh
+++ b/tools/testing/selftests/drivers/net/hw/ethtool.sh
@@ -11,6 +11,22 @@ ALL_TESTS="
 "
 NUM_NETIFS=2
 lib_dir=$(dirname "$0")
+
+# The test needs a device capable of setting link speed and
+# autonegotiation. Where no real interfaces are configured, lib.sh makes
+# a veth pair instead, which cannot, and every case then fails on
+# something the kernel correctly refuses. Check this before sourcing
+# lib.sh: it creates that pair as it is sourced, so exiting afterwards
+# leaves the devices behind, and the next test that wants one of those
+# names cannot create it.
+
+: "${NETIF_CREATE:=yes}"
+: "${NETIF_TYPE:=veth}"
+if [[ "$NETIF_CREATE" = yes && "$NETIF_TYPE" = veth ]]; then
+	echo "SKIP: needs a real interface, set NETIFS in forwarding.config"
+	exit 4 # ksft_skip, lib.sh is not sourced yet
+fi
+
 source "$lib_dir"/../../../net/forwarding/lib.sh
 source ethtool_lib.sh
 
diff --git a/tools/testing/selftests/drivers/net/hw/ethtool_extended_state.sh b/tools/testing/selftests/drivers/net/hw/ethtool_extended_state.sh
index a7584448416e..c6c96117b1f1 100755
--- a/tools/testing/selftests/drivers/net/hw/ethtool_extended_state.sh
+++ b/tools/testing/selftests/drivers/net/hw/ethtool_extended_state.sh
@@ -9,6 +9,22 @@ ALL_TESTS="
 
 NUM_NETIFS=2
 lib_dir=$(dirname "$0")
+
+# The test needs a device capable of reporting an extended link state.
+# Where no real interfaces are configured, lib.sh makes a veth pair
+# instead, which cannot, and every case then fails on something the
+# kernel correctly refuses. Check this before sourcing lib.sh: it
+# creates that pair as it is sourced, so exiting afterwards leaves the
+# devices behind, and the next test that wants one of those names cannot
+# create it.
+
+: "${NETIF_CREATE:=yes}"
+: "${NETIF_TYPE:=veth}"
+if [[ "$NETIF_CREATE" = yes && "$NETIF_TYPE" = veth ]]; then
+	echo "SKIP: needs a real interface, set NETIFS in forwarding.config"
+	exit 4 # ksft_skip, lib.sh is not sourced yet
+fi
+
 source "$lib_dir"/../../../net/forwarding/lib.sh
 source ethtool_lib.sh
 
diff --git a/tools/testing/selftests/drivers/net/hw/hw_stats_l3.sh b/tools/testing/selftests/drivers/net/hw/hw_stats_l3.sh
index 67fafefc80be..1776a4954601 100755
--- a/tools/testing/selftests/drivers/net/hw/hw_stats_l3.sh
+++ b/tools/testing/selftests/drivers/net/hw/hw_stats_l3.sh
@@ -49,6 +49,22 @@ ALL_TESTS="
 "
 NUM_NETIFS=4
 lib_dir=$(dirname "$0")
+
+# The test needs a device capable of offloading L3 statistics. Where no
+# real interfaces are configured, lib.sh makes a veth pair instead,
+# which cannot, and every case then fails on something the kernel
+# correctly refuses. Check this before sourcing lib.sh: it creates that
+# pair as it is sourced, so exiting afterwards leaves the devices
+# behind, and the next test that wants one of those names cannot create
+# it.
+
+: "${NETIF_CREATE:=yes}"
+: "${NETIF_TYPE:=veth}"
+if [[ "$NETIF_CREATE" = yes && "$NETIF_TYPE" = veth ]]; then
+	echo "SKIP: needs a real interface, set NETIFS in forwarding.config"
+	exit 4 # ksft_skip, lib.sh is not sourced yet
+fi
+
 source "$lib_dir"/../../../net/forwarding/lib.sh
 source "$lib_dir"/../../../net/forwarding/tc_common.sh
 
diff --git a/tools/testing/selftests/drivers/net/hw/hw_stats_l3_gre.sh b/tools/testing/selftests/drivers/net/hw/hw_stats_l3_gre.sh
index a94d92e1abce..2cbc2682f7b7 100755
--- a/tools/testing/selftests/drivers/net/hw/hw_stats_l3_gre.sh
+++ b/tools/testing/selftests/drivers/net/hw/hw_stats_l3_gre.sh
@@ -13,6 +13,22 @@ ALL_TESTS="
 "
 NUM_NETIFS=6
 lib_dir=$(dirname "$0")
+
+# The test needs a device capable of offloading L3 statistics. Where no
+# real interfaces are configured, lib.sh makes a veth pair instead,
+# which cannot, and every case then fails on something the kernel
+# correctly refuses. Check this before sourcing lib.sh: it creates that
+# pair as it is sourced, so exiting afterwards leaves the devices
+# behind, and the next test that wants one of those names cannot create
+# it.
+
+: "${NETIF_CREATE:=yes}"
+: "${NETIF_TYPE:=veth}"
+if [[ "$NETIF_CREATE" = yes && "$NETIF_TYPE" = veth ]]; then
+	echo "SKIP: needs a real interface, set NETIFS in forwarding.config"
+	exit 4 # ksft_skip, lib.sh is not sourced yet
+fi
+
 source "$lib_dir"/../../../net/forwarding/lib.sh
 source "$lib_dir"/../../../net/forwarding/ipip_lib.sh
 source "$lib_dir"/../../../net/forwarding/tc_common.sh
-- 
2.55.0

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-08-31 22:53 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-08-31 22:50 [Devel] [PATCH vz10 1/4] ms/selftests: net: Add python context manager for netns entering Eva Kurchatova
2026-08-31 22:50 ` [Devel] [PATCH vz10 2/4] selftests: net: py: add the missing ksft_is() and ksft_not_in() helpers Eva Kurchatova
2026-08-31 22:50 ` [Devel] [PATCH vz10 3/4] selftests: net: py: report a refused environment as a skip Eva Kurchatova
2026-08-31 22:50 ` [Devel] [PATCH vz10 4/4] selftests: drv-net-hw: skip the tests that need a real interface Eva Kurchatova

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox