From patchwork Mon Dec 9 14:01:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiao Liang X-Patchwork-Id: 18718 X-Patchwork-Delegate: sw@simonwunderlich.de Return-Path: X-Original-To: patchwork@open-mesh.org Delivered-To: patchwork@open-mesh.org Received: from diktynna.open-mesh.org (localhost [IPv6:::1]) by diktynna.open-mesh.org (Postfix) with ESMTP id B46E783CAB for ; Mon, 9 Dec 2024 17:51:13 +0100 (CET) ARC-Seal: i=2; cv=pass; a=rsa-sha256; d=open-mesh.org; s=20121; t=1733763073; b=1jj/5UaxOc41a2VE1qes8JXWLWL9vNLuAfa7APte0FZGFO0sCRsDHOS3UaHlv6You5Vck CgPGbOOKxnhIKpoUQwCHTGyKxX9j147q7fWseUe7HVCEsHyxhdhjFo+/y0rLsfr8O+YK1jT JZoQJ+ubwAU1UL1yJkMklwxabwccSeM= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1733763073; h=from : sender : reply-to : subject : date : message-id : to : cc : mime-version : content-type : content-transfer-encoding : content-id : content-description : resent-date : resent-from : resent-sender : resent-to : resent-cc : resent-message-id : in-reply-to : references : list-id : list-help : list-unsubscribe : list-subscribe : list-post : list-owner : list-archive; bh=f/aVoxIsj2NaHZxO0wthneclpZNQFTaEi7bz2gcdad0=; b=yXLdgIt0WhJwLTspKYhvobuBoCwoKTQV0aELch+9EO02kT5YIVzE0NBSB+HTXe65eNXqI V8wg0k1kzSzHMp52ShzxF1NgkHcsMBK5uGsfVKnNqp29D26rZGWmG/x6hqjoKR+KXDEZ/VU FaewRFdvJwlfHPF2oIKK4xUKT42k3jM= ARC-Authentication-Results: i=2; open-mesh.org; dkim=pass header.d=gmail.com; arc=pass; dmarc=pass (Used From Domain Record) header.from=gmail.com policy.dmarc=quarantine Authentication-Results: open-mesh.org; dkim=pass header.d=gmail.com; arc=pass; dmarc=pass (Used From Domain Record) header.from=gmail.com policy.dmarc=quarantine Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by diktynna.open-mesh.org (Postfix) with ESMTPS id 9ADB583BDD for ; Mon, 9 Dec 2024 15:06:07 +0100 (CET) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1733753168; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=f/aVoxIsj2NaHZxO0wthneclpZNQFTaEi7bz2gcdad0=; b=Hn2MT2gghIOaLBEHpNtEK2S1Dt0c21rroJtYJ0kB/KaKmqCyFqYM45uRy1hEh51s+rDvNG W5YMSsO98fHgzAOBfiHWszk2DVBWKi7q+Z/j/g6m5/FM2RBQ3vJ+EeHw9u4w5aJ5y6PQvT kyBwHbxH1lFdaN/caAP48zb3WeAgAXI= ARC-Seal: i=1; s=20121; d=open-mesh.org; t=1733753168; a=rsa-sha256; cv=none; b=AAJP+Bsvx19aCAwzEcG1AM9SMxW3+dn9W6aaPGn4cSUC2H4Ss0GbpVoDEE6mz7lfI/K8FP qHh9FIkRFFlvi9DgaxyHHcj/D/7KLoAQA+w5McJSnkGpoqH2KKnTFVs+VKB1+0iEsA2fo7 1nAN623saVIplz/8M3Rq5oDsVQ/G/Io= ARC-Authentication-Results: i=1; diktynna.open-mesh.org; dkim=pass header.d=gmail.com header.s=20230601 header.b=lvjHixRr; spf=pass (diktynna.open-mesh.org: domain of shaw.leon@gmail.com designates 2607:f8b0:4864:20::632 as permitted sender) smtp.mailfrom=shaw.leon@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-21631789fcdso10136175ad.1 for ; Mon, 09 Dec 2024 06:06:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733753166; x=1734357966; darn=lists.open-mesh.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=f/aVoxIsj2NaHZxO0wthneclpZNQFTaEi7bz2gcdad0=; b=lvjHixRr83t4n0I1yB9IsWliO0R/teIEkd5RMNQA6SZR5EF9vwOOZzOd0ChVstLxAk 7lVHcPthSiBM7lzn78EXWYRQilJJKMLhd5heI7IVMiawoh/BIDRgPp+SgQsAC8aNJ4b8 hoktwckKN/zX+XZMn1nQYDWhjUWGKqnp8e8zXBjtCY2Eh/zTQK55rOq1khWxuHj4jmSs 7BPZ5zxOyRue5BgeKEgdy1Ee6qQkX5J/ukjkAz8CwHSksahet8VLIj/DSVVYSsFWyBTI xu8XyefRHAk5lDHqVaM+yZ1w35trbcjfkM9vepHIw0uodjCWwTmmAPbCvSvhmHvqF2X/ Q4+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733753166; x=1734357966; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=f/aVoxIsj2NaHZxO0wthneclpZNQFTaEi7bz2gcdad0=; b=UIRPdYZ4vEM4Mjjojo8JbdSokG4pH+EL1RPowteShtcoOG4BuZ+M3hKalQnfrjeKw5 VWgtNIp59sjr/yCBwJ+jKdCy0RT3WlSiGGlHID3d20lf8sFcMRwluqIdNT1X5yWZUmB8 iYoNL3vOXBAKDrJVUyUrWiKObAA/DRelJf+JNhGrl3YbrNFSv974F2Oc438K7t2CppIg 4OLNglDVKDnEShhxjToqOce171yJztiFWPUBEN/9BsWYX5afva9JbCZH7hEZgeA41+lu /dFErh4KJaVwaY/A0InjQXQrxt9uv4gPqJxehEP0wkkY9aB4Y1vanpllwMPsW+zM6WM/ nBkA== X-Forwarded-Encrypted: i=1; AJvYcCUAD2ZvadJztS1RK/v6r1Zs8Fw0wnzWTmJTlVmcNOOhPdn7kB/2+2ffwG7HdFCk/+Hs3x8UxUjvmIw1Dw==@lists.open-mesh.org X-Gm-Message-State: AOJu0YymyqN6eOBQxI+pmArDqRUBvqI7etgEJq56h6LXVeAQES48FL8x tAAbX/T82t6Mbi/Xk8yM/hVPcm4lKC6EEPLcLv17MQ+JyujIEajL X-Gm-Gg: ASbGncsIdONHT4DmkwRNAsyTT5HZ62y+rycORd5SRBxLe+Gdgj5GKg86/SnKD7HnQHi UP/w+0RPopxSFBcc4RSK4sICZ9DYoZ0cS4idmcOtSfnijhsVtB0pXWEWKZjV+fPmbUh8edMlY3K dcEzXnW3CqnItpKWWS8YYyKOusxummbhoxu1OPDneZWW1aKgF7XOetsu+iCa0tTeQaiLNSxYZV4 s30228J9cLI/dOMdQ/SwsEHESx21XtD4zJe+2dOz8wtLwQ= X-Google-Smtp-Source: AGHT+IEgO+eAsdpey+DR5/LF2b2LrxgYAnjIwOd9wPEert31MRK25WTtIv1pjQ5j1prur61gIiJ4iQ== X-Received: by 2002:a17:902:c949:b0:215:58be:334e with SMTP id d9443c01a7336-21610b3539amr198169675ad.10.1733753165835; Mon, 09 Dec 2024 06:06:05 -0800 (PST) Received: from nova-ws.. ([103.167.140.11]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-216221db645sm49605645ad.46.2024.12.09.06.05.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Dec 2024 06:06:05 -0800 (PST) From: Xiao Liang To: netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, Kuniyuki Iwashima , Jakub Kicinski , Donald Hunter Cc: "David S. Miller" , David Ahern , Eric Dumazet , Paolo Abeni , Ido Schimmel , Andrew Lunn , Simon Horman , Shuah Khan , Jiri Pirko , Hangbin Liu , linux-rdma@vger.kernel.org, linux-can@vger.kernel.org, osmocom-net-gprs@lists.osmocom.org, bpf@vger.kernel.org, linux-ppp@vger.kernel.org, wireguard@lists.zx2c4.com, linux-wireless@vger.kernel.org, b.a.t.m.a.n@lists.open-mesh.org, bridge@lists.linux.dev, linux-wpan@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net-next v5 4/5] selftests: net: Add python context manager for netns entering Date: Mon, 9 Dec 2024 22:01:50 +0800 Message-ID: <20241209140151.231257-5-shaw.leon@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241209140151.231257-1-shaw.leon@gmail.com> References: <20241209140151.231257-1-shaw.leon@gmail.com> MIME-Version: 1.0 X-MailFrom: shaw.leon@gmail.com X-Mailman-Rule-Hits: nonmember-moderation X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-b.a.t.m.a.n.lists.open-mesh.org-0; header-match-b.a.t.m.a.n.lists.open-mesh.org-1; header-match-b.a.t.m.a.n.lists.open-mesh.org-2 Message-ID-Hash: S3MZCAE55R6O3545M7UCLUTYYXYKWGZZ X-Message-ID-Hash: S3MZCAE55R6O3545M7UCLUTYYXYKWGZZ X-Mailman-Approved-At: Mon, 09 Dec 2024 17:47:40 +0100 X-Mailman-Version: 3.3.8 Precedence: list List-Id: The list for a Better Approach To Mobile Ad-hoc Networking Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- 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 54d8f5eba810..e2d6c7b63019 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 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()