From patchwork Wed Nov 13 12:57:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiao Liang X-Patchwork-Id: 18678 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 6747483FA9 for ; Wed, 13 Nov 2024 14:02:09 +0100 (CET) ARC-Seal: i=2; cv=pass; a=rsa-sha256; d=open-mesh.org; s=20121; t=1731502929; b=LxfsunY+o+qr2cKsB8Z2J/7ILdrR+qdt7ZIbaNVfsgeEzopGBDUXfnIF2X5Qm8EDKQaHt qFKaIkZKSX+VCta0tiGh0NjdDt72kQzV39bfWI2TpKnqpdyeLBeNY1JFMsc8bQaFabO7Mny uMGPCwDeKXzGePkS1S3Kw6eMwCZJP8Q= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1731502929; 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=9sIZI/WV8nhbNfyJEUDND2yz+m6sbgKmtk/cQXv5cxc=; b=ycSvAi5DpZVmVOl+3S9MpNEeOoLPDEXsnsjutBBNM1rpvyaweXIT0pZ7C/+Y1u28YM5T9 swowaAzC01cO4Q6bScWXwJXkWU5eK/s0YO15QcVsGU+ainooLgxw9DrhTrDG3g6Rwq8w2+R pDCWt/N/4oSjex+GqGpMN+w+Hl7tiy8= 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-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by diktynna.open-mesh.org (Postfix) with ESMTPS id 5F01C82841 for ; Wed, 13 Nov 2024 13:57:46 +0100 (CET) ARC-Seal: i=1; s=20121; d=open-mesh.org; t=1731502666; a=rsa-sha256; cv=none; b=AD9xsVZjyDyDrKbLqvxI12ku5aorEm1RCviR9uCbaetPmrt5odU4Jjo1/MooM2AAz1kiSL 8qRZd3wlJpIDUrY/ShjL5yIToNUDTRATmR1+y24q6CBGDcvokjPum5InX7jTe6VeZxfuwv T6JWCzcBlUGmblxuRe4JmZKQHBd2A1A= ARC-Authentication-Results: i=1; diktynna.open-mesh.org; dkim=pass header.d=gmail.com header.s=20230601 header.b="DIW/gVc1"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (diktynna.open-mesh.org: domain of shaw.leon@gmail.com designates 2607:f8b0:4864:20::102f as permitted sender) smtp.mailfrom=shaw.leon@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1731502666; 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=9sIZI/WV8nhbNfyJEUDND2yz+m6sbgKmtk/cQXv5cxc=; b=mlg7GEXGokrrrliSXJRo679yoecewTnCQvohrN9YkUtosQn+fVTzc7jdNI5whJ0uVh3eDA dKwlb7mNTXKfp3YSQv3ZMd0QDGY0vPMoJ6JnVamJmhc47rfE/q1GWvvHWlTzb5O+B6+3vi 8AOQtAIgO3+dktJ5pAnkaHQrsbT3wq0= Received: by mail-pj1-x102f.google.com with SMTP id 98e67ed59e1d1-2e31af47681so5514815a91.2 for ; Wed, 13 Nov 2024 04:57:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731502665; x=1732107465; 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=9sIZI/WV8nhbNfyJEUDND2yz+m6sbgKmtk/cQXv5cxc=; b=DIW/gVc13h9zEPVPqjnzo6yMvCa1Btp7rvxmLtaeAgFpUySuZkWkuBV4+fbUKqgWMr v26fUgNQqWG3wj5RFW7VVtTjZnFXxVWLMJFwuioOCDw78WYHvYPOhhhf51IE7goy0Qfq B79qcQwM1l7JUNSgzleszap8Vk/EQZGTJbimmRAAwPb2PNqXGlxKXepZQKZ1AVAskkXp gs4sq1LrnK9XmXU721ajbI9rJ5Sm1tAJMtKRLs2xat9Vu7LI9Hg1ClZ4HtudRONVsp83 pjPkF965EoMKa/pu1XI+PG0Hzs0QuydXo+lcxKi3Ix8hawDNENh1kz1QJdY4IEHj/9W1 eKLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731502665; x=1732107465; 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=9sIZI/WV8nhbNfyJEUDND2yz+m6sbgKmtk/cQXv5cxc=; b=mDlYVMvWx3MobJTBQewJDVCTbPOuMnxYJkEfU7daPEDLsXwlQxftzhlN1+SRNy/z4c aqfxohjGU6NYHRmzpO4u7ULN6T1ZpGC4j20rcqN/AXUEzOUbPiGS+eHtQpEegdjIFKi6 PN3HfE5Cu/5FlXMcPtBcWjxCPdTvmTMgfNJNtOMSeKlyDbQOYcpJFriYd84KcuPbncJ4 kJ2BEqdzuk0t+dh8kKMxRW3ay0byF7jCN2guzC5UonOdeGbZYkibSflmol7RFXs4H53z sFBY5sBHX05Uwg7iXpAHn5vRQZfyIlKdh/TKnUhVfW9h4UICwbwyNskRjaSGDsX1lUv/ FAlQ== X-Forwarded-Encrypted: i=1; AJvYcCWOTtw1ZUvbxkE55f/etXxupRQZX2R6aDEg2EJGn/i+xt91ftKxINDAb0H4bolwwQPNrUXPkK2Nlqm/JA==@lists.open-mesh.org X-Gm-Message-State: AOJu0YxGsEQ+S+OT5Qov0A2kjKYVX10xK+c65ZQbCXeC2cIrpOtKbOWO hPiIPng6+e+sT5FLTNBAy01f99JD61qYVD3soE3aDdyDkqSAfoje X-Google-Smtp-Source: AGHT+IFBBLW/otzoU3VQWNkQ98rdfNz5WHNkXJFNuYVXzuqAWCzWf4F656wLCdVO3Ph+9deaw1I3JA== X-Received: by 2002:a17:90b:48c8:b0:2e2:c40c:6e8a with SMTP id 98e67ed59e1d1-2e9f2d5da7amr2684315a91.26.1731502664731; Wed, 13 Nov 2024 04:57:44 -0800 (PST) Received: from nova-ws.. ([103.167.140.11]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9f3f8ed0esm1398632a91.40.2024.11.13.04.57.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Nov 2024 04:57:44 -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 v3 1/6] Revert "tools/net/ynl: improve async notification handling" Date: Wed, 13 Nov 2024 20:57:10 +0800 Message-ID: <20241113125715.150201-2-shaw.leon@gmail.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241113125715.150201-1-shaw.leon@gmail.com> References: <20241113125715.150201-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: VHJ6GZOJD2INDEBBGTEJEG6FAHU32HKE X-Message-ID-Hash: VHJ6GZOJD2INDEBBGTEJEG6FAHU32HKE X-Mailman-Approved-At: Wed, 13 Nov 2024 14:00:42 +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: From: Donald Hunter This reverts commit 1bf70e6c3a5346966c25e0a1ff492945b25d3f80. This modification to check_ntf() is being reverted so that its behaviour remains equivalent to ynl_ntf_check() in the C YNL. Instead a new poll_ntf() will be added in a separate patch. Signed-off-by: Donald Hunter --- tools/net/ynl/cli.py | 10 +++----- tools/net/ynl/lib/ynl.py | 49 ++++++++++++++++------------------------ 2 files changed, 23 insertions(+), 36 deletions(-) diff --git a/tools/net/ynl/cli.py b/tools/net/ynl/cli.py index 9e95016b85b3..b8481f401376 100755 --- a/tools/net/ynl/cli.py +++ b/tools/net/ynl/cli.py @@ -5,7 +5,6 @@ import argparse import json import pprint import time -import signal from lib import YnlFamily, Netlink, NlError @@ -18,8 +17,6 @@ class YnlEncoder(json.JSONEncoder): return list(obj) return json.JSONEncoder.default(self, obj) -def handle_timeout(sig, frame): - exit(0) def main(): description = """ @@ -84,8 +81,7 @@ def main(): ynl.ntf_subscribe(args.ntf) if args.sleep: - signal.signal(signal.SIGALRM, handle_timeout) - signal.alarm(args.sleep) + time.sleep(args.sleep) if args.list_ops: for op_name, op in ynl.ops.items(): @@ -110,8 +106,8 @@ def main(): exit(1) if args.ntf: - for msg in ynl.check_ntf(): - output(msg) + ynl.check_ntf() + output(ynl.async_msg_queue) if __name__ == "__main__": diff --git a/tools/net/ynl/lib/ynl.py b/tools/net/ynl/lib/ynl.py index 92f85698c50e..c22c22bf2cb7 100644 --- a/tools/net/ynl/lib/ynl.py +++ b/tools/net/ynl/lib/ynl.py @@ -12,8 +12,6 @@ import sys import yaml import ipaddress import uuid -import queue -import time from .nlspec import SpecFamily @@ -491,7 +489,7 @@ class YnlFamily(SpecFamily): self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_GET_STRICT_CHK, 1) self.async_msg_ids = set() - self.async_msg_queue = queue.Queue() + self.async_msg_queue = [] for msg in self.msgs.values(): if msg.is_async: @@ -905,39 +903,32 @@ class YnlFamily(SpecFamily): msg['name'] = op['name'] msg['msg'] = attrs - self.async_msg_queue.put(msg) + self.async_msg_queue.append(msg) - def check_ntf(self, interval=0.1): + def check_ntf(self): while True: try: reply = self.sock.recv(self._recv_size, socket.MSG_DONTWAIT) - nms = NlMsgs(reply) - self._recv_dbg_print(reply, nms) - for nl_msg in nms: - if nl_msg.error: - print("Netlink error in ntf!?", os.strerror(-nl_msg.error)) - print(nl_msg) - continue - if nl_msg.done: - print("Netlink done while checking for ntf!?") - continue + except BlockingIOError: + return - decoded = self.nlproto.decode(self, nl_msg, None) - if decoded.cmd() not in self.async_msg_ids: - print("Unexpected msg id while checking for ntf", decoded) - continue + nms = NlMsgs(reply) + self._recv_dbg_print(reply, nms) + for nl_msg in nms: + if nl_msg.error: + print("Netlink error in ntf!?", os.strerror(-nl_msg.error)) + print(nl_msg) + continue + if nl_msg.done: + print("Netlink done while checking for ntf!?") + continue - self.handle_ntf(decoded) - except BlockingIOError: - pass + decoded = self.nlproto.decode(self, nl_msg, None) + if decoded.cmd() not in self.async_msg_ids: + print("Unexpected msg id done while checking for ntf", decoded) + continue - try: - yield self.async_msg_queue.get_nowait() - except queue.Empty: - try: - time.sleep(interval) - except KeyboardInterrupt: - return + self.handle_ntf(decoded) def operation_do_attributes(self, name): """ From patchwork Wed Nov 13 12:57:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiao Liang X-Patchwork-Id: 18679 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 7DD5C84029 for ; Wed, 13 Nov 2024 14:02:53 +0100 (CET) ARC-Seal: i=2; cv=pass; a=rsa-sha256; d=open-mesh.org; s=20121; t=1731502973; b=OD5mn7wygOD8XMvPqcrWlaqtNdTkxAHvKPsA26zcPErPVql7j53nrHQP07oFNjNbZ9ngC fn5fKzvx3ZusUU+KKN1n0jkD1/1K5o7g+AsqZ7zwFwCa6HjgY2rtX0jXUnfxZMdHfNxAizx GczLsKbBGYWzI44xaJ3vJaohiaWqKO4= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1731502973; 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=IrCaarQptqoPnVIGF6Rw6t9hI+Rf9ZxQVNSlbFD1HYQ=; b=IofiFhr2BigK3VEQzd2iST35Y8QnqcExnwsk8Zu954hQuv1Vc3LxKwANMzvFF9USXJy6o u0AY9G6exGvuKGRtcxNVv7ZX1wIIn2NMeI+oHJZwOW35ciLUXwi/qgK7rRz4U5JnYYf9/L0 mBs+d2eh55fLbzT4l+4sK+hCccXHqtY= 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-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by diktynna.open-mesh.org (Postfix) with ESMTPS id AC23481865 for ; Wed, 13 Nov 2024 13:57:54 +0100 (CET) ARC-Seal: i=1; s=20121; d=open-mesh.org; t=1731502674; a=rsa-sha256; cv=none; b=Lve/f6+zfkmFSZPRAjPPRjg0nzR2Mc6jXsA1ysF1Rqmiovt6T2wWgDvqk33SHTXthTxtAP pHT1VpFulTSeV3vd/wHY/rcWGZZA7nhTj+cAw/suq1/wpnViXAL9VbAmq9u1oKXOS3jnwn uOIQy1lyfiyGQwyHYC+NtXJL9b0Ys2c= ARC-Authentication-Results: i=1; diktynna.open-mesh.org; dkim=pass header.d=gmail.com header.s=20230601 header.b=UVNIWq+5; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (diktynna.open-mesh.org: domain of shaw.leon@gmail.com designates 2607:f8b0:4864:20::1031 as permitted sender) smtp.mailfrom=shaw.leon@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1731502674; 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=IrCaarQptqoPnVIGF6Rw6t9hI+Rf9ZxQVNSlbFD1HYQ=; b=ScjaBlYjwMgQRvBr35rMO5x+baoc3Fc2TeB52JnMhNXy93NkdaOhubr3suwUZPZo1xi48Y Csrd8A4wCTyPQOXqz1vJCkJNNua0v6W6zO/kVss6vCK6uipyMXSBgIkJ0ngO6fsBsBCaB5 cWRqSrwEsOZrWdMmWkMPQN7fUGODSrg= Received: by mail-pj1-x1031.google.com with SMTP id 98e67ed59e1d1-2e5a0177531so5402940a91.2 for ; Wed, 13 Nov 2024 04:57:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731502673; x=1732107473; 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=IrCaarQptqoPnVIGF6Rw6t9hI+Rf9ZxQVNSlbFD1HYQ=; b=UVNIWq+5PSVQbtaV5WPPbQgctyZzJdwpzbO6Hts6MrYR7m2+e3kSzEgGfogPbWTMB5 30sUYJQ0etb7mABowPaQm9Qr+ix4yGwDQZq5nPmGRLKsD0J+1bILn8bM9jxX/ytdzFS+ DSHddanTL+Z2p2aqBiZSHFk2i3uzMv8oFDRxc5RO18aPLnaOPbiPt0k9G8oiqAz7BPfR 7/jRiPotDVnjws+Wgq84Muvw/BFL0gGKiTlhMzF+Tb4heNux50NGzqWA057zTbPQy+EK ACANRAZhExQKDxFRdBV8SId+8P44Sww1byMJKrKMa8XZ6WilaZaJWAtddwVGOeSizLZk nyNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731502673; x=1732107473; 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=IrCaarQptqoPnVIGF6Rw6t9hI+Rf9ZxQVNSlbFD1HYQ=; b=tIOoB7lnD86p6Xu8j7sqJ40VVs23/utyLhntDArg1wSOpg5mXyUrWgCkpdI0ylngjS Kpa6+u5kFqLct5Yt3jeyyFH6hyz/rK8komFCg57WTF3BY98x72MkSZZWxyrtyWS8Xj9D rJFGO/72F8E0p0VqHGWFo40CW0eWHNXNAGfkK6zr5+AWKIqiHpU/3fJ+GWkRjGujcwC+ nX8+mqNynJQtZOLHg2gxoPQqIP0C1TKZONbKDsmX0x0YceSd8V5InHA5FZw1LWAFe/YE 8jlV/PsV0KCIAUuy3mK9tdt6SxJGWsPD4GfV9ToXbCCkMwQTdGWH1i28HvJRb/f+lxXy DxYg== X-Forwarded-Encrypted: i=1; AJvYcCWKc3PaWTgoCY4FNRWIY7ZNkcH3mqskLNqjQhBF9KJtuNfx6rheqPmCP9bmufKXqYB9rVbIdBRiqXBKew==@lists.open-mesh.org X-Gm-Message-State: AOJu0YwLm4KV6uLZho9O66gNLQIzUSpL8vN92tuMcIh4fipYBmcNjEDN eRjaHx1953+7YBpCKRrAqo3kVAYmAAudz0JZKAH7x9EApC1G6W2M X-Google-Smtp-Source: AGHT+IGJi6Tp37K+i8d+pRtqcGbSCXzQQJQiHEkYvBXRMyT+Ytmcy1PnetStqWOPaDWwW+msVvcvjQ== X-Received: by 2002:a17:90b:38c4:b0:2e2:c6a6:84da with SMTP id 98e67ed59e1d1-2e9b1799123mr24937176a91.34.1731502673070; Wed, 13 Nov 2024 04:57:53 -0800 (PST) Received: from nova-ws.. ([103.167.140.11]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9f3f8ed0esm1398632a91.40.2024.11.13.04.57.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Nov 2024 04:57:52 -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 v3 2/6] net: ip_tunnel: Build flow in underlay net namespace Date: Wed, 13 Nov 2024 20:57:11 +0800 Message-ID: <20241113125715.150201-3-shaw.leon@gmail.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241113125715.150201-1-shaw.leon@gmail.com> References: <20241113125715.150201-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: 46PNTN253ZNETZ2J4X4L6G6QVPWZNVGB X-Message-ID-Hash: 46PNTN253ZNETZ2J4X4L6G6QVPWZNVGB X-Mailman-Approved-At: Wed, 13 Nov 2024 14:00:42 +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: Build IPv4 flow in underlay net namespace, where encapsulated packets are routed. Signed-off-by: Xiao Liang --- net/ipv4/ip_tunnel.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/net/ipv4/ip_tunnel.c b/net/ipv4/ip_tunnel.c index 25505f9b724c..09b73acf037a 100644 --- a/net/ipv4/ip_tunnel.c +++ b/net/ipv4/ip_tunnel.c @@ -294,7 +294,7 @@ static int ip_tunnel_bind_dev(struct net_device *dev) ip_tunnel_init_flow(&fl4, iph->protocol, iph->daddr, iph->saddr, tunnel->parms.o_key, - iph->tos & INET_DSCP_MASK, dev_net(dev), + iph->tos & INET_DSCP_MASK, tunnel->net, tunnel->parms.link, tunnel->fwmark, 0, 0); rt = ip_route_output_key(tunnel->net, &fl4); @@ -611,7 +611,7 @@ void ip_md_tunnel_xmit(struct sk_buff *skb, struct net_device *dev, } ip_tunnel_init_flow(&fl4, proto, key->u.ipv4.dst, key->u.ipv4.src, tunnel_id_to_key32(key->tun_id), - tos & INET_DSCP_MASK, dev_net(dev), 0, skb->mark, + tos & INET_DSCP_MASK, tunnel->net, 0, skb->mark, skb_get_hash(skb), key->flow_flags); if (!tunnel_hlen) @@ -774,7 +774,7 @@ void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev, ip_tunnel_init_flow(&fl4, protocol, dst, tnl_params->saddr, tunnel->parms.o_key, tos & INET_DSCP_MASK, - dev_net(dev), READ_ONCE(tunnel->parms.link), + tunnel->net, READ_ONCE(tunnel->parms.link), tunnel->fwmark, skb_get_hash(skb), 0); if (ip_tunnel_encap(skb, &tunnel->encap, &protocol, &fl4) < 0) From patchwork Wed Nov 13 12:57:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiao Liang X-Patchwork-Id: 18680 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 00D0883F62 for ; Wed, 13 Nov 2024 14:03:37 +0100 (CET) ARC-Seal: i=2; cv=pass; a=rsa-sha256; d=open-mesh.org; s=20121; t=1731503016; b=PQl2mkmyi7zki6INIVAeZNCNAYUZuuQCpIvciZbVkgM6hdr1B0dS+yiph1XeXRWh1jnp9 j5v8cycXzCa638hHPm1LRJVDVG8j67cneHTgwXIe0n0gEufAcYt6JkI7mX2ZdTv2dEd4NnG bBknMVSAibnMQZ/2O5bKScCwJNPA5w4= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1731503016; 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=r30qTLZF8ANhBKyxqq0VyimbMaQgwK/VTM3BE1AEUlA=; b=AnXEa0o5Ou6BF2ByzS7Kc4uSi+I1ugd0ju/4OOHsfMsAumqzyHzQnNRRd4oCzncQMS0A5 rzp68whEzgwg+g/5wMsuCXis3EkRMrTL15vdtnfIk8WroZPWNmD7ClrOzcKiOaOOWQ5uYqr Bc8qUCIvPHldqJMZWKAOUb2S+bttmpI= 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-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by diktynna.open-mesh.org (Postfix) with ESMTPS id F000183002 for ; Wed, 13 Nov 2024 13:58:02 +0100 (CET) ARC-Seal: i=1; s=20121; d=open-mesh.org; t=1731502683; a=rsa-sha256; cv=none; b=UUP4ZsGdVU78ELNntC9zNsbuNs1kALESRbnAnK4I379xzNMPtuFtYJ77AjuPsr3nGMeuL7 bCV2tStHv9Ep6cEMtjnvr3rWZ3X31JjpLRKPS/yk7Fp98wTFhyjaXuHTezeq5fMwTumvD9 sK9wXqwfUaODRMxaEyzGFpKtfT/K1F4= ARC-Authentication-Results: i=1; diktynna.open-mesh.org; dkim=pass header.d=gmail.com header.s=20230601 header.b=eKw+btc1; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (diktynna.open-mesh.org: domain of shaw.leon@gmail.com designates 2607:f8b0:4864:20::1031 as permitted sender) smtp.mailfrom=shaw.leon@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1731502683; 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=r30qTLZF8ANhBKyxqq0VyimbMaQgwK/VTM3BE1AEUlA=; b=ujQbrLFkd49+zOQCEUmP5oiPVL3OAWFAPlHkzTbLqdKDpEetYYxuMrs23qrVFpVdKUdFHE lIIprtWsyKwu5eV21j02JTcvSzyz0uEwut6aZwZfucPWDKWvGW03awIFUMQwORORAnQ7Oz 4SPFp8IjkC/LF3MxJt8YIG0ZKeUJ1tc= Received: by mail-pj1-x1031.google.com with SMTP id 98e67ed59e1d1-2e2ad9825a7so5287514a91.0 for ; Wed, 13 Nov 2024 04:58:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731502681; x=1732107481; 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=r30qTLZF8ANhBKyxqq0VyimbMaQgwK/VTM3BE1AEUlA=; b=eKw+btc1PQs6Wj5Ea4hkn5hgjz8iHapSvkIAPKrLFnY7DYBHQvfk/naSAIYYFryf56 IQYLokoAQaBGh9bLXNZ+u2v9BQdaO+lrIGV6ad3otREgPV4oQevCG+mBOKoAPnSoOGRK AWyGbARU+lKH9HHceQAVimhO0ws1qTfxKyKXua/DQ62TDYa3x0DXJmrq4heHGgU9zGXi d3Dj+xE/Xujc6rTGgo9k+k6lZwjlsMfh3roISASQvAVFGLLg+vQINoHfPGE0cWajBwzX oHcRjGCfC1+DSrIzxqnHexh1hqpRh3SAxCufcCqWjFgCk8I7zyqsFVx1igEYOhcm1ADa kGzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731502681; x=1732107481; 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=r30qTLZF8ANhBKyxqq0VyimbMaQgwK/VTM3BE1AEUlA=; b=tAz7yNC0FCVOymdkP20pdMZ1aanNgZZVZ+QyrDRKeIJnr7uav6U4fvtx4dki7Fs8Ll fQwH8A9u8i47kJsIfjJU053KVbtyyjhdptJ87chWvz/MbrydGFcSUOK6bvVqQuMqph3C hPta899afvpFTxJTqrFMfP/EdMrowlb+t2IPbDHF8eSwS4rEGxVXBh03p62MFl33vChM H2MNa1X6e9u7RlXMLhCqRv8eVi7eAU+CwzZEWIx8KuNatJgr6kFFKqN4aP4RiYdSqeAQ gg28+iYxBWjXqVVYQ1rCyDAIsuSq8lzWNlpRbieGjyxnx9XHurEwFGGgORmd3hEEWQPw re6g== X-Forwarded-Encrypted: i=1; AJvYcCXY2Z/RAmwveFyaFJjWDJW4aQk0mcxub7oawKIjLiaqbvGLgp1LrZ8LzLXkzRDH/+wP0A+A9QBMLvxr2g==@lists.open-mesh.org X-Gm-Message-State: AOJu0Yyghf4Y4wGQTpALgUv19LBfdLA18o1sVP6V448KvznWY49X5pY6 t47pPUZgH9JTCUVLrwKi3JZUNPproA2NZ916GFgIBKsq7XMo8wknubmcrMRGyf0= X-Google-Smtp-Source: AGHT+IGHbWu6TztKh6tN8Sp6Cv2pF+0T1uVgEKu2tiVbycxbLB+U4mJhCEtmHq5ke3dUIYABcOdsZA== X-Received: by 2002:a17:90b:3907:b0:2c9:df1c:4a58 with SMTP id 98e67ed59e1d1-2e9b177fdcdmr24476632a91.23.1731502681391; Wed, 13 Nov 2024 04:58:01 -0800 (PST) Received: from nova-ws.. ([103.167.140.11]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9f3f8ed0esm1398632a91.40.2024.11.13.04.57.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Nov 2024 04:58:00 -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 v3 3/6] rtnetlink: Lookup device in target netns when creating link Date: Wed, 13 Nov 2024 20:57:12 +0800 Message-ID: <20241113125715.150201-4-shaw.leon@gmail.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241113125715.150201-1-shaw.leon@gmail.com> References: <20241113125715.150201-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: 2WFKQOVLS4IIX3VWAPDMCZWC4JGA7R6U X-Message-ID-Hash: 2WFKQOVLS4IIX3VWAPDMCZWC4JGA7R6U X-Mailman-Approved-At: Wed, 13 Nov 2024 14:00:42 +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: When creating link, lookup for existing device in target net namespace instead of current one. For example, two links created by: # ip link add dummy1 type dummy # ip link add netns ns1 dummy1 type dummy should have no conflict since they are in different namespaces. Signed-off-by: Xiao Liang --- net/core/rtnetlink.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c index 327fa4957929..f573ace60234 100644 --- a/net/core/rtnetlink.c +++ b/net/core/rtnetlink.c @@ -3846,20 +3846,26 @@ static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, { struct nlattr ** const tb = tbs->tb; struct net *net = sock_net(skb->sk); + struct net *device_net; struct net_device *dev; struct ifinfomsg *ifm; bool link_specified; + /* When creating, lookup for existing device in target net namespace */ + device_net = (nlh->nlmsg_flags & NLM_F_CREATE) && + (nlh->nlmsg_flags & NLM_F_EXCL) ? + tgt_net : net; + ifm = nlmsg_data(nlh); if (ifm->ifi_index > 0) { link_specified = true; - dev = __dev_get_by_index(net, ifm->ifi_index); + dev = __dev_get_by_index(device_net, ifm->ifi_index); } else if (ifm->ifi_index < 0) { NL_SET_ERR_MSG(extack, "ifindex can't be negative"); return -EINVAL; } else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) { link_specified = true; - dev = rtnl_dev_get(net, tb); + dev = rtnl_dev_get(device_net, tb); } else { link_specified = false; dev = NULL; From patchwork Wed Nov 13 12:57:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiao Liang X-Patchwork-Id: 18681 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 B961D83FF7 for ; Wed, 13 Nov 2024 14:04:18 +0100 (CET) ARC-Seal: i=2; cv=pass; a=rsa-sha256; d=open-mesh.org; s=20121; t=1731503058; b=Nonuoc9L+3RcuEpKNFVwmj+/giTDI/WyHl1v4x8hYQkbSBjdRQVA+AJUr0yt6CJB+FGIg 77BRyzMan0aMsbF/iLB89/AL8/jYBgkfT7DmU5C1AVu+4bLo/Nxr5lophxCbjJOjD9iT1xY GHM5q/eNlw7/kXpdfBDV8GTkmTfcaec= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1731503058; 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=c4uHKNgHTelYlny2krcQ638zz7tT9VKe4QfYY382d0Q=; b=hgrVtho4wo5GqhnJqbOB0HM3PdyFW5HRRWU412wTpLp3z3UmxnRywEme3Qdrz5OXlqs4X VrpcVze+MaXmCYubFrbhn567otnuO75Lif/vwPhNfvIvPYF4gurcgiJexmIUJyRwlmYq4UU GEiDPZ4rlNn6TGiv9Z6rD97vJF5XRmw= 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-pg1-x52d.google.com (mail-pg1-x52d.google.com [IPv6:2607:f8b0:4864:20::52d]) by diktynna.open-mesh.org (Postfix) with ESMTPS id 3F96F831EC for ; Wed, 13 Nov 2024 13:58:12 +0100 (CET) ARC-Seal: i=1; s=20121; d=open-mesh.org; t=1731502692; a=rsa-sha256; cv=none; b=bICYMEv/iEWH5Mxdia3PZuYVKdWm08iqbTern4gqWAouv2W2jbLqBlWR0egcAwc5Yig11m MLXH5abaTgkWr1vZ1DLGSe4EL6066AAkSJrKup4PGSQhyOssWZc43hBAeB6PoaNeXUWeTZ 01qLdEA16EvZv72EQSVMLtgBXVUxGdE= ARC-Authentication-Results: i=1; diktynna.open-mesh.org; dkim=pass header.d=gmail.com header.s=20230601 header.b=GaxCkmrM; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (diktynna.open-mesh.org: domain of shaw.leon@gmail.com designates 2607:f8b0:4864:20::52d as permitted sender) smtp.mailfrom=shaw.leon@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1731502692; 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=c4uHKNgHTelYlny2krcQ638zz7tT9VKe4QfYY382d0Q=; b=dHY2MGYRz8jCNjPWRu+DdFghqmDBD+twPXxRu+FT9wdtbOyFQrxCFJKXvKkEx7Hw890O00 5FRUMFdsM9OUEZF7CYvBNYCT8IPyRYiuRp0qVaLSE1myH5Y1tqjLgqcxn2RtwDx+QiMjLI G4Lv2ZZQijwufuuJamhadEALU4faH6Y= Received: by mail-pg1-x52d.google.com with SMTP id 41be03b00d2f7-7ed9c16f687so4687913a12.0 for ; Wed, 13 Nov 2024 04:58:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731502691; x=1732107491; 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=c4uHKNgHTelYlny2krcQ638zz7tT9VKe4QfYY382d0Q=; b=GaxCkmrMPyH9Ncwd0EHKBlPRC6wC2NAMDKrTwVSnr7u2ZteCWqC8wuceYX7MQ0e3If 5FNQhr1IGUhR3n9pWNgbxJUJdK1kfsGsWMq5r5KNezrIjYwHIE2QEu0wShOdxWuSTcYR 8mAAs3/AcReE2YXsngu1mQulQKNuRKSO0FK6w2wDmnrsNdLSxtWbtAkeUvo7U3aIlp/3 lU1yZI1l7G+2Je13OnfO554/C81vu8/1vq7YM/pZSR0qnX7M6HU95i1FXMRkopgmaUWf b6Ayano/UYwPLe3zQC58ywfD3Yi9VeGFtASXDQCRnXm4oOoUAgJL2DVDcx8HM8yQ0qhz 9yjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731502691; x=1732107491; 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=c4uHKNgHTelYlny2krcQ638zz7tT9VKe4QfYY382d0Q=; b=bwvmvrnROHQWbOuKmT+sZG30oWiHtfP/96stDn8D3jpExHPM0lboQ69+IlIHE9AuFF tW/5K7TNoM7JqKSQzMuZNkOetGyjRYHhEye1ZC098/RbM6/ByLTK7aTAISdPkvo8o7t+ obNfHpBlxPRM9BPcSiZXrsrGWJrtq44Y4Qw0QaIBmnSfiGYFX6A6z/3oUsQA4ej8GXpt +jUDqmuGtq2XrKjxuPrmB+NFNLz+mMAvCb6S5BL74K3EtZVb4qtM3IN2fO3NkhunWkKs J/wrB6qqbx4OfwoQgDYDICYFKxj8C0DF9XSpgAkQHyYa6kXlgvDPMnpNVe+dUTBHwUge 01gw== X-Forwarded-Encrypted: i=1; AJvYcCWFxFgNw2543FStpBTxqadVhKr6Zra+O3E70aQvAy6L/Gmqj7QIt9wTuohA9+7y8QquGQVDohoJQ1VCHA==@lists.open-mesh.org X-Gm-Message-State: AOJu0YxHJFNcuXd+bKZh+F8y74foSR3LM4i5+WPqoHgaECtMYkH//uhq ji8aBIGZtB69EjbKkNx6WTZBfIV1X2AjvNhgV0HKyelutCeZVhwL X-Google-Smtp-Source: AGHT+IEW/8FLBI4yQo5ibxoGCxIofggmcjjVftErLiAf+Ryg0MG2XED6WhS+2Fn2LgKf4ZhExPXwEQ== X-Received: by 2002:a05:6a20:244d:b0:1db:e0d7:675c with SMTP id adf61e73a8af0-1dc7037f641mr3401109637.13.1731502690293; Wed, 13 Nov 2024 04:58:10 -0800 (PST) Received: from nova-ws.. ([103.167.140.11]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9f3f8ed0esm1398632a91.40.2024.11.13.04.58.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Nov 2024 04:58:09 -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 v3 4/6] rtnetlink: Decouple net namespaces in rtnl_newlink_create() Date: Wed, 13 Nov 2024 20:57:13 +0800 Message-ID: <20241113125715.150201-5-shaw.leon@gmail.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241113125715.150201-1-shaw.leon@gmail.com> References: <20241113125715.150201-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: YFOHY57N7IW4IYLIEBXAB2HEG4KA5HAA X-Message-ID-Hash: YFOHY57N7IW4IYLIEBXAB2HEG4KA5HAA X-Mailman-Approved-At: Wed, 13 Nov 2024 14:00:42 +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: There are three net namespaces involved when creating links: - source netns - where the netlink socket resides, - target netns - where to put the device being created, - link netns - netns associated with the device (backend). Currently, two nets are passed to newlink() callback - "src_net" parameter and "dev_net" (implicitly in net_device). They are set as follows, depending on whether IFLA_LINK_NETNSID is present. +-------------------+---------+---------+ | IFLA_LINK_NETNSID | src_net | dev_net | +-------------------+---------+---------+ | absent | source | target | +-------------------+---------+---------+ | present | link | link | +-------------------+---------+---------+ When IFLA_LINK_NETNSID is present, the device is created in link netns first. This has some side effects, including extra ifindex allocation, ifname validation and link notifications. There's also an extra step to move the device to target netns. These could be avoided if we create it in target netns at the beginning. On the other hand, the meaning of src_net is ambiguous. It should be the effective link netns by design, but some drivers ignore it and use dev_net instead. This patch refactors netns handling. rtnl_newlink_create() now creates devices in target netns directly, so dev_net is always target netns. Source and link netns are passed to newlink() as src_net and link_net in a new struct consistently. When determining the effective link netns, in the absence link_net, drivers should look for src_net in general. But for compatibility, drivers that use dev_net will keep current behavior. Signed-off-by: Xiao Liang --- There're some issues found when coverting drivers. Please check if they work as intended: - In amt_newlink() drivers/net/amt.c: amt->net = net; ... amt->stream_dev = dev_get_by_index(net, ... Uses net (src_net actually), but amt_lookup_upper_dev() only searches in dev_net. - In gtp_newlink() in drivers/net/gtp.c: gtp->net = src_net; ... gn = net_generic(dev_net(dev), gtp_net_id); list_add_rcu(>p->list, &gn->gtp_dev_list); Uses src_net, but is linked to list in dev_net. - In pfcp_newlink() in drivers/net/pfcp.c: pfcp->net = net; ... pn = net_generic(dev_net(dev), pfcp_net_id); list_add_rcu(&pfcp->list, &pn->pfcp_dev_list); Same. - In lowpan_newlink() in net/ieee802154/6lowpan/core.c: wdev = dev_get_by_index(dev_net(ldev), nla_get_u32(tb[IFLA_LINK])); Looks for IFLA_LINK in dev_net, but in theory the ifindex is defined in link netns. --- drivers/infiniband/ulp/ipoib/ipoib_netlink.c | 6 +++-- drivers/net/amt.c | 6 ++--- drivers/net/bareudp.c | 4 ++-- drivers/net/bonding/bond_netlink.c | 3 ++- drivers/net/can/dev/netlink.c | 2 +- drivers/net/can/vxcan.c | 4 ++-- .../ethernet/qualcomm/rmnet/rmnet_config.c | 5 +++-- drivers/net/geneve.c | 4 ++-- drivers/net/gtp.c | 4 ++-- drivers/net/ipvlan/ipvlan.h | 2 +- drivers/net/ipvlan/ipvlan_main.c | 5 +++-- drivers/net/ipvlan/ipvtap.c | 4 ++-- drivers/net/macsec.c | 5 +++-- drivers/net/macvlan.c | 5 +++-- drivers/net/macvtap.c | 5 +++-- drivers/net/netkit.c | 4 ++-- drivers/net/pfcp.c | 4 ++-- drivers/net/ppp/ppp_generic.c | 4 ++-- drivers/net/team/team_core.c | 2 +- drivers/net/veth.c | 4 ++-- drivers/net/vrf.c | 2 +- drivers/net/vxlan/vxlan_core.c | 4 ++-- drivers/net/wireguard/device.c | 4 ++-- drivers/net/wireless/virtual/virt_wifi.c | 5 +++-- drivers/net/wwan/wwan_core.c | 6 +++-- include/net/ip_tunnels.h | 5 +++-- include/net/rtnetlink.h | 22 ++++++++++++++++++- net/8021q/vlan_netlink.c | 5 +++-- net/batman-adv/soft-interface.c | 5 +++-- net/bridge/br_netlink.c | 2 +- net/caif/chnl_net.c | 2 +- net/core/rtnetlink.c | 15 ++++++------- net/hsr/hsr_netlink.c | 8 +++---- net/ieee802154/6lowpan/core.c | 5 +++-- net/ipv4/ip_gre.c | 13 ++++++----- net/ipv4/ip_tunnel.c | 10 +++++---- net/ipv4/ip_vti.c | 5 +++-- net/ipv4/ipip.c | 5 +++-- net/ipv6/ip6_gre.c | 17 +++++++------- net/ipv6/ip6_tunnel.c | 11 +++++----- net/ipv6/ip6_vti.c | 11 +++++----- net/ipv6/sit.c | 11 +++++----- net/xfrm/xfrm_interface_core.c | 13 +++++------ 43 files changed, 153 insertions(+), 115 deletions(-) diff --git a/drivers/infiniband/ulp/ipoib/ipoib_netlink.c b/drivers/infiniband/ulp/ipoib/ipoib_netlink.c index 9ad8d9856275..7714115a4635 100644 --- a/drivers/infiniband/ulp/ipoib/ipoib_netlink.c +++ b/drivers/infiniband/ulp/ipoib/ipoib_netlink.c @@ -97,7 +97,8 @@ static int ipoib_changelink(struct net_device *dev, struct nlattr *tb[], return ret; } -static int ipoib_new_child_link(struct net *src_net, struct net_device *dev, +static int ipoib_new_child_link(struct rtnl_link_nets *nets, + struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { @@ -109,7 +110,8 @@ static int ipoib_new_child_link(struct net *src_net, struct net_device *dev, if (!tb[IFLA_LINK]) return -EINVAL; - pdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); + pdev = __dev_get_by_index(rtnl_link_netns(nets), + nla_get_u32(tb[IFLA_LINK])); if (!pdev || pdev->type != ARPHRD_INFINIBAND) return -ENODEV; diff --git a/drivers/net/amt.c b/drivers/net/amt.c index 98c6205ed19f..9537175f7ac8 100644 --- a/drivers/net/amt.c +++ b/drivers/net/amt.c @@ -3161,14 +3161,14 @@ static int amt_validate(struct nlattr *tb[], struct nlattr *data[], return 0; } -static int amt_newlink(struct net *net, struct net_device *dev, +static int amt_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { struct amt_dev *amt = netdev_priv(dev); int err = -EINVAL; - amt->net = net; + amt->net = rtnl_link_netns(nets); amt->mode = nla_get_u32(data[IFLA_AMT_MODE]); if (data[IFLA_AMT_MAX_TUNNELS] && @@ -3183,7 +3183,7 @@ static int amt_newlink(struct net *net, struct net_device *dev, amt->hash_buckets = AMT_HSIZE; amt->nr_tunnels = 0; get_random_bytes(&amt->hash_seed, sizeof(amt->hash_seed)); - amt->stream_dev = dev_get_by_index(net, + amt->stream_dev = dev_get_by_index(rtnl_link_netns(nets), nla_get_u32(data[IFLA_AMT_LINK])); if (!amt->stream_dev) { NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_LINK], diff --git a/drivers/net/bareudp.c b/drivers/net/bareudp.c index a2abfade82dd..c0946006d175 100644 --- a/drivers/net/bareudp.c +++ b/drivers/net/bareudp.c @@ -698,7 +698,7 @@ static void bareudp_dellink(struct net_device *dev, struct list_head *head) unregister_netdevice_queue(dev, head); } -static int bareudp_newlink(struct net *net, struct net_device *dev, +static int bareudp_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { @@ -709,7 +709,7 @@ static int bareudp_newlink(struct net *net, struct net_device *dev, if (err) return err; - err = bareudp_configure(net, dev, &conf, extack); + err = bareudp_configure(rtnl_link_netns(nets), dev, &conf, extack); if (err) return err; diff --git a/drivers/net/bonding/bond_netlink.c b/drivers/net/bonding/bond_netlink.c index 2a6a424806aa..242a37934f25 100644 --- a/drivers/net/bonding/bond_netlink.c +++ b/drivers/net/bonding/bond_netlink.c @@ -564,7 +564,8 @@ static int bond_changelink(struct net_device *bond_dev, struct nlattr *tb[], return 0; } -static int bond_newlink(struct net *src_net, struct net_device *bond_dev, +static int bond_newlink(struct rtnl_link_nets *nets, + struct net_device *bond_dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { diff --git a/drivers/net/can/dev/netlink.c b/drivers/net/can/dev/netlink.c index 01aacdcda260..daf88325ccac 100644 --- a/drivers/net/can/dev/netlink.c +++ b/drivers/net/can/dev/netlink.c @@ -624,7 +624,7 @@ static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev) return -EMSGSIZE; } -static int can_newlink(struct net *src_net, struct net_device *dev, +static int can_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { diff --git a/drivers/net/can/vxcan.c b/drivers/net/can/vxcan.c index da7c72105fb6..2375175044bc 100644 --- a/drivers/net/can/vxcan.c +++ b/drivers/net/can/vxcan.c @@ -172,7 +172,7 @@ static void vxcan_setup(struct net_device *dev) /* forward declaration for rtnl_create_link() */ static struct rtnl_link_ops vxcan_link_ops; -static int vxcan_newlink(struct net *net, struct net_device *dev, +static int vxcan_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { @@ -203,7 +203,7 @@ static int vxcan_newlink(struct net *net, struct net_device *dev, name_assign_type = NET_NAME_ENUM; } - peer_net = rtnl_link_get_net(net, tbp); + peer_net = rtnl_link_get_net(rtnl_link_netns(nets), tbp); peer = rtnl_create_link(peer_net, ifname, name_assign_type, &vxcan_link_ops, tbp, extack); if (IS_ERR(peer)) { diff --git a/drivers/net/ethernet/qualcomm/rmnet/rmnet_config.c b/drivers/net/ethernet/qualcomm/rmnet/rmnet_config.c index f3bea196a8f9..fa881b038d69 100644 --- a/drivers/net/ethernet/qualcomm/rmnet/rmnet_config.c +++ b/drivers/net/ethernet/qualcomm/rmnet/rmnet_config.c @@ -117,7 +117,7 @@ static void rmnet_unregister_bridge(struct rmnet_port *port) rmnet_unregister_real_device(bridge_dev); } -static int rmnet_newlink(struct net *src_net, struct net_device *dev, +static int rmnet_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { @@ -134,7 +134,8 @@ static int rmnet_newlink(struct net *src_net, struct net_device *dev, return -EINVAL; } - real_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); + real_dev = __dev_get_by_index(rtnl_link_netns(nets), + nla_get_u32(tb[IFLA_LINK])); if (!real_dev) { NL_SET_ERR_MSG_MOD(extack, "link does not exist"); return -ENODEV; diff --git a/drivers/net/geneve.c b/drivers/net/geneve.c index 2f29b1386b1c..f621d5887b3d 100644 --- a/drivers/net/geneve.c +++ b/drivers/net/geneve.c @@ -1614,7 +1614,7 @@ static void geneve_link_config(struct net_device *dev, geneve_change_mtu(dev, ldev_mtu - info->options_len); } -static int geneve_newlink(struct net *net, struct net_device *dev, +static int geneve_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { @@ -1631,7 +1631,7 @@ static int geneve_newlink(struct net *net, struct net_device *dev, if (err) return err; - err = geneve_configure(net, dev, extack, &cfg); + err = geneve_configure(rtnl_link_netns(nets), dev, extack, &cfg); if (err) return err; diff --git a/drivers/net/gtp.c b/drivers/net/gtp.c index 89a996ad8cd0..73ad219746ec 100644 --- a/drivers/net/gtp.c +++ b/drivers/net/gtp.c @@ -1460,7 +1460,7 @@ static int gtp_create_sockets(struct gtp_dev *gtp, const struct nlattr *nla, #define GTP_TH_MAXLEN (sizeof(struct udphdr) + sizeof(struct gtp0_header)) #define GTP_IPV6_MAXLEN (sizeof(struct ipv6hdr) + GTP_TH_MAXLEN) -static int gtp_newlink(struct net *src_net, struct net_device *dev, +static int gtp_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { @@ -1494,7 +1494,7 @@ static int gtp_newlink(struct net *src_net, struct net_device *dev, gtp->restart_count = nla_get_u8_default(data[IFLA_GTP_RESTART_COUNT], 0); - gtp->net = src_net; + gtp->net = rtnl_link_netns(nets); err = gtp_hashtable_new(gtp, hashsize); if (err < 0) diff --git a/drivers/net/ipvlan/ipvlan.h b/drivers/net/ipvlan/ipvlan.h index 025e0c19ec25..81e11086df51 100644 --- a/drivers/net/ipvlan/ipvlan.h +++ b/drivers/net/ipvlan/ipvlan.h @@ -166,7 +166,7 @@ struct ipvl_addr *ipvlan_addr_lookup(struct ipvl_port *port, void *lyr3h, void *ipvlan_get_L3_hdr(struct ipvl_port *port, struct sk_buff *skb, int *type); void ipvlan_count_rx(const struct ipvl_dev *ipvlan, unsigned int len, bool success, bool mcast); -int ipvlan_link_new(struct net *src_net, struct net_device *dev, +int ipvlan_link_new(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack); void ipvlan_link_delete(struct net_device *dev, struct list_head *head); diff --git a/drivers/net/ipvlan/ipvlan_main.c b/drivers/net/ipvlan/ipvlan_main.c index ee2c3cf4df36..5bd670927d27 100644 --- a/drivers/net/ipvlan/ipvlan_main.c +++ b/drivers/net/ipvlan/ipvlan_main.c @@ -532,7 +532,7 @@ static int ipvlan_nl_fillinfo(struct sk_buff *skb, return ret; } -int ipvlan_link_new(struct net *src_net, struct net_device *dev, +int ipvlan_link_new(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { @@ -545,7 +545,8 @@ int ipvlan_link_new(struct net *src_net, struct net_device *dev, if (!tb[IFLA_LINK]) return -EINVAL; - phy_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); + phy_dev = __dev_get_by_index(rtnl_link_netns(nets), + nla_get_u32(tb[IFLA_LINK])); if (!phy_dev) return -ENODEV; diff --git a/drivers/net/ipvlan/ipvtap.c b/drivers/net/ipvlan/ipvtap.c index 1afc4c47be73..7d8f780581e3 100644 --- a/drivers/net/ipvlan/ipvtap.c +++ b/drivers/net/ipvlan/ipvtap.c @@ -73,7 +73,7 @@ static void ipvtap_update_features(struct tap_dev *tap, netdev_update_features(vlan->dev); } -static int ipvtap_newlink(struct net *src_net, struct net_device *dev, +static int ipvtap_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { @@ -97,7 +97,7 @@ static int ipvtap_newlink(struct net *src_net, struct net_device *dev, /* Don't put anything that may fail after macvlan_common_newlink * because we can't undo what it does. */ - err = ipvlan_link_new(src_net, dev, tb, data, extack); + err = ipvlan_link_new(nets, dev, tb, data, extack); if (err) { netdev_rx_handler_unregister(dev); return err; diff --git a/drivers/net/macsec.c b/drivers/net/macsec.c index 1bc1e5993f56..c7871c6747ca 100644 --- a/drivers/net/macsec.c +++ b/drivers/net/macsec.c @@ -4141,7 +4141,7 @@ static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len) static struct lock_class_key macsec_netdev_addr_lock_key; -static int macsec_newlink(struct net *net, struct net_device *dev, +static int macsec_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { @@ -4154,7 +4154,8 @@ static int macsec_newlink(struct net *net, struct net_device *dev, if (!tb[IFLA_LINK]) return -EINVAL; - real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK])); + real_dev = __dev_get_by_index(rtnl_link_netns(nets), + nla_get_u32(tb[IFLA_LINK])); if (!real_dev) return -ENODEV; if (real_dev->type != ARPHRD_ETHER) diff --git a/drivers/net/macvlan.c b/drivers/net/macvlan.c index edbd5afcec41..24a1630e2ead 100644 --- a/drivers/net/macvlan.c +++ b/drivers/net/macvlan.c @@ -1565,11 +1565,12 @@ int macvlan_common_newlink(struct net *src_net, struct net_device *dev, } EXPORT_SYMBOL_GPL(macvlan_common_newlink); -static int macvlan_newlink(struct net *src_net, struct net_device *dev, +static int macvlan_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { - return macvlan_common_newlink(src_net, dev, tb, data, extack); + return macvlan_common_newlink(rtnl_link_netns(nets), dev, tb, data, + extack); } void macvlan_dellink(struct net_device *dev, struct list_head *head) diff --git a/drivers/net/macvtap.c b/drivers/net/macvtap.c index 29a5929d48e5..99152d4ba82b 100644 --- a/drivers/net/macvtap.c +++ b/drivers/net/macvtap.c @@ -77,7 +77,7 @@ static void macvtap_update_features(struct tap_dev *tap, netdev_update_features(vlan->dev); } -static int macvtap_newlink(struct net *src_net, struct net_device *dev, +static int macvtap_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { @@ -105,7 +105,8 @@ static int macvtap_newlink(struct net *src_net, struct net_device *dev, /* Don't put anything that may fail after macvlan_common_newlink * because we can't undo what it does. */ - err = macvlan_common_newlink(src_net, dev, tb, data, extack); + err = macvlan_common_newlink(rtnl_link_netns(nets), dev, tb, data, + extack); if (err) { netdev_rx_handler_unregister(dev); return err; diff --git a/drivers/net/netkit.c b/drivers/net/netkit.c index bb07725d1c72..70837ebbb9f2 100644 --- a/drivers/net/netkit.c +++ b/drivers/net/netkit.c @@ -327,7 +327,7 @@ static int netkit_validate(struct nlattr *tb[], struct nlattr *data[], static struct rtnl_link_ops netkit_link_ops; -static int netkit_new_link(struct net *src_net, struct net_device *dev, +static int netkit_new_link(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { @@ -385,7 +385,7 @@ static int netkit_new_link(struct net *src_net, struct net_device *dev, (tb[IFLA_ADDRESS] || tbp[IFLA_ADDRESS])) return -EOPNOTSUPP; - net = rtnl_link_get_net(src_net, tbp); + net = rtnl_link_get_net(rtnl_link_netns(nets), tbp); peer = rtnl_create_link(net, ifname, ifname_assign_type, &netkit_link_ops, tbp, extack); if (IS_ERR(peer)) { diff --git a/drivers/net/pfcp.c b/drivers/net/pfcp.c index 69434fd13f96..e1cf41cb9a87 100644 --- a/drivers/net/pfcp.c +++ b/drivers/net/pfcp.c @@ -184,7 +184,7 @@ static int pfcp_add_sock(struct pfcp_dev *pfcp) return PTR_ERR_OR_ZERO(pfcp->sock); } -static int pfcp_newlink(struct net *net, struct net_device *dev, +static int pfcp_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { @@ -192,7 +192,7 @@ static int pfcp_newlink(struct net *net, struct net_device *dev, struct pfcp_net *pn; int err; - pfcp->net = net; + pfcp->net = rtnl_link_netns(nets); err = pfcp_add_sock(pfcp); if (err) { diff --git a/drivers/net/ppp/ppp_generic.c b/drivers/net/ppp/ppp_generic.c index 4583e15ad03a..87ade0389d0d 100644 --- a/drivers/net/ppp/ppp_generic.c +++ b/drivers/net/ppp/ppp_generic.c @@ -1303,7 +1303,7 @@ static int ppp_nl_validate(struct nlattr *tb[], struct nlattr *data[], return 0; } -static int ppp_nl_newlink(struct net *src_net, struct net_device *dev, +static int ppp_nl_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { @@ -1343,7 +1343,7 @@ static int ppp_nl_newlink(struct net *src_net, struct net_device *dev, if (!tb[IFLA_IFNAME] || !nla_len(tb[IFLA_IFNAME]) || !*(char *)nla_data(tb[IFLA_IFNAME])) conf.ifname_is_set = false; - err = ppp_dev_configure(src_net, dev, &conf); + err = ppp_dev_configure(rtnl_link_netns(nets), dev, &conf); out_unlock: mutex_unlock(&ppp_mutex); diff --git a/drivers/net/team/team_core.c b/drivers/net/team/team_core.c index a1b27b69f010..d0f1fc404264 100644 --- a/drivers/net/team/team_core.c +++ b/drivers/net/team/team_core.c @@ -2206,7 +2206,7 @@ static void team_setup(struct net_device *dev) dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX; } -static int team_newlink(struct net *src_net, struct net_device *dev, +static int team_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { diff --git a/drivers/net/veth.c b/drivers/net/veth.c index 0d6d0d749d44..a64d0c87b18f 100644 --- a/drivers/net/veth.c +++ b/drivers/net/veth.c @@ -1765,7 +1765,7 @@ static int veth_init_queues(struct net_device *dev, struct nlattr *tb[]) return 0; } -static int veth_newlink(struct net *src_net, struct net_device *dev, +static int veth_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { @@ -1800,7 +1800,7 @@ static int veth_newlink(struct net *src_net, struct net_device *dev, name_assign_type = NET_NAME_ENUM; } - net = rtnl_link_get_net(src_net, tbp); + net = rtnl_link_get_net(rtnl_link_netns(nets), tbp); peer = rtnl_create_link(net, ifname, name_assign_type, &veth_link_ops, tbp, extack); if (IS_ERR(peer)) { diff --git a/drivers/net/vrf.c b/drivers/net/vrf.c index 67d25f4f94ef..7dd53d84b289 100644 --- a/drivers/net/vrf.c +++ b/drivers/net/vrf.c @@ -1698,7 +1698,7 @@ static void vrf_dellink(struct net_device *dev, struct list_head *head) unregister_netdevice_queue(dev, head); } -static int vrf_newlink(struct net *src_net, struct net_device *dev, +static int vrf_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { diff --git a/drivers/net/vxlan/vxlan_core.c b/drivers/net/vxlan/vxlan_core.c index 42b07bc2b107..6a7fee5fc504 100644 --- a/drivers/net/vxlan/vxlan_core.c +++ b/drivers/net/vxlan/vxlan_core.c @@ -4345,7 +4345,7 @@ static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[], return 0; } -static int vxlan_newlink(struct net *src_net, struct net_device *dev, +static int vxlan_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { @@ -4356,7 +4356,7 @@ static int vxlan_newlink(struct net *src_net, struct net_device *dev, if (err) return err; - return __vxlan_dev_create(src_net, dev, &conf, extack); + return __vxlan_dev_create(rtnl_link_netns(nets), dev, &conf, extack); } static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[], diff --git a/drivers/net/wireguard/device.c b/drivers/net/wireguard/device.c index 45e9b908dbfb..25118e1085f7 100644 --- a/drivers/net/wireguard/device.c +++ b/drivers/net/wireguard/device.c @@ -306,14 +306,14 @@ static void wg_setup(struct net_device *dev) wg->dev = dev; } -static int wg_newlink(struct net *src_net, struct net_device *dev, +static int wg_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { struct wg_device *wg = netdev_priv(dev); int ret = -ENOMEM; - rcu_assign_pointer(wg->creating_net, src_net); + rcu_assign_pointer(wg->creating_net, rtnl_link_netns(nets)); init_rwsem(&wg->static_identity.lock); mutex_init(&wg->socket_update_lock); mutex_init(&wg->device_update_lock); diff --git a/drivers/net/wireless/virtual/virt_wifi.c b/drivers/net/wireless/virtual/virt_wifi.c index 4ee374080466..3fea244010e8 100644 --- a/drivers/net/wireless/virtual/virt_wifi.c +++ b/drivers/net/wireless/virtual/virt_wifi.c @@ -519,7 +519,8 @@ static rx_handler_result_t virt_wifi_rx_handler(struct sk_buff **pskb) } /* Called with rtnl lock held. */ -static int virt_wifi_newlink(struct net *src_net, struct net_device *dev, +static int virt_wifi_newlink(struct rtnl_link_nets *nets, + struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { @@ -532,7 +533,7 @@ static int virt_wifi_newlink(struct net *src_net, struct net_device *dev, netif_carrier_off(dev); priv->upperdev = dev; - priv->lowerdev = __dev_get_by_index(src_net, + priv->lowerdev = __dev_get_by_index(rtnl_link_netns(nets), nla_get_u32(tb[IFLA_LINK])); if (!priv->lowerdev) diff --git a/drivers/net/wwan/wwan_core.c b/drivers/net/wwan/wwan_core.c index a51e2755991a..f208993e04c0 100644 --- a/drivers/net/wwan/wwan_core.c +++ b/drivers/net/wwan/wwan_core.c @@ -967,7 +967,8 @@ static struct net_device *wwan_rtnl_alloc(struct nlattr *tb[], return dev; } -static int wwan_rtnl_newlink(struct net *src_net, struct net_device *dev, +static int wwan_rtnl_newlink(struct rtnl_link_nets *nets, + struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { @@ -1064,6 +1065,7 @@ static void wwan_create_default_link(struct wwan_device *wwandev, struct net_device *dev; struct nlmsghdr *nlh; struct sk_buff *msg; + struct rtnl_link_nets nets = { .src_net = &init_net }; /* Forge attributes required to create a WWAN netdev. We first * build a netlink message and then parse it. This looks @@ -1105,7 +1107,7 @@ static void wwan_create_default_link(struct wwan_device *wwandev, if (WARN_ON(IS_ERR(dev))) goto unlock; - if (WARN_ON(wwan_rtnl_newlink(&init_net, dev, tb, data, NULL))) { + if (WARN_ON(wwan_rtnl_newlink(&nets, dev, tb, data, NULL))) { free_netdev(dev); goto unlock; } diff --git a/include/net/ip_tunnels.h b/include/net/ip_tunnels.h index 1aa31bdb2b31..ae1f2dda4533 100644 --- a/include/net/ip_tunnels.h +++ b/include/net/ip_tunnels.h @@ -406,8 +406,9 @@ int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb, bool log_ecn_error); int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[], struct ip_tunnel_parm_kern *p, __u32 fwmark); -int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[], - struct ip_tunnel_parm_kern *p, __u32 fwmark); +int ip_tunnel_newlink(struct net *net, struct net_device *dev, + struct nlattr *tb[], struct ip_tunnel_parm_kern *p, + __u32 fwmark); void ip_tunnel_setup(struct net_device *dev, unsigned int net_id); bool ip_tunnel_netlink_encap_parms(struct nlattr *data[], diff --git a/include/net/rtnetlink.h b/include/net/rtnetlink.h index bc0069a8b6ea..f285071a24d4 100644 --- a/include/net/rtnetlink.h +++ b/include/net/rtnetlink.h @@ -69,6 +69,26 @@ static inline int rtnl_msg_family(const struct nlmsghdr *nlh) return AF_UNSPEC; } +/** + * struct rtnl_link_nets - net namespace context of newlink. + * + * @src_net: Source netns of rtnetlink socket + * @link_net: Link netns by IFLA_LINK_NETNSID, NULL if not specified. + */ +struct rtnl_link_nets { + struct net *src_net; + struct net *link_net; +}; + +/* Get effective link netns from struct rtnl_link_nets. Generally, this is + * link_net and falls back to src_net. But for compatibility, a driver may + * choose to use dev_net(dev) instead. + */ +static inline struct net *rtnl_link_netns(struct rtnl_link_nets *nets) +{ + return nets->link_net ? : nets->src_net; +} + /** * struct rtnl_link_ops - rtnetlink link operations * @@ -125,7 +145,7 @@ struct rtnl_link_ops { struct nlattr *data[], struct netlink_ext_ack *extack); - int (*newlink)(struct net *src_net, + int (*newlink)(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], diff --git a/net/8021q/vlan_netlink.c b/net/8021q/vlan_netlink.c index 134419667d59..fe62c7c71e7f 100644 --- a/net/8021q/vlan_netlink.c +++ b/net/8021q/vlan_netlink.c @@ -135,7 +135,7 @@ static int vlan_changelink(struct net_device *dev, struct nlattr *tb[], return 0; } -static int vlan_newlink(struct net *src_net, struct net_device *dev, +static int vlan_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { @@ -155,7 +155,8 @@ static int vlan_newlink(struct net *src_net, struct net_device *dev, return -EINVAL; } - real_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); + real_dev = __dev_get_by_index(rtnl_link_netns(nets), + nla_get_u32(tb[IFLA_LINK])); if (!real_dev) { NL_SET_ERR_MSG_MOD(extack, "link does not exist"); return -ENODEV; diff --git a/net/batman-adv/soft-interface.c b/net/batman-adv/soft-interface.c index 2758aba47a2f..d1c12072feaa 100644 --- a/net/batman-adv/soft-interface.c +++ b/net/batman-adv/soft-interface.c @@ -1063,7 +1063,7 @@ static int batadv_softif_validate(struct nlattr *tb[], struct nlattr *data[], /** * batadv_softif_newlink() - pre-initialize and register new batadv link - * @src_net: the applicable net namespace + * @nets: the applicable net namespaces * @dev: network device to register * @tb: IFLA_INFO_DATA netlink attributes * @data: enum batadv_ifla_attrs attributes @@ -1071,7 +1071,8 @@ static int batadv_softif_validate(struct nlattr *tb[], struct nlattr *data[], * * Return: 0 if successful or error otherwise. */ -static int batadv_softif_newlink(struct net *src_net, struct net_device *dev, +static int batadv_softif_newlink(struct rtnl_link_nets *nets, + struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { diff --git a/net/bridge/br_netlink.c b/net/bridge/br_netlink.c index 3e0f47203f2a..715abe3f8b89 100644 --- a/net/bridge/br_netlink.c +++ b/net/bridge/br_netlink.c @@ -1553,7 +1553,7 @@ static int br_changelink(struct net_device *brdev, struct nlattr *tb[], return 0; } -static int br_dev_newlink(struct net *src_net, struct net_device *dev, +static int br_dev_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { diff --git a/net/caif/chnl_net.c b/net/caif/chnl_net.c index 94ad09e36df2..a805f7552ac9 100644 --- a/net/caif/chnl_net.c +++ b/net/caif/chnl_net.c @@ -438,7 +438,7 @@ static void caif_netlink_parms(struct nlattr *data[], } } -static int ipcaif_newlink(struct net *src_net, struct net_device *dev, +static int ipcaif_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c index f573ace60234..93bc4ea8a62e 100644 --- a/net/core/rtnetlink.c +++ b/net/core/rtnetlink.c @@ -3750,6 +3750,10 @@ static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm, struct net_device *dev; char ifname[IFNAMSIZ]; int err; + struct rtnl_link_nets nets = { + .src_net = net, + .link_net = link_net, + }; if (!ops->alloc && !ops->setup) return -EOPNOTSUPP; @@ -3761,8 +3765,8 @@ static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm, name_assign_type = NET_NAME_ENUM; } - dev = rtnl_create_link(link_net ? : tgt_net, ifname, - name_assign_type, ops, tb, extack); + dev = rtnl_create_link(tgt_net, ifname, name_assign_type, ops, tb, + extack); if (IS_ERR(dev)) { err = PTR_ERR(dev); goto out; @@ -3771,7 +3775,7 @@ static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm, dev->ifindex = ifm->ifi_index; if (ops->newlink) - err = ops->newlink(link_net ? : net, dev, tb, data, extack); + err = ops->newlink(&nets, dev, tb, data, extack); else err = register_netdevice(dev); if (err < 0) { @@ -3782,11 +3786,6 @@ static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm, err = rtnl_configure_link(dev, ifm, portid, nlh); if (err < 0) goto out_unregister; - if (link_net) { - err = dev_change_net_namespace(dev, tgt_net, ifname); - if (err < 0) - goto out_unregister; - } if (tb[IFLA_MASTER]) { err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); if (err) diff --git a/net/hsr/hsr_netlink.c b/net/hsr/hsr_netlink.c index b68f2f71d0e1..efd42af79b94 100644 --- a/net/hsr/hsr_netlink.c +++ b/net/hsr/hsr_netlink.c @@ -29,7 +29,7 @@ static const struct nla_policy hsr_policy[IFLA_HSR_MAX + 1] = { /* Here, it seems a netdevice has already been allocated for us, and the * hsr_dev_setup routine has been executed. Nice! */ -static int hsr_newlink(struct net *src_net, struct net_device *dev, +static int hsr_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { @@ -46,7 +46,7 @@ static int hsr_newlink(struct net *src_net, struct net_device *dev, NL_SET_ERR_MSG_MOD(extack, "Slave1 device not specified"); return -EINVAL; } - link[0] = __dev_get_by_index(src_net, + link[0] = __dev_get_by_index(rtnl_link_netns(nets), nla_get_u32(data[IFLA_HSR_SLAVE1])); if (!link[0]) { NL_SET_ERR_MSG_MOD(extack, "Slave1 does not exist"); @@ -56,7 +56,7 @@ static int hsr_newlink(struct net *src_net, struct net_device *dev, NL_SET_ERR_MSG_MOD(extack, "Slave2 device not specified"); return -EINVAL; } - link[1] = __dev_get_by_index(src_net, + link[1] = __dev_get_by_index(rtnl_link_netns(nets), nla_get_u32(data[IFLA_HSR_SLAVE2])); if (!link[1]) { NL_SET_ERR_MSG_MOD(extack, "Slave2 does not exist"); @@ -69,7 +69,7 @@ static int hsr_newlink(struct net *src_net, struct net_device *dev, } if (data[IFLA_HSR_INTERLINK]) - interlink = __dev_get_by_index(src_net, + interlink = __dev_get_by_index(rtnl_link_netns(nets), nla_get_u32(data[IFLA_HSR_INTERLINK])); if (interlink && interlink == link[0]) { diff --git a/net/ieee802154/6lowpan/core.c b/net/ieee802154/6lowpan/core.c index 175efd860f7b..fd661fec3975 100644 --- a/net/ieee802154/6lowpan/core.c +++ b/net/ieee802154/6lowpan/core.c @@ -129,7 +129,7 @@ static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[], return 0; } -static int lowpan_newlink(struct net *src_net, struct net_device *ldev, +static int lowpan_newlink(struct rtnl_link_nets *nets, struct net_device *ldev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { @@ -143,7 +143,8 @@ static int lowpan_newlink(struct net *src_net, struct net_device *ldev, if (!tb[IFLA_LINK]) return -EINVAL; /* find and hold wpan device */ - wdev = dev_get_by_index(dev_net(ldev), nla_get_u32(tb[IFLA_LINK])); + wdev = dev_get_by_index(nets->link_net ? : dev_net(ldev), + nla_get_u32(tb[IFLA_LINK])); if (!wdev) return -ENODEV; if (wdev->type != ARPHRD_IEEE802154) { diff --git a/net/ipv4/ip_gre.c b/net/ipv4/ip_gre.c index f1f31ebfc793..6fc344f5005c 100644 --- a/net/ipv4/ip_gre.c +++ b/net/ipv4/ip_gre.c @@ -1389,10 +1389,11 @@ ipgre_newlink_encap_setup(struct net_device *dev, struct nlattr *data[]) return 0; } -static int ipgre_newlink(struct net *src_net, struct net_device *dev, +static int ipgre_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { + struct net *net = nets->link_net ? : dev_net(dev); struct ip_tunnel_parm_kern p; __u32 fwmark = 0; int err; @@ -1404,13 +1405,14 @@ static int ipgre_newlink(struct net *src_net, struct net_device *dev, err = ipgre_netlink_parms(dev, data, tb, &p, &fwmark); if (err < 0) return err; - return ip_tunnel_newlink(dev, tb, &p, fwmark); + return ip_tunnel_newlink(net, dev, tb, &p, fwmark); } -static int erspan_newlink(struct net *src_net, struct net_device *dev, +static int erspan_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { + struct net *net = nets->link_net ? : dev_net(dev); struct ip_tunnel_parm_kern p; __u32 fwmark = 0; int err; @@ -1422,7 +1424,7 @@ static int erspan_newlink(struct net *src_net, struct net_device *dev, err = erspan_netlink_parms(dev, data, tb, &p, &fwmark); if (err) return err; - return ip_tunnel_newlink(dev, tb, &p, fwmark); + return ip_tunnel_newlink(net, dev, tb, &p, fwmark); } static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[], @@ -1695,6 +1697,7 @@ struct net_device *gretap_fb_dev_create(struct net *net, const char *name, LIST_HEAD(list_kill); struct ip_tunnel *t; int err; + struct rtnl_link_nets nets = { .src_net = net }; memset(&tb, 0, sizeof(tb)); @@ -1707,7 +1710,7 @@ struct net_device *gretap_fb_dev_create(struct net *net, const char *name, t = netdev_priv(dev); t->collect_md = true; - err = ipgre_newlink(net, dev, tb, NULL, NULL); + err = ipgre_newlink(&nets, dev, tb, NULL, NULL); if (err < 0) { free_netdev(dev); return ERR_PTR(err); diff --git a/net/ipv4/ip_tunnel.c b/net/ipv4/ip_tunnel.c index 09b73acf037a..618a50d5c0c2 100644 --- a/net/ipv4/ip_tunnel.c +++ b/net/ipv4/ip_tunnel.c @@ -1213,11 +1213,11 @@ void ip_tunnel_delete_nets(struct list_head *net_list, unsigned int id, } EXPORT_SYMBOL_GPL(ip_tunnel_delete_nets); -int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[], - struct ip_tunnel_parm_kern *p, __u32 fwmark) +int ip_tunnel_newlink(struct net *net, struct net_device *dev, + struct nlattr *tb[], struct ip_tunnel_parm_kern *p, + __u32 fwmark) { struct ip_tunnel *nt; - struct net *net = dev_net(dev); struct ip_tunnel_net *itn; int mtu; int err; @@ -1326,7 +1326,9 @@ int ip_tunnel_init(struct net_device *dev) } tunnel->dev = dev; - tunnel->net = dev_net(dev); + if (!tunnel->net) + tunnel->net = dev_net(dev); + strscpy(tunnel->parms.name, dev->name); iph->version = 4; iph->ihl = 5; diff --git a/net/ipv4/ip_vti.c b/net/ipv4/ip_vti.c index f0b4419cef34..c315d60e02ec 100644 --- a/net/ipv4/ip_vti.c +++ b/net/ipv4/ip_vti.c @@ -575,7 +575,7 @@ static void vti_netlink_parms(struct nlattr *data[], *fwmark = nla_get_u32(data[IFLA_VTI_FWMARK]); } -static int vti_newlink(struct net *src_net, struct net_device *dev, +static int vti_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { @@ -583,7 +583,8 @@ static int vti_newlink(struct net *src_net, struct net_device *dev, __u32 fwmark = 0; vti_netlink_parms(data, &parms, &fwmark); - return ip_tunnel_newlink(dev, tb, &parms, fwmark); + return ip_tunnel_newlink(nets->link_net ? : dev_net(dev), dev, tb, + &parms, fwmark); } static int vti_changelink(struct net_device *dev, struct nlattr *tb[], diff --git a/net/ipv4/ipip.c b/net/ipv4/ipip.c index dc0db5895e0e..7d6619d7ab3e 100644 --- a/net/ipv4/ipip.c +++ b/net/ipv4/ipip.c @@ -436,7 +436,7 @@ static void ipip_netlink_parms(struct nlattr *data[], *fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]); } -static int ipip_newlink(struct net *src_net, struct net_device *dev, +static int ipip_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { @@ -453,7 +453,8 @@ static int ipip_newlink(struct net *src_net, struct net_device *dev, } ipip_netlink_parms(data, &p, &t->collect_md, &fwmark); - return ip_tunnel_newlink(dev, tb, &p, fwmark); + return ip_tunnel_newlink(nets->link_net ? : dev_net(dev), dev, tb, &p, + fwmark); } static int ipip_changelink(struct net_device *dev, struct nlattr *tb[], diff --git a/net/ipv6/ip6_gre.c b/net/ipv6/ip6_gre.c index 235808cfec70..eb2909689002 100644 --- a/net/ipv6/ip6_gre.c +++ b/net/ipv6/ip6_gre.c @@ -1971,7 +1971,7 @@ static bool ip6gre_netlink_encap_parms(struct nlattr *data[], return ret; } -static int ip6gre_newlink_common(struct net *src_net, struct net_device *dev, +static int ip6gre_newlink_common(struct net *link_net, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { @@ -1992,7 +1992,7 @@ static int ip6gre_newlink_common(struct net *src_net, struct net_device *dev, eth_hw_addr_random(dev); nt->dev = dev; - nt->net = dev_net(dev); + nt->net = link_net; err = register_netdevice(dev); if (err) @@ -2005,12 +2005,12 @@ static int ip6gre_newlink_common(struct net *src_net, struct net_device *dev, return err; } -static int ip6gre_newlink(struct net *src_net, struct net_device *dev, +static int ip6gre_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { struct ip6_tnl *nt = netdev_priv(dev); - struct net *net = dev_net(dev); + struct net *net = nets->link_net ? : dev_net(dev); struct ip6gre_net *ign; int err; @@ -2025,7 +2025,7 @@ static int ip6gre_newlink(struct net *src_net, struct net_device *dev, return -EEXIST; } - err = ip6gre_newlink_common(src_net, dev, tb, data, extack); + err = ip6gre_newlink_common(net, dev, tb, data, extack); if (!err) { ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]); ip6gre_tunnel_link_md(ign, nt); @@ -2241,12 +2241,13 @@ static void ip6erspan_tap_setup(struct net_device *dev) netif_keep_dst(dev); } -static int ip6erspan_newlink(struct net *src_net, struct net_device *dev, +static int ip6erspan_newlink(struct rtnl_link_nets *nets, + struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { struct ip6_tnl *nt = netdev_priv(dev); - struct net *net = dev_net(dev); + struct net *net = nets->link_net ? : dev_net(dev); struct ip6gre_net *ign; int err; @@ -2262,7 +2263,7 @@ static int ip6erspan_newlink(struct net *src_net, struct net_device *dev, return -EEXIST; } - err = ip6gre_newlink_common(src_net, dev, tb, data, extack); + err = ip6gre_newlink_common(net, dev, tb, data, extack); if (!err) { ip6erspan_tnl_link_config(nt, !tb[IFLA_MTU]); ip6erspan_tunnel_link_md(ign, nt); diff --git a/net/ipv6/ip6_tunnel.c b/net/ipv6/ip6_tunnel.c index 48fd53b98972..994357690d52 100644 --- a/net/ipv6/ip6_tunnel.c +++ b/net/ipv6/ip6_tunnel.c @@ -250,10 +250,9 @@ static void ip6_dev_free(struct net_device *dev) dst_cache_destroy(&t->dst_cache); } -static int ip6_tnl_create2(struct net_device *dev) +static int ip6_tnl_create2(struct net *net, struct net_device *dev) { struct ip6_tnl *t = netdev_priv(dev); - struct net *net = dev_net(dev); struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); int err; @@ -308,7 +307,7 @@ static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p) t = netdev_priv(dev); t->parms = *p; t->net = dev_net(dev); - err = ip6_tnl_create2(dev); + err = ip6_tnl_create2(net, dev); if (err < 0) goto failed_free; @@ -2002,11 +2001,11 @@ static void ip6_tnl_netlink_parms(struct nlattr *data[], parms->fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]); } -static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev, +static int ip6_tnl_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { - struct net *net = dev_net(dev); + struct net *net = nets->link_net ? : dev_net(dev); struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); struct ip_tunnel_encap ipencap; struct ip6_tnl *nt, *t; @@ -2031,7 +2030,7 @@ static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev, return -EEXIST; } - err = ip6_tnl_create2(dev); + err = ip6_tnl_create2(net, dev); if (!err && tb[IFLA_MTU]) ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU])); diff --git a/net/ipv6/ip6_vti.c b/net/ipv6/ip6_vti.c index 590737c27537..a329ee728331 100644 --- a/net/ipv6/ip6_vti.c +++ b/net/ipv6/ip6_vti.c @@ -174,10 +174,9 @@ vti6_tnl_unlink(struct vti6_net *ip6n, struct ip6_tnl *t) } } -static int vti6_tnl_create2(struct net_device *dev) +static int vti6_tnl_create2(struct net *net, struct net_device *dev) { struct ip6_tnl *t = netdev_priv(dev); - struct net *net = dev_net(dev); struct vti6_net *ip6n = net_generic(net, vti6_net_id); int err; @@ -221,7 +220,7 @@ static struct ip6_tnl *vti6_tnl_create(struct net *net, struct __ip6_tnl_parm *p t->parms = *p; t->net = dev_net(dev); - err = vti6_tnl_create2(dev); + err = vti6_tnl_create2(net, dev); if (err < 0) goto failed_free; @@ -997,11 +996,11 @@ static void vti6_netlink_parms(struct nlattr *data[], parms->fwmark = nla_get_u32(data[IFLA_VTI_FWMARK]); } -static int vti6_newlink(struct net *src_net, struct net_device *dev, +static int vti6_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { - struct net *net = dev_net(dev); + struct net *net = nets->link_net ? : dev_net(dev); struct ip6_tnl *nt; nt = netdev_priv(dev); @@ -1012,7 +1011,7 @@ static int vti6_newlink(struct net *src_net, struct net_device *dev, if (vti6_locate(net, &nt->parms, 0)) return -EEXIST; - return vti6_tnl_create2(dev); + return vti6_tnl_create2(net, dev); } static void vti6_dellink(struct net_device *dev, struct list_head *head) diff --git a/net/ipv6/sit.c b/net/ipv6/sit.c index 39bd8951bfca..6d7e4be325e7 100644 --- a/net/ipv6/sit.c +++ b/net/ipv6/sit.c @@ -198,10 +198,9 @@ static void ipip6_tunnel_clone_6rd(struct net_device *dev, struct sit_net *sitn) #endif } -static int ipip6_tunnel_create(struct net_device *dev) +static int ipip6_tunnel_create(struct net *net, struct net_device *dev) { struct ip_tunnel *t = netdev_priv(dev); - struct net *net = dev_net(dev); struct sit_net *sitn = net_generic(net, sit_net_id); int err; @@ -270,7 +269,7 @@ static struct ip_tunnel *ipip6_tunnel_locate(struct net *net, nt = netdev_priv(dev); nt->parms = *parms; - if (ipip6_tunnel_create(dev) < 0) + if (ipip6_tunnel_create(net, dev) < 0) goto failed_free; if (!parms->name[0]) @@ -1550,11 +1549,11 @@ static bool ipip6_netlink_6rd_parms(struct nlattr *data[], } #endif -static int ipip6_newlink(struct net *src_net, struct net_device *dev, +static int ipip6_newlink(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { - struct net *net = dev_net(dev); + struct net *net = nets->link_net ? : dev_net(dev); struct ip_tunnel *nt; struct ip_tunnel_encap ipencap; #ifdef CONFIG_IPV6_SIT_6RD @@ -1575,7 +1574,7 @@ static int ipip6_newlink(struct net *src_net, struct net_device *dev, if (ipip6_tunnel_locate(net, &nt->parms, 0)) return -EEXIST; - err = ipip6_tunnel_create(dev); + err = ipip6_tunnel_create(net, dev); if (err < 0) return err; diff --git a/net/xfrm/xfrm_interface_core.c b/net/xfrm/xfrm_interface_core.c index 98f1e2b67c76..c072de6db133 100644 --- a/net/xfrm/xfrm_interface_core.c +++ b/net/xfrm/xfrm_interface_core.c @@ -242,10 +242,9 @@ static void xfrmi_dev_free(struct net_device *dev) gro_cells_destroy(&xi->gro_cells); } -static int xfrmi_create(struct net_device *dev) +static int xfrmi_create(struct net *net, struct net_device *dev) { struct xfrm_if *xi = netdev_priv(dev); - struct net *net = dev_net(dev); struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id); int err; @@ -814,11 +813,11 @@ static void xfrmi_netlink_parms(struct nlattr *data[], parms->collect_md = true; } -static int xfrmi_newlink(struct net *src_net, struct net_device *dev, - struct nlattr *tb[], struct nlattr *data[], - struct netlink_ext_ack *extack) +static int xfrmi_newlink(struct rtnl_link_nets *nets, struct net_device *dev, + struct nlattr *tb[], struct nlattr *data[], + struct netlink_ext_ack *extack) { - struct net *net = dev_net(dev); + struct net *net = nets->link_net ? : dev_net(dev); struct xfrm_if_parms p = {}; struct xfrm_if *xi; int err; @@ -851,7 +850,7 @@ static int xfrmi_newlink(struct net *src_net, struct net_device *dev, xi->net = net; xi->dev = dev; - err = xfrmi_create(dev); + err = xfrmi_create(net, dev); return err; } From patchwork Wed Nov 13 12:57:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiao Liang X-Patchwork-Id: 18682 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 6362A8358B for ; Wed, 13 Nov 2024 14:05:05 +0100 (CET) ARC-Seal: i=2; cv=pass; a=rsa-sha256; d=open-mesh.org; s=20121; t=1731503105; b=2Xwu04VynRqc2Lu6p74fpzTJvnhB0b1MD/vWvgNdEydbm785wbELW6CQrF3+3JtZ2bRsH sY8aCEbEsi2FUafvcEPXbwFOCxukB9oVqmyPQruya9pmNecL1GEOS4encULjSCJs9TZ9FxE zwBxGzAKhGF1rnbEMpkahnL1tgQb1rA= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1731503105; 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=dFTsOSdSHxPXXC/kmCtPA19UOd1z5UErpUP4tQUDFm4=; b=2fHEbH9JXfKVEfMAZRegcR8E16w5JtlHNwvbC/VBukiisAHgmCdrUXJ6+PczJQcXyWfOC 2adu8nnrDBMZSXMsfU3Mpi2MLV7XEq+9D7ErFsV3CXmOZNNPvzD6F2e7HLS/GQ0I9Ke1z7f kLVKcD0s2+ZbvWOhUA7lCXbussaWF80= 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-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by diktynna.open-mesh.org (Postfix) with ESMTPS id 5AA9C83324 for ; Wed, 13 Nov 2024 13:58:20 +0100 (CET) ARC-Seal: i=1; s=20121; d=open-mesh.org; t=1731502700; a=rsa-sha256; cv=none; b=wqnsAfB5qP8rqvhxLu4heCfxb30vlyaC3e2jvhh3quprqbjezl9nMePdmvUZieBm5++TOp 9tYyY0PUPNJzYPeZeZpwfEBreRKN2x8sTNC+a3iXRi6oTpAZjCXeVSgoBSMSxETkJ21Nu6 9NhhZy9hXYDga2vip9VZwuEyW8Y1lcU= ARC-Authentication-Results: i=1; diktynna.open-mesh.org; dkim=pass header.d=gmail.com header.s=20230601 header.b=cVW4AJpG; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (diktynna.open-mesh.org: domain of shaw.leon@gmail.com designates 2607:f8b0:4864:20::1031 as permitted sender) smtp.mailfrom=shaw.leon@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1731502700; 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=dFTsOSdSHxPXXC/kmCtPA19UOd1z5UErpUP4tQUDFm4=; b=T2TfZWCsiQQtKMRIYTk/eZScyQe62qjfePMCGN1kKSGBXUBW6OtG0hVPCSxwPBupmMvBTj Y9Rb0S8AodD+5SzVRwcnQtw4gPa2cnYmwkKy4inOIsDSNCp5+iWcRRc/Nte477tb34Cs0F mtGlnw57SjCKntO2ueJoOW3/zL9MqZ4= Received: by mail-pj1-x1031.google.com with SMTP id 98e67ed59e1d1-2e2b549799eso5439563a91.3 for ; Wed, 13 Nov 2024 04:58:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731502699; x=1732107499; 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=dFTsOSdSHxPXXC/kmCtPA19UOd1z5UErpUP4tQUDFm4=; b=cVW4AJpG0FRX00wwAbBO5miqps5iTLEC5/Vs4HWPonYJ7HLTFB63uhKYdd1qw7gFZu NK9cMykciQxl8vkWvOzX2qhzxAsoWUw+ss8xGoVAZAJI+blid9rDxlwENGOfFy4mKn+l UrWKcm3Bi02JJSuCajqNkQgN2SsAeh4DsyfglDQEzgpt0ZM30lvOpY0S38y2up3a4Ukk eYvO5zXo2mtTgpszfWI/DdtEtT1HoPTCPQ1GyIWK+kb5Nky+JNDLQFkuu6DMF/ZMnorm WgKcyIFK5/vZHE46MrfGvdZ/Yw1u5BMyC7pLIziTFVa9n6N7MUOqkUHFUge9JNHnFBjt b6+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731502699; x=1732107499; 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=dFTsOSdSHxPXXC/kmCtPA19UOd1z5UErpUP4tQUDFm4=; b=XwigVkUlJO3Up7OO5JNSjB0rD/8Kmk9Do8tgKmJkHzQYLZi15T23EsYttcStWa+ZGM P9XAI7yBm+KradWfHhlMuWA8twc9w4TPI9VXfCuoSLCmgsadG8qsvYyMo5POu+rywPKA xQbox+guCtnHiTGVbYJb35Nr2QwGsnZQeQOkjvOZd3vM4vwD4geohPXTT0ShosBWL+mG 25NoI96tYBZwk9zz0Uj1tOM5wbrymX5yl7bmcC8/uFh3cFuvE/t5c7c1JezT0/lQ/sPe RzeiuHXLL8oA06EzQJCvDHdBl20oB+lgjsflbd3KTENAzldKJRFo74RmcO3tqlBkON+S qx4Q== X-Forwarded-Encrypted: i=1; AJvYcCXd1U0nyCdC2ySmPKq3lYEjFLFxnXiqc7n1RbP4DIYGB+AT1R4p6olBTB2CiBww2R9tt6yVkMCrhh/C4w==@lists.open-mesh.org X-Gm-Message-State: AOJu0YxrTmuXFi+jSRJoNAmd+B0CIyCl+Gr9EMsVmIV+NKBCUxbM53Ql J0oyEeMQvbPTm//cPE7F56SLLdAMcfmaKePoGF7UkWc//N2zfyFw X-Google-Smtp-Source: AGHT+IG3aP3jfG+H+mqTGfWaQF6eC/hcm8KAxQSToRubUMwTZod3v43r/5qYZbnllxo9jc+4RsAB2Q== X-Received: by 2002:a17:90b:1810:b0:2e2:cd5e:7eef with SMTP id 98e67ed59e1d1-2e9b1773411mr26583707a91.27.1731502698852; Wed, 13 Nov 2024 04:58:18 -0800 (PST) Received: from nova-ws.. ([103.167.140.11]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9f3f8ed0esm1398632a91.40.2024.11.13.04.58.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Nov 2024 04:58:18 -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 v3 5/6] selftests: net: Add python context manager for netns entering Date: Wed, 13 Nov 2024 20:57:14 +0800 Message-ID: <20241113125715.150201-6-shaw.leon@gmail.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241113125715.150201-1-shaw.leon@gmail.com> References: <20241113125715.150201-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: IWAQHIVSK3Y3GYHZIVR4FFIQQNUVAYHX X-Message-ID-Hash: IWAQHIVSK3Y3GYHZIVR4FFIQQNUVAYHX X-Mailman-Approved-At: Wed, 13 Nov 2024 14:00:42 +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() From patchwork Wed Nov 13 12:57:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiao Liang X-Patchwork-Id: 18683 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 9EB69840CE for ; Wed, 13 Nov 2024 14:06:12 +0100 (CET) ARC-Seal: i=2; cv=pass; a=rsa-sha256; d=open-mesh.org; s=20121; t=1731503172; b=21reG10JK2wXCF6+IaF/U8XhODXrEYVOsZEDyU72+THFohYcM06syvBlL0w+Bwv3+Jomd wCBFXJ2ovmVHUswwnq3BvulUU00UogecMNCvujrXnllea0MVwiDFBxUbjE1MyFhBjMZpDoA hgA1VK0xeu2ksmmvnRsLr43BgMeX3Os= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1731503172; 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=tqrrLk6+C692qIcVQOp9KatlJAZHjGryjisTBrwbzoI=; b=AeVROxn74NJT67CKAYm9Y/eRRoYCqfot/e2KeTLX8UxKzO8uAqM1kH7o86KkLJGJeqGHk T6t67kJPGZtURaxdWIfH7rdhtd57yAI+/4Ad2D0pp8HNyImrjWUQKH2rt7JRWz0z4avadCc J1lBkyNuaGLvgYITYaVzL3+81IrEnkk= 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-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by diktynna.open-mesh.org (Postfix) with ESMTPS id C4F708357C for ; Wed, 13 Nov 2024 13:58:28 +0100 (CET) ARC-Seal: i=1; s=20121; d=open-mesh.org; t=1731502708; a=rsa-sha256; cv=none; b=vRrXCqWJCqU7hTF+yGEhuH5rNHaQpxfGPNt+rTbNUQywFTUoojfCX2x4IvhC5b9Oktq77U urzodTr20k/63vuyiY2yrlJSi3bNe0UJPuHigC8R9RHNWcT5ZF2VCRyP/X3KPFWWBI1pdm 2BAh8wtC8ZvmnWKOfqPEmh9JZs9ACpQ= ARC-Authentication-Results: i=1; diktynna.open-mesh.org; dkim=pass header.d=gmail.com header.s=20230601 header.b=K9XLUMzT; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (diktynna.open-mesh.org: domain of shaw.leon@gmail.com designates 2607:f8b0:4864:20::1029 as permitted sender) smtp.mailfrom=shaw.leon@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1731502708; 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=tqrrLk6+C692qIcVQOp9KatlJAZHjGryjisTBrwbzoI=; b=n1NxZFLQJsRlJWSdzjhFtFwiVkNjKprIpfbriNWQ2p/q0goj3Ql/wy8Pv6gj2w0w+nV4ws bGyR4GC1yzUFqkqU03VFYBZEuv3WHGtb9Ua6HfsYuBSlqCxpXyO97TuI9eBCt1vfplMp4v 0hSb7Mbqlm557YrBDhY0Blo5PYo7ReI= Received: by mail-pj1-x1029.google.com with SMTP id 98e67ed59e1d1-2e2eb9dde40so5533428a91.0 for ; Wed, 13 Nov 2024 04:58:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731502707; x=1732107507; 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=tqrrLk6+C692qIcVQOp9KatlJAZHjGryjisTBrwbzoI=; b=K9XLUMzTXsj4ZgXcgDNzqpW2DTxsdViWCH2h9KVXlmBLmhK23A81pivW1+h+embJjZ F9liJtVqUl1WzJl9n61TEr5Xbr/qIAK0SavKGjVB0j93ABhKpDzMn8XvFbMpXey/WryP pSFBTHtJqigRuhxPqbxRmzJG4cziHY7rZNlNm3YjzJ/7DI6FzKts51MVdnuqq8V/4Pg9 ddfIhNQQ0BbDP0Owf+ECi7GrBYkNCCx9+EE3lCCUSICzdUfJpWgkNrIkrFUMxzbeF1BL krmZQq4HXCGhNjq2qv5A4VdkQkk/iP86PWv5NA+M7qYgIJXong/m7QtNnOgWr9tzq2zm pXPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731502707; x=1732107507; 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=tqrrLk6+C692qIcVQOp9KatlJAZHjGryjisTBrwbzoI=; b=Euxltf/3XaFa+0FeW49RGv4FsOjpSGJljiiT2xo8vxj9ZtLocwhz/smbmyoOxAy3CP 7xeFKjLN8m0OwtpxKKOl4qos53XKfcg8Hvs8dNdEOfmjLZI4wX1w/9xrwzJHbR1zTbtG /A96wowBfhncfNy/BDBsotS4nE9W9lLpFZ4axtZ4baHsMtc+illlG3JxtLY6i0tVkf/E GCfqsnmcu1xBVmQSevIb+lxGVquiHA2VPJcHzZG5gkTW9XP9buSsyE8mnz6PvXz1/Cqw iX0xBLA/BByAltS4xSJ4F/LvHh2gzh7Q2IO0rYLY4cywk6yXaMnpiDrx0wQIXJjYhnMA MIVg== X-Forwarded-Encrypted: i=1; AJvYcCVpdp48FywFde73ypzZejEz/bB9KQNKPlmUQJf6P5p5ifIOdVcbKE+VN8SJgMU9ZSztSYVMkoEFUJq9GA==@lists.open-mesh.org X-Gm-Message-State: AOJu0YwPpFbnYJ/iOjSVzha5JkBvHAtjlxkcOuo4YKyFtBrnpxQqVJEn AWd33r80mKwsvtHXO1kkHIFK7JfYeaC+ETLLbEa8MpzaZK+Gvvpl X-Google-Smtp-Source: AGHT+IE/vtXg73mPc9UwMocIh3JnpJ4Nx9KNFh/J++F7E0J+l3pL2CaZbF6bhZwKtLiUviIv9q/4Rw== X-Received: by 2002:a17:90b:3c12:b0:2e2:b2ce:e41e with SMTP id 98e67ed59e1d1-2e9f2c78421mr3106773a91.13.1731502707231; Wed, 13 Nov 2024 04:58:27 -0800 (PST) Received: from nova-ws.. ([103.167.140.11]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9f3f8ed0esm1398632a91.40.2024.11.13.04.58.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Nov 2024 04:58:26 -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 v3 6/6] selftests: net: Add two test cases for link netns Date: Wed, 13 Nov 2024 20:57:15 +0800 Message-ID: <20241113125715.150201-7-shaw.leon@gmail.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241113125715.150201-1-shaw.leon@gmail.com> References: <20241113125715.150201-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: EW6CZU6Y26EOJMAWL5UKD4YF3LYG75PQ X-Message-ID-Hash: EW6CZU6Y26EOJMAWL5UKD4YF3LYG75PQ X-Mailman-Approved-At: Wed, 13 Nov 2024 14:00:42 +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: - Add test for creating link in another netns when a link of the same name and ifindex exists in current netns. - Add test for link netns atomicity - create link directly in target netns, and no notifications should be generated in current netns. Signed-off-by: Xiao Liang --- tools/testing/selftests/net/Makefile | 1 + tools/testing/selftests/net/netns-name.sh | 10 ++++++ tools/testing/selftests/net/netns_atomic.py | 38 +++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100755 tools/testing/selftests/net/netns_atomic.py diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile index 2b2a5ec7fa6a..4c15a115c251 100644 --- a/tools/testing/selftests/net/Makefile +++ b/tools/testing/selftests/net/Makefile @@ -34,6 +34,7 @@ TEST_PROGS += gre_gso.sh TEST_PROGS += cmsg_so_mark.sh TEST_PROGS += cmsg_time.sh cmsg_ipv6.sh TEST_PROGS += netns-name.sh +TEST_PROGS += netns_atomic.py TEST_PROGS += nl_netdev.py TEST_PROGS += srv6_end_dt46_l3vpn_test.sh TEST_PROGS += srv6_end_dt4_l3vpn_test.sh diff --git a/tools/testing/selftests/net/netns-name.sh b/tools/testing/selftests/net/netns-name.sh index 6974474c26f3..0be1905d1f2f 100755 --- a/tools/testing/selftests/net/netns-name.sh +++ b/tools/testing/selftests/net/netns-name.sh @@ -78,6 +78,16 @@ ip -netns $NS link show dev $ALT_NAME 2> /dev/null && fail "Can still find alt-name after move" ip -netns $test_ns link del $DEV || fail +# +# Test no conflict of the same name/ifindex in different netns +# +ip -netns $NS link add name $DEV index 100 type dummy || fail +ip -netns $NS link add netns $test_ns name $DEV index 100 type dummy || + fail "Can create in netns without moving" +ip -netns $test_ns link show dev $DEV >> /dev/null || fail "Device not found" +ip -netns $NS link del $DEV || fail +ip -netns $test_ns link del $DEV || fail + echo -ne "$(basename $0) \t\t\t\t" if [ $RET_CODE -eq 0 ]; then echo "[ OK ]" diff --git a/tools/testing/selftests/net/netns_atomic.py b/tools/testing/selftests/net/netns_atomic.py new file mode 100755 index 000000000000..e6c4147ef75e --- /dev/null +++ b/tools/testing/selftests/net/netns_atomic.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 + +import time + +from lib.py import ksft_run, ksft_exit, ksft_true +from lib.py import ip +from lib.py import NetNS, NetNSEnter +from lib.py import RtnlFamily + + +def test_event(ns1, ns2) -> None: + with NetNSEnter(str(ns1)): + rtnl = RtnlFamily() + + rtnl.ntf_subscribe("rtnlgrp-link") + + ip(f"netns set {ns1} 0", ns=str(ns2)) + + ip(f"link add netns {ns2} link-netnsid 0 dummy1 type dummy") + ip(f"link add netns {ns2} dummy2 type dummy", ns=str(ns1)) + + ip("link del dummy1", ns=str(ns2)) + ip("link del dummy2", ns=str(ns2)) + + time.sleep(1) + rtnl.check_ntf() + ksft_true(not rtnl.async_msg_queue, "Received unexpected link notification") + + +def main() -> None: + with NetNS() as ns1, NetNS() as ns2: + ksft_run([test_event], args=(ns1, ns2)) + ksft_exit() + + +if __name__ == "__main__": + main()