OpenVZ / Virtuozzo kernel development (devel@openvz.org)
 help / color / mirror / Atom feed
From: Konstantin Khorenko <khorenko@virtuozzo.com>
To: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
Cc: devel@openvz.org
Subject: Re: [Devel] [PATCH vz10 1/4] ms/selftests: net: Add python context manager for netns entering
Date: Thu, 24 Sep 2026 15:27:38 +0200	[thread overview]
Message-ID: <75de8d9d-ced0-4cb3-97dc-ba38ecf9d9a9@virtuozzo.com> (raw)
In-Reply-To: <20260831225137.1642608-1-eva.kurchatova@virtuozzo.com>

Committed to branch-rh10-6.12.0-211.52.1.22.x.vz10-ovz, will be released in rh10-6.12.0-211.52.1.22.3.vz10

ae029f6f2312



ms/selftests: net: Add python context manager for netns entering
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>
Link: http://lore.virtuozzo.com/20260831225137.1642608-1-eva.kurchatova@virtuozzo.com







--
Best regards,

Konstantin Khorenko,
Virtuozzo Linux Kernel Team

On 9/1/26 00:50, Eva Kurchatova wrote:
> 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()

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

      parent reply	other threads:[~2026-09-24 13:29 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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
2026-09-24 13:27   ` Konstantin Khorenko
2026-08-31 22:50 ` [Devel] [PATCH vz10 3/4] selftests: net: py: report a refused environment as a skip Eva Kurchatova
2026-09-24 13:28   ` Konstantin Khorenko
2026-08-31 22:50 ` [Devel] [PATCH vz10 4/4] selftests: drv-net-hw: skip the tests that need a real interface Eva Kurchatova
2026-09-24 13:23   ` Konstantin Khorenko
2026-09-24 13:29   ` Konstantin Khorenko
2026-09-24 13:27 ` Konstantin Khorenko [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=75de8d9d-ced0-4cb3-97dc-ba38ecf9d9a9@virtuozzo.com \
    --to=khorenko@virtuozzo.com \
    --cc=devel@openvz.org \
    --cc=eva.kurchatova@virtuozzo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox