From nobody Tue Dec 2 02:43:45 2025 Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7572C31354C for ; Tue, 18 Nov 2025 02:00:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763431232; cv=none; b=oenxghh55i+qFUhxJW9B9zdYfN9cqSOF9alp/r1yFNlNTTuYP4UaU6oxJOGS5zQnif/Mk2fMWvE8OAFFNXVG9aSAfibPV3Wfo/EZaCS4m4noRM1LsPCvGxel3/A2phMtrKQb8S/u3WLW5LUL+y9h/C6SJO37pLyL8wjdmQqPHUU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763431232; c=relaxed/simple; bh=rxQ/1GJhCmCe2AFwaDeo7zZNAc/4zN5ruqTcN3iFTYA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=H1V6cXSDr2+jqPmaTRrfbB3kehYNkg8WFhUNbEE7o2J6Jdz49y6RzuzLTiW3tepWMSmPcIxwXwm2LvW4/6lKH3dvX0c5wFnAtY5MPsqqgCXiKMKCVp4RSEQ9j3ZHt/S6DlQB7yNVq2aZjHBxK/TiMGmuOGTID5S3RifJYsubdYI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=hkJ7Xr8p; arc=none smtp.client-ip=209.85.216.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hkJ7Xr8p" Received: by mail-pj1-f48.google.com with SMTP id 98e67ed59e1d1-34374febdefso5296978a91.0 for ; Mon, 17 Nov 2025 18:00:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763431230; x=1764036030; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=NqihfKJRHAENT+vtDuZaHGz1v+/r/9FmzH4WlFsBg7Q=; b=hkJ7Xr8pMisnltHjKWCskpOMVJmScPJV1TvUEwICznzrroDM/oe/A1hOdjekvuw/Ep v9d0pSVePS6dspYrvpF7rkxSr42Wb5pR5T3R79FzXzzi+0V4QDdEJ6yt7HO8W5oFFTr3 4bSJH5a/AmB/6uwdkSZhe8Dqb15C8V/VZ0MbwRbfeBFEp7L4e3EROtLOuE1D4yrHUIgL dxlXcYU8OeTLNRROAMoNm2CkOg94Qp+UiF9dBzPIhXLgedBG4IQtEGubG0mAfwKkkAOK HoSebBbMhIgTzjpl9+bKoVGJ5o1KmyxzPTDCh57ndVxfMIIsftBpTMaTzkIz0Ww6Oj6u DWnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763431230; x=1764036030; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=NqihfKJRHAENT+vtDuZaHGz1v+/r/9FmzH4WlFsBg7Q=; b=PV3W//x3tR4sf7iOGi0znL8nbTwlhuDJRJpxwrKy69rYfpZLqizDWxIkOkKSeVtfCI GuSoANsOlcz5iubwBwkstDGEBCBIXSN221y2RTrlsG+iqnPPICnSp3+3Kn1e36pkOHmJ zEsIAfbDrgDQOJ03RHyF9Zilwr2ePeU/U5Ooizg/KG8VQjUff7GtFeIpeB6Vk/6VGW5A +pONGXOW3oAh1+W6gQb+WtXj/ufVHnvMM7SrSufRUg26vwoKWBXCHMHor8JJczocbJT7 2giuTYAV5t/ks8n+g7gdqCbSjU9jKikilVy8/CJEMKfUdhBoSilx5ZBdxZzpxRLzQ0Bx fHew== X-Gm-Message-State: AOJu0YxzjXWfOawpKKxeerw6QLg1vBx3WFAWJawOFto5wljnDtninxpE b02K1adHVzMkjh38lrlZ++1S1/dJuBP4BllejEYD9uj0z/E5LLXBFPEZ X-Gm-Gg: ASbGncsb2Vuccfvyuev7RFClAZ6jgH5bxaHzwoKf12j0sEiC0n8W8yQVomh45JMcPxP CicpxsUXUhKgpv9EoVnKU6RYOeNDLa3OC/NtOzF71nWsRVY64k04nukoarqo0Yt2HMV/bb2hlni tr8hIiXdQq1CfbmYHkbqCQCxBGqjOJ7moLah+sAADtQ3RZbVBmEHdVjTUnyP/+cgzwPPcuQhrgL 3kDyV84XEMCS+dgR8JooaR7pXtVavcTr728lEJxpG1cpcmxA+384maZRSCOj8cvIleaWtCg+lLR P+Z19SDzIZRaUvjqNgbG8WRZdFcwKam3Q+5O9Mg/cMo6nl52MgW1lTX05i1spgppI+7mYwBIyHd DEBMXwapwwJ+zh2m8V/Bp0T2xZUnFMXyy02ByeYnXguI7NJIEbGRehx5gS2g1cV8dqt5cLDHKsx shpCNn5K0Vacb9B9wQmXSHYlJpYnhx+g== X-Google-Smtp-Source: AGHT+IEbZw6qRSUc9CAVQ8dKc8ULj7Y+j+jbzDDMbNJuCyLL/Q1LuCfTO0BI6N2AF9S1ioRpA4NGpw== X-Received: by 2002:a17:90b:3b45:b0:33e:1acc:1799 with SMTP id 98e67ed59e1d1-343f9edf47dmr16628668a91.14.1763431229421; Mon, 17 Nov 2025 18:00:29 -0800 (PST) Received: from localhost ([2a03:2880:2ff:4::]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3456513f0eesm11147308a91.7.2025.11.17.18.00.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Nov 2025 18:00:29 -0800 (PST) From: Bobby Eshleman Date: Mon, 17 Nov 2025 18:00:24 -0800 Subject: [PATCH net-next v10 01/11] vsock: a per-net vsock NS mode state Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251117-vsock-vmtest-v10-1-df08f165bf3e@meta.com> References: <20251117-vsock-vmtest-v10-0-df08f165bf3e@meta.com> In-Reply-To: <20251117-vsock-vmtest-v10-0-df08f165bf3e@meta.com> To: Stefano Garzarella , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Stefan Hajnoczi , "Michael S. Tsirkin" , Jason Wang , =?utf-8?q?Eugenio_P=C3=A9rez?= , Xuan Zhuo , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Bryan Tan , Vishnu Dasa , Broadcom internal kernel review list , Shuah Khan Cc: linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, netdev@vger.kernel.org, kvm@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kselftest@vger.kernel.org, Sargun Dhillon , Bobby Eshleman , berrange@redhat.com, Bobby Eshleman X-Mailer: b4 0.14.3 From: Bobby Eshleman Add the per-net vsock NS mode state. This only adds the structure for holding the mode and some of the functions for setting/getting and checking the mode, but does not integrate the functionality yet. A "net_mode" field is added to vsock_sock to store the mode of the namespace when the vsock_sock was created. In order to evaluate namespace mode rules we need to know both a) which namespace the endpoints are in, and b) what mode that namespace had when the endpoints were created. This allows us to handle the changing of modes from global to local *after* a socket has been created by remembering that the mode was global when the socket was created. If we were to use the current net's mode instead, then the lookup would fail and the socket would break. Signed-off-by: Bobby Eshleman Reviewed-by: Stefano Garzarella Suggested-by: Sargun Dhillon --- Changes in v10: - change mode_locked to int (Stefano) Changes in v9: - use xchg(), WRITE_ONCE(), READ_ONCE() for mode and mode_locked (Stefano) - clarify mode0/mode1 meaning in vsock_net_check_mode() comment - remove spin lock in net->vsock (not used anymore) - change mode from u8 to enum vsock_net_mode in vsock_net_write_mode() Changes in v7: - clarify vsock_net_check_mode() comments - change to `orig_net_mode =3D=3D VSOCK_NET_MODE_GLOBAL && orig_net_mode = =3D=3D vsk->orig_net_mode` - remove extraneous explanation of `orig_net_mode` - rename `written` to `mode_locked` - rename `vsock_hdr` to `sysctl_hdr` - change `orig_net_mode` to `net_mode` - make vsock_net_check_mode() more generic by taking just net pointers and modes, instead of a vsock_sock ptr, for reuse by transports (e.g., vhost_vsock) Changes in v6: - add orig_net_mode to store mode at creation time which will be used to avoid breakage when namespace changes mode during socket/VM lifespan Changes in v5: - use /proc/sys/net/vsock/ns_mode instead of /proc/net/vsock_ns_mode - change from net->vsock.ns_mode to net->vsock.mode - change vsock_net_set_mode() to vsock_net_write_mode() - vsock_net_write_mode() returns bool for write success to avoid need to use vsock_net_mode_can_set() - remove vsock_net_mode_can_set() --- MAINTAINERS | 1 + include/net/af_vsock.h | 44 +++++++++++++++++++++++++++++++++++++++++= +++ include/net/net_namespace.h | 4 ++++ include/net/netns/vsock.h | 17 +++++++++++++++++ 4 files changed, 66 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 37f4278db851..e1e0e2092d0c 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -27101,6 +27101,7 @@ L: netdev@vger.kernel.org S: Maintained F: drivers/vhost/vsock.c F: include/linux/virtio_vsock.h +F: include/net/netns/vsock.h F: include/uapi/linux/virtio_vsock.h F: net/vmw_vsock/virtio_transport.c F: net/vmw_vsock/virtio_transport_common.c diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h index d40e978126e3..9b5bdd083b6f 100644 --- a/include/net/af_vsock.h +++ b/include/net/af_vsock.h @@ -10,6 +10,7 @@ =20 #include #include +#include #include #include =20 @@ -65,6 +66,7 @@ struct vsock_sock { u32 peer_shutdown; bool sent_request; bool ignore_connecting_rst; + enum vsock_net_mode net_mode; =20 /* Protected by lock_sock(sk) */ u64 buffer_size; @@ -256,4 +258,46 @@ static inline bool vsock_msgzerocopy_allow(const struc= t vsock_transport *t) { return t->msgzerocopy_allow && t->msgzerocopy_allow(); } + +static inline enum vsock_net_mode vsock_net_mode(struct net *net) +{ + return READ_ONCE(net->vsock.mode); +} + +static inline bool vsock_net_write_mode(struct net *net, + enum vsock_net_mode mode) +{ + if (xchg(&net->vsock.mode_locked, 1)) + return false; + + WRITE_ONCE(net->vsock.mode, mode); + return true; +} + +/* Return true if two namespaces and modes pass the mode rules. Otherwise, + * return false. + * + * - ns0 and ns1 are the namespaces being checked. + * - mode0 and mode1 are the vsock namespace modes of ns0 and ns1 at the t= ime + * the vsock objects were created. + * + * Read more about modes in the comment header of net/vmw_vsock/af_vsock.c. + */ +static inline bool vsock_net_check_mode(struct net *ns0, + enum vsock_net_mode mode0, + struct net *ns1, + enum vsock_net_mode mode1) +{ + /* Any vsocks within the same network namespace are always reachable, + * regardless of the mode. + */ + if (net_eq(ns0, ns1)) + return true; + + /* + * If the network namespaces differ, vsocks are only reachable if both + * were created in VSOCK_NET_MODE_GLOBAL mode. + */ + return mode0 =3D=3D VSOCK_NET_MODE_GLOBAL && mode0 =3D=3D mode1; +} #endif /* __AF_VSOCK_H__ */ diff --git a/include/net/net_namespace.h b/include/net/net_namespace.h index cb664f6e3558..66d3de1d935f 100644 --- a/include/net/net_namespace.h +++ b/include/net/net_namespace.h @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -196,6 +197,9 @@ struct net { /* Move to a better place when the config guard is removed. */ struct mutex rtnl_mutex; #endif +#if IS_ENABLED(CONFIG_VSOCKETS) + struct netns_vsock vsock; +#endif } __randomize_layout; =20 #include diff --git a/include/net/netns/vsock.h b/include/net/netns/vsock.h new file mode 100644 index 000000000000..c1a5e805949d --- /dev/null +++ b/include/net/netns/vsock.h @@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __NET_NET_NAMESPACE_VSOCK_H +#define __NET_NET_NAMESPACE_VSOCK_H + +#include + +enum vsock_net_mode { + VSOCK_NET_MODE_GLOBAL, + VSOCK_NET_MODE_LOCAL, +}; + +struct netns_vsock { + struct ctl_table_header *sysctl_hdr; + enum vsock_net_mode mode; + int mode_locked; +}; +#endif /* __NET_NET_NAMESPACE_VSOCK_H */ --=20 2.47.3 From nobody Tue Dec 2 02:43:45 2025 Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9B344317711 for ; Tue, 18 Nov 2025 02:00:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763431234; cv=none; b=sJyvgjJ6lL+tD+MQEe9UWMdBIygUHX92esZ0zJBWfuTPcJmJ8hOwGpHDSZGUi0xBeONH3fXOh68le2pRXg1Vwemyp5Ct+YX3ajMy2wtaV4haXtC3L1amjV9wBgCM7G6naPSSn2hxSLS2puj0iqbaXgGn1ZG4/N1f/11qwUcrOII= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763431234; c=relaxed/simple; bh=r65tIkVGPG+I7txZxl659u96TA/A/kOu/wQFyXqP57E=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=mDzPNIMeyRjsfY+undYngqOd/fVJIWZC8tltKlOAuEBsUwLfbXVfKYqHew+U0zM+DOpXMVHM7ZfLOPxaKouyJNFsQlp/M0cwgN3vDHMcmz7hq2aorfuoL+ne2DbhUb9g14T0tzHhah8bB05G1WzLelT6/8gKfrqFjOTrnUqXPEE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=a4iVRA56; arc=none smtp.client-ip=209.85.210.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="a4iVRA56" Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-7baf61be569so3352398b3a.3 for ; Mon, 17 Nov 2025 18:00:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763431231; x=1764036031; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=yUyVTo4aOY50nXTgh+D69xvYR/JGoBaNPPEBWVCSt0M=; b=a4iVRA56UPp85F/cxSk/uU3ipsZ0rv9j7f1z8+6QEHcw32AgWAZDAgRScxOpDJQyGr yswm40CuIqbA09HcgwcwTEgn+tdhXJXi6oosEtqmVV7FfhTJ24PPnIAST32e/7Cfazv9 ofUT1Gj4W3itoqqC9X+ndB4wCz0Vx9TFqnzRix4dYKDqxDBLX4tRxnb15gdSeqGWUQL8 ZVOzxCU9TpfkH5mSyXs9aHCmUzuqN0U5FCylM5gwCnUphmSH+YFwvs4M7/78V2+LDAqc sOCVkkbOB6+mSyjtBsw317nK5xfJNZmy5cVeTE5DLKpcCskuFgZspb7TEakJcOwnJA5N k69A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763431231; x=1764036031; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=yUyVTo4aOY50nXTgh+D69xvYR/JGoBaNPPEBWVCSt0M=; b=IgVXumF6y/FeZxQK5y2TLaF/nmTCIAHHSAouVeRUS7z/EoK77McpE3HAWA3CQMZpi5 6/eYn2X0XUNBE7ivBQkmwlbDE7yOL8bxCbspczZW5YVWht6XIqth+K9KOli04mp8x1D4 SZ/kVozjrerhgKxHz1k3d1Zux0jaW6X9qVOeQIkGoPIpD3OZYG+TGxjY4HQlIFi0LAC7 itsB1Wbj3UB0rOhZWhM4k4tTw0Heos0L1hBn4jeeXSIVxTVrF+SWU8AjLrOVq36/eWBg KY06LRvjyczKjCbukHwSVBOOnBYVOvU6b74ASZ9ANEc1HvgUyhhqQbDxbTqfVQyTBNdT tWag== X-Gm-Message-State: AOJu0Yw61E/dm55/Clo4qm/xsaehZw59N1VQIIyV3mjsEKRq8DQtikoD UCQe5lMiozmkDUGQaXXZOJEwDzo3j5fFqe4yUQIpOOgL2t0+2KhjOufv X-Gm-Gg: ASbGncu2YiCLHbmN7ll6Y4guwzJU1sYDlLCJKs7HWnpBcFAQ5YpmfqYqXdupuqTDfOA Kbrh+vKkf48uaFVjqffcNPL9T+7HuY1WK22PbA/WLii7I6hghrY1cfDf0NKeoY8OVqVujd6fDz/ dY8tClMW8hCz5RJtWgXvesnIDq/QRKjX+t48w9XtV/+/aQTSYRPpgILBA35Fumy+dgsiVVuqGVE Mm37Wp0gjhUypYAXaHOh5+ga8kHrG974AoZFQCvD0AqgVQuzrh8k6KfAPdGMJEaWM0pJFPYY5KX Z24MSYwqAarR3ZpC+sbTJC6/yrPs1duwsn4soZAZGz8cNDK5AU/TBgGzJZBwWKeiXFzbKuxqZmW eeIRVVFDehByEHIHHchUwh9LUVA9TJy/VNN4CDQFWAmEG2/EQtkSSmUtIE1LN95LgCWbWd1JxI8 ojB1NPBIXSxfuk1/0igII+JFA9ZBdCbg== X-Google-Smtp-Source: AGHT+IFJYn4lxHfAW8tbjtz9bBBDaSZZLromoL9YW/JZC02qAxmCSb3/fgSzPkLLZvefvuODipbaJA== X-Received: by 2002:a05:6a00:1797:b0:7a9:c738:5e88 with SMTP id d2e1a72fcca58-7ba3a0bbe05mr16120322b3a.8.1763431230519; Mon, 17 Nov 2025 18:00:30 -0800 (PST) Received: from localhost ([2a03:2880:2ff:1::]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7b92782d2e3sm14527449b3a.59.2025.11.17.18.00.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Nov 2025 18:00:30 -0800 (PST) From: Bobby Eshleman Date: Mon, 17 Nov 2025 18:00:25 -0800 Subject: [PATCH net-next v10 02/11] vsock: add netns to vsock core Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251117-vsock-vmtest-v10-2-df08f165bf3e@meta.com> References: <20251117-vsock-vmtest-v10-0-df08f165bf3e@meta.com> In-Reply-To: <20251117-vsock-vmtest-v10-0-df08f165bf3e@meta.com> To: Stefano Garzarella , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Stefan Hajnoczi , "Michael S. Tsirkin" , Jason Wang , =?utf-8?q?Eugenio_P=C3=A9rez?= , Xuan Zhuo , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Bryan Tan , Vishnu Dasa , Broadcom internal kernel review list , Shuah Khan Cc: linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, netdev@vger.kernel.org, kvm@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kselftest@vger.kernel.org, Sargun Dhillon , Bobby Eshleman , berrange@redhat.com, Bobby Eshleman X-Mailer: b4 0.14.3 From: Bobby Eshleman Add netns logic to vsock core. Additionally, modify transport hook prototypes to be used by later transport-specific patches (e.g., *_seqpacket_allow()). Namespaces are supported primarily by changing socket lookup functions (e.g., vsock_find_connected_socket()) to take into account the socket namespace and the namespace mode before considering a candidate socket a "match". This patch also introduces the sysctl /proc/sys/net/vsock/ns_mode that accepts the "global" or "local" mode strings. Add netns functionality (initialization, passing to transports, procfs, etc...) to the af_vsock socket layer. Later patches that add netns support to transports depend on this patch. seqpacket_allow() callbacks are modified to take a vsk so that transport implementations can inspect sock_net(sk) and vsk->net_mode when performing lookups (e.g., vhost does this in its future netns patch). Because the API change affects all transports, it seemed more appropriate to make this internal API change in the "vsock core" patch then in the "vhost" patch. Signed-off-by: Bobby Eshleman Reviewed-by: Stefano Garzarella Suggested-by: Sargun Dhillon --- Changes in v10: - add file-level comment about what happens to sockets/devices when the namespace mode changes (Stefano) - change the 'if (write)' boolean in vsock_net_mode_string() to if (!write), this simplifies a later patch which adds "goto" for mutex unlocking on function exit. Changes in v9: - remove virtio_vsock_alloc_rx_skb() (Stefano) - remove vsock_global_dummy_net, not needed as net=3DNULL + net_mode=3DVSOCK_NET_MODE_GLOBAL achieves identical result Changes in v7: - hv_sock: fix hyperv build error - explain why vhost does not use the dummy - explain usage of __vsock_global_dummy_net - explain why VSOCK_NET_MODE_STR_MAX is 8 characters - use switch-case in vsock_net_mode_string() - avoid changing transports as much as possible - add vsock_find_{bound,connected}_socket_net() - rename `vsock_hdr` to `sysctl_hdr` - add virtio_vsock_alloc_linear_skb() wrapper for setting dummy net and global mode for virtio-vsock, move skb->cb zero-ing into wrapper - explain seqpacket_allow() change - move net setting to __vsock_create() instead of vsock_create() so that child sockets also have their net assigned upon accept() Changes in v6: - unregister sysctl ops in vsock_exit() - af_vsock: clarify description of CID behavior - af_vsock: fix buf vs buffer naming, and length checking - af_vsock: fix length checking w/ correct ctl_table->maxlen Changes in v5: - vsock_global_net() -> vsock_global_dummy_net() - update comments for new uAPI - use /proc/sys/net/vsock/ns_mode instead of /proc/net/vsock_ns_mode - add prototype changes so patch remains compilable --- drivers/vhost/vsock.c | 6 +- include/net/af_vsock.h | 9 +- net/vmw_vsock/af_vsock.c | 258 +++++++++++++++++++++++++++++++++++= +--- net/vmw_vsock/virtio_transport.c | 6 +- net/vmw_vsock/vsock_loopback.c | 6 +- 5 files changed, 261 insertions(+), 24 deletions(-) diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c index ae01457ea2cd..2c937a2df83b 100644 --- a/drivers/vhost/vsock.c +++ b/drivers/vhost/vsock.c @@ -404,7 +404,8 @@ static bool vhost_transport_msgzerocopy_allow(void) return true; } =20 -static bool vhost_transport_seqpacket_allow(u32 remote_cid); +static bool +vhost_transport_seqpacket_allow(struct vsock_sock *vsk, u32 remote_cid); =20 static struct virtio_transport vhost_transport =3D { .transport =3D { @@ -460,7 +461,8 @@ static struct virtio_transport vhost_transport =3D { .send_pkt =3D vhost_transport_send_pkt, }; =20 -static bool vhost_transport_seqpacket_allow(u32 remote_cid) +static bool +vhost_transport_seqpacket_allow(struct vsock_sock *vsk, u32 remote_cid) { struct vhost_vsock *vsock; bool seqpacket_allow =3D false; diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h index 9b5bdd083b6f..59d97a143204 100644 --- a/include/net/af_vsock.h +++ b/include/net/af_vsock.h @@ -145,7 +145,7 @@ struct vsock_transport { int flags); int (*seqpacket_enqueue)(struct vsock_sock *vsk, struct msghdr *msg, size_t len); - bool (*seqpacket_allow)(u32 remote_cid); + bool (*seqpacket_allow)(struct vsock_sock *vsk, u32 remote_cid); u32 (*seqpacket_has_data)(struct vsock_sock *vsk); =20 /* Notification. */ @@ -218,6 +218,13 @@ void vsock_remove_connected(struct vsock_sock *vsk); struct sock *vsock_find_bound_socket(struct sockaddr_vm *addr); struct sock *vsock_find_connected_socket(struct sockaddr_vm *src, struct sockaddr_vm *dst); +struct sock *vsock_find_bound_socket_net(struct sockaddr_vm *addr, + struct net *net, + enum vsock_net_mode net_mode); +struct sock *vsock_find_connected_socket_net(struct sockaddr_vm *src, + struct sockaddr_vm *dst, + struct net *net, + enum vsock_net_mode net_mode); void vsock_remove_sock(struct vsock_sock *vsk); void vsock_for_each_connected_socket(struct vsock_transport *transport, void (*fn)(struct sock *sk)); diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index 72bb6b7ed386..54373ae101c3 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -83,6 +83,38 @@ * TCP_ESTABLISHED - connected * TCP_CLOSING - disconnecting * TCP_LISTEN - listening + * + * - Namespaces in vsock support two different modes configured + * through /proc/sys/net/vsock/ns_mode. The modes are "local" and "globa= l". + * Each mode defines how the namespace interacts with CIDs. + * /proc/sys/net/vsock/ns_mode is write-once, so that it may be configur= ed + * and locked down by a namespace manager. The default is "global". The = mode + * is set per-namespace. + * + * The modes affect the allocation and accessibility of CIDs as follows: + * + * - global - access and allocation are all system-wide + * - all CID allocation from global namespaces draw from the same + * system-wide pool. + * - if one global namespace has already allocated some CID, another + * global namespace will not be able to allocate the same CID. + * - global mode AF_VSOCK sockets can reach any VM or socket in any g= lobal + * namespace, they are not contained to only their own namespace. + * - AF_VSOCK sockets in a global mode namespace cannot reach VMs or + * sockets in any local mode namespace. + * - local - access and allocation are contained within the namespace + * - CID allocation draws only from a private pool local only to the + * namespace, and does not affect the CIDs available for allocation = in any + * other namespace (global or local). + * - VMs in a local namespace do not collide with CIDs in any other lo= cal + * namespace or any global namespace. For example, if a VM in a loca= l mode + * namespace is given CID 10, then CID 10 is still available for + * allocation in any other namespace, but not in the same namespace. + * - AF_VSOCK sockets in a local mode namespace can connect only to VM= s or + * other sockets within their own namespace. + * - when a socket or device is initialized in a namespace with mode + * global, it will stay in global mode even if the namespace later + * changes to local. */ =20 #include @@ -100,6 +132,7 @@ #include #include #include +#include #include #include #include @@ -111,9 +144,18 @@ #include #include #include +#include #include #include =20 +#define VSOCK_NET_MODE_STR_GLOBAL "global" +#define VSOCK_NET_MODE_STR_LOCAL "local" + +/* 6 chars for "global", 1 for null-terminator, and 1 more for '\n'. + * The newline is added by proc_dostring() for read operations. + */ +#define VSOCK_NET_MODE_STR_MAX 8 + static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr); static void vsock_sk_destruct(struct sock *sk); static int vsock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb); @@ -235,33 +277,47 @@ static void __vsock_remove_connected(struct vsock_soc= k *vsk) sock_put(&vsk->sk); } =20 -static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr) +static struct sock *__vsock_find_bound_socket_net(struct sockaddr_vm *addr, + struct net *net, + enum vsock_net_mode net_mode) { struct vsock_sock *vsk; =20 list_for_each_entry(vsk, vsock_bound_sockets(addr), bound_table) { - if (vsock_addr_equals_addr(addr, &vsk->local_addr)) - return sk_vsock(vsk); + struct sock *sk =3D sk_vsock(vsk); + + if (vsock_addr_equals_addr(addr, &vsk->local_addr) && + vsock_net_check_mode(sock_net(sk), vsk->net_mode, net, + net_mode)) + return sk; =20 if (addr->svm_port =3D=3D vsk->local_addr.svm_port && (vsk->local_addr.svm_cid =3D=3D VMADDR_CID_ANY || - addr->svm_cid =3D=3D VMADDR_CID_ANY)) - return sk_vsock(vsk); + addr->svm_cid =3D=3D VMADDR_CID_ANY) && + vsock_net_check_mode(sock_net(sk), vsk->net_mode, net, + net_mode)) + return sk; } =20 return NULL; } =20 -static struct sock *__vsock_find_connected_socket(struct sockaddr_vm *src, - struct sockaddr_vm *dst) +static struct sock * +__vsock_find_connected_socket_net(struct sockaddr_vm *src, + struct sockaddr_vm *dst, struct net *net, + enum vsock_net_mode net_mode) { struct vsock_sock *vsk; =20 list_for_each_entry(vsk, vsock_connected_sockets(src, dst), connected_table) { + struct sock *sk =3D sk_vsock(vsk); + if (vsock_addr_equals_addr(src, &vsk->remote_addr) && - dst->svm_port =3D=3D vsk->local_addr.svm_port) { - return sk_vsock(vsk); + dst->svm_port =3D=3D vsk->local_addr.svm_port && + vsock_net_check_mode(sock_net(sk), vsk->net_mode, net, + net_mode)) { + return sk; } } =20 @@ -304,12 +360,14 @@ void vsock_remove_connected(struct vsock_sock *vsk) } EXPORT_SYMBOL_GPL(vsock_remove_connected); =20 -struct sock *vsock_find_bound_socket(struct sockaddr_vm *addr) +struct sock *vsock_find_bound_socket_net(struct sockaddr_vm *addr, + struct net *net, + enum vsock_net_mode net_mode) { struct sock *sk; =20 spin_lock_bh(&vsock_table_lock); - sk =3D __vsock_find_bound_socket(addr); + sk =3D __vsock_find_bound_socket_net(addr, net, net_mode); if (sk) sock_hold(sk); =20 @@ -317,15 +375,23 @@ struct sock *vsock_find_bound_socket(struct sockaddr_= vm *addr) =20 return sk; } +EXPORT_SYMBOL_GPL(vsock_find_bound_socket_net); + +struct sock *vsock_find_bound_socket(struct sockaddr_vm *addr) +{ + return vsock_find_bound_socket_net(addr, NULL, VSOCK_NET_MODE_GLOBAL); +} EXPORT_SYMBOL_GPL(vsock_find_bound_socket); =20 -struct sock *vsock_find_connected_socket(struct sockaddr_vm *src, - struct sockaddr_vm *dst) +struct sock *vsock_find_connected_socket_net(struct sockaddr_vm *src, + struct sockaddr_vm *dst, + struct net *net, + enum vsock_net_mode net_mode) { struct sock *sk; =20 spin_lock_bh(&vsock_table_lock); - sk =3D __vsock_find_connected_socket(src, dst); + sk =3D __vsock_find_connected_socket_net(src, dst, net, net_mode); if (sk) sock_hold(sk); =20 @@ -333,6 +399,14 @@ struct sock *vsock_find_connected_socket(struct sockad= dr_vm *src, =20 return sk; } +EXPORT_SYMBOL_GPL(vsock_find_connected_socket_net); + +struct sock *vsock_find_connected_socket(struct sockaddr_vm *src, + struct sockaddr_vm *dst) +{ + return vsock_find_connected_socket_net(src, dst, + NULL, VSOCK_NET_MODE_GLOBAL); +} EXPORT_SYMBOL_GPL(vsock_find_connected_socket); =20 void vsock_remove_sock(struct vsock_sock *vsk) @@ -528,7 +602,7 @@ int vsock_assign_transport(struct vsock_sock *vsk, stru= ct vsock_sock *psk) =20 if (sk->sk_type =3D=3D SOCK_SEQPACKET) { if (!new_transport->seqpacket_allow || - !new_transport->seqpacket_allow(remote_cid)) { + !new_transport->seqpacket_allow(vsk, remote_cid)) { module_put(new_transport->module); return -ESOCKTNOSUPPORT; } @@ -676,6 +750,7 @@ static void vsock_pending_work(struct work_struct *work) static int __vsock_bind_connectible(struct vsock_sock *vsk, struct sockaddr_vm *addr) { + struct net *net =3D sock_net(sk_vsock(vsk)); static u32 port; struct sockaddr_vm new_addr; =20 @@ -695,7 +770,8 @@ static int __vsock_bind_connectible(struct vsock_sock *= vsk, =20 new_addr.svm_port =3D port++; =20 - if (!__vsock_find_bound_socket(&new_addr)) { + if (!__vsock_find_bound_socket_net(&new_addr, net, + vsk->net_mode)) { found =3D true; break; } @@ -712,7 +788,8 @@ static int __vsock_bind_connectible(struct vsock_sock *= vsk, return -EACCES; } =20 - if (__vsock_find_bound_socket(&new_addr)) + if (__vsock_find_bound_socket_net(&new_addr, net, + vsk->net_mode)) return -EADDRINUSE; } =20 @@ -836,6 +913,8 @@ static struct sock *__vsock_create(struct net *net, vsk->buffer_max_size =3D VSOCK_DEFAULT_BUFFER_MAX_SIZE; } =20 + vsk->net_mode =3D vsock_net_mode(net); + return sk; } =20 @@ -2636,6 +2715,142 @@ static struct miscdevice vsock_device =3D { .fops =3D &vsock_device_ops, }; =20 +static int vsock_net_mode_string(const struct ctl_table *table, int write, + void *buffer, size_t *lenp, loff_t *ppos) +{ + char data[VSOCK_NET_MODE_STR_MAX] =3D {0}; + enum vsock_net_mode mode; + struct ctl_table tmp; + struct net *net; + int ret; + + if (!table->data || !table->maxlen || !*lenp) { + *lenp =3D 0; + return 0; + } + + net =3D current->nsproxy->net_ns; + tmp =3D *table; + tmp.data =3D data; + + if (!write) { + const char *p; + + mode =3D vsock_net_mode(net); + + switch (mode) { + case VSOCK_NET_MODE_GLOBAL: + p =3D VSOCK_NET_MODE_STR_GLOBAL; + break; + case VSOCK_NET_MODE_LOCAL: + p =3D VSOCK_NET_MODE_STR_LOCAL; + break; + default: + WARN_ONCE(true, "netns has invalid vsock mode"); + *lenp =3D 0; + return 0; + } + + strscpy(data, p, sizeof(data)); + tmp.maxlen =3D strlen(p); + } + + ret =3D proc_dostring(&tmp, write, buffer, lenp, ppos); + if (ret) + return ret; + + if (!write) + return 0; + + if (*lenp >=3D sizeof(data)) + return -EINVAL; + + if (!strncmp(data, VSOCK_NET_MODE_STR_GLOBAL, sizeof(data))) + mode =3D VSOCK_NET_MODE_GLOBAL; + else if (!strncmp(data, VSOCK_NET_MODE_STR_LOCAL, sizeof(data))) + mode =3D VSOCK_NET_MODE_LOCAL; + else + return -EINVAL; + + if (!vsock_net_write_mode(net, mode)) + return -EPERM; + + return 0; +} + +static struct ctl_table vsock_table[] =3D { + { + .procname =3D "ns_mode", + .data =3D &init_net.vsock.mode, + .maxlen =3D VSOCK_NET_MODE_STR_MAX, + .mode =3D 0644, + .proc_handler =3D vsock_net_mode_string + }, +}; + +static int __net_init vsock_sysctl_register(struct net *net) +{ + struct ctl_table *table; + + if (net_eq(net, &init_net)) { + table =3D vsock_table; + } else { + table =3D kmemdup(vsock_table, sizeof(vsock_table), GFP_KERNEL); + if (!table) + goto err_alloc; + + table[0].data =3D &net->vsock.mode; + } + + net->vsock.sysctl_hdr =3D register_net_sysctl_sz(net, "net/vsock", table, + ARRAY_SIZE(vsock_table)); + if (!net->vsock.sysctl_hdr) + goto err_reg; + + return 0; + +err_reg: + if (!net_eq(net, &init_net)) + kfree(table); +err_alloc: + return -ENOMEM; +} + +static void vsock_sysctl_unregister(struct net *net) +{ + const struct ctl_table *table; + + table =3D net->vsock.sysctl_hdr->ctl_table_arg; + unregister_net_sysctl_table(net->vsock.sysctl_hdr); + if (!net_eq(net, &init_net)) + kfree(table); +} + +static void vsock_net_init(struct net *net) +{ + net->vsock.mode =3D VSOCK_NET_MODE_GLOBAL; +} + +static __net_init int vsock_sysctl_init_net(struct net *net) +{ + vsock_net_init(net); + + if (vsock_sysctl_register(net)) + return -ENOMEM; + + return 0; +} + +static __net_exit void vsock_sysctl_exit_net(struct net *net) +{ + vsock_sysctl_unregister(net); +} + +static struct pernet_operations vsock_sysctl_ops __net_initdata =3D { + .init =3D vsock_sysctl_init_net, + .exit =3D vsock_sysctl_exit_net, +}; + static int __init vsock_init(void) { int err =3D 0; @@ -2663,10 +2878,18 @@ static int __init vsock_init(void) goto err_unregister_proto; } =20 + if (register_pernet_subsys(&vsock_sysctl_ops)) { + err =3D -ENOMEM; + goto err_unregister_sock; + } + + vsock_net_init(&init_net); vsock_bpf_build_proto(); =20 return 0; =20 +err_unregister_sock: + sock_unregister(AF_VSOCK); err_unregister_proto: proto_unregister(&vsock_proto); err_deregister_misc: @@ -2680,6 +2903,7 @@ static void __exit vsock_exit(void) misc_deregister(&vsock_device); sock_unregister(AF_VSOCK); proto_unregister(&vsock_proto); + unregister_pernet_subsys(&vsock_sysctl_ops); } =20 const struct vsock_transport *vsock_core_get_transport(struct vsock_sock *= vsk) diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transp= ort.c index 8c867023a2e5..5d379ccf3770 100644 --- a/net/vmw_vsock/virtio_transport.c +++ b/net/vmw_vsock/virtio_transport.c @@ -536,7 +536,8 @@ static bool virtio_transport_msgzerocopy_allow(void) return true; } =20 -static bool virtio_transport_seqpacket_allow(u32 remote_cid); +static bool +virtio_transport_seqpacket_allow(struct vsock_sock *vsk, u32 remote_cid); =20 static struct virtio_transport virtio_transport =3D { .transport =3D { @@ -593,7 +594,8 @@ static struct virtio_transport virtio_transport =3D { .can_msgzerocopy =3D virtio_transport_can_msgzerocopy, }; =20 -static bool virtio_transport_seqpacket_allow(u32 remote_cid) +static bool +virtio_transport_seqpacket_allow(struct vsock_sock *vsk, u32 remote_cid) { struct virtio_vsock *vsock; bool seqpacket_allow; diff --git a/net/vmw_vsock/vsock_loopback.c b/net/vmw_vsock/vsock_loopback.c index bc2ff918b315..8722337a4f80 100644 --- a/net/vmw_vsock/vsock_loopback.c +++ b/net/vmw_vsock/vsock_loopback.c @@ -46,7 +46,8 @@ static int vsock_loopback_cancel_pkt(struct vsock_sock *v= sk) return 0; } =20 -static bool vsock_loopback_seqpacket_allow(u32 remote_cid); +static bool vsock_loopback_seqpacket_allow(struct vsock_sock *vsk, + u32 remote_cid); static bool vsock_loopback_msgzerocopy_allow(void) { return true; @@ -106,7 +107,8 @@ static struct virtio_transport loopback_transport =3D { .send_pkt =3D vsock_loopback_send_pkt, }; =20 -static bool vsock_loopback_seqpacket_allow(u32 remote_cid) +static bool +vsock_loopback_seqpacket_allow(struct vsock_sock *vsk, u32 remote_cid) { return true; } --=20 2.47.3 From nobody Tue Dec 2 02:43:45 2025 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7DD163218CF for ; Tue, 18 Nov 2025 02:00:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763431236; cv=none; b=q6TyEduIFKWxvukn06qx1oHoSemROC8n+ewuqixMFUz60UBKe2zlo6r7p2oFUYMEbGlTZwcPGhg7AUsAMSV32ReNYdwShM2iH3QvYjTSjNNysxGz/tSbYGKzv1bUjJbgj6JnNaHWDeh8QSXS1g+Ga99UPMArFIvfrr9/g5wU7JM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763431236; c=relaxed/simple; bh=lpQTojzEs/okUQ7fOYuYhyqIVXbB/fT4DFlfWpvIxXk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=lBEPgb+QQyMvCmcWEpBcb32hnlV4fErv0hhkQnXCOjicjhmmWaaHd1iFUwSnd9qFnZ8K3ITGDJ460RRL5zcx37/5atfKvt9AGdXKDZ0BKGzR1/qEwnTRR7TYodTzAcbpE1cb2QyFV4TWuGUir1qwvbPcUKK/2mIYNGEXD29nDhw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Qv3Rs1ej; arc=none smtp.client-ip=209.85.214.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Qv3Rs1ej" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-29845b06dd2so57002195ad.2 for ; Mon, 17 Nov 2025 18:00:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763431232; x=1764036032; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=b8aIeeKxPnyywiO1EfP9zuyICRurfwXKu8QSv7CunkI=; b=Qv3Rs1ejm4e1zyADitkycTjvUdHJGK9REhexfrwC299QF+bfpM3MM5jX16fhMF1T84 34bIv24RhMzzZNSB8208zcr1R6jDDbZCzdyyS/N88ssoVyePN/Os3O+MtfHiCe3D6n2J 0LCUwYchWlPL7PiyWFlZQMndoS+SIqonoF6qWXt8rWA99bPK+7mMLnMuh/8yKQKlzjxr 7ZDT5hlIv09dQ9t/K2OVTVk/Nv1tHVr1iWqNhd+Kz8gAy/3gkOcx/8TqtHez18VKIc7K wfNGx5vJxaIMi9yL6Y4y0ocYk4Nz3htw3XYz8n7foXvz/s/67nCkXwi2OClDtU61+WUz uZ7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763431232; x=1764036032; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=b8aIeeKxPnyywiO1EfP9zuyICRurfwXKu8QSv7CunkI=; b=NYi2YviApFKSDt7yzB4gA9S4eEBSPeqN0Qrp34r+aGuXu4gTRNLTxfLS5Y6WBgUAl5 IlpwpxvOyGLH+Y4Zu9krMawiMA8QCRwsb58xDWymMPWF43AXFk26YauL/lmMUQRDd4b+ odB6j7TEaCL3zlfcSCSHo862W9I252yj6HtwGPBy+BjbFfgBBoJlUcL7EGng/cpXxDud dLVVOMYtYecBxS0NYg74kyHKlMxFeNKz+K5hBV5YGDXC6QCFi0bO6QkkvT7GK4/6Acnb 686UNAt9eduRZiPTjTEeVNkpgZoeCeLihLoifMNOxSRcotpOL9Uo9l5IIKMZgYwEh5Kl 1iyg== X-Gm-Message-State: AOJu0YxjuAtj0LXgLZtxXZEB7uI2vdfrZFZ0BdadeTdKRVs4M2F9/SKq HcseQCIuOGikZRS26+ggx3p4qpVcz4fN4r1gmhmMSUPjqQx//RKPuSpS X-Gm-Gg: ASbGncuDcVg48epQ159/5KYhPElLATCiPZyg/CEvsB4AMaVK+D6H/XfLV+VkK14rn+V oZYuEhXcTQQHHorPxXTDTmXAWAEsEAn2AbQHm0WCVcQ+QFM4/M8I4dRy0ZoXNKeKIKMgVNW6Qho KA4N7UJVGhxUi+X8typi/yFXoEteZL3hTvXT7hhNa7M5FmiVo6KpNGjnZPS7ltNvRxrPnvtAgez MXk7apJx0SSbWKGr5b/ui6R1azedKujRwLBRyEkZzPshT+GxqBksFEwkQxdHdIrIDEHPlnXsPfL 921IxRbWw2dYupQTor1+AjqhZ8AuzxCs3enofETWhO4njDSl2SUa/Pxs8o3oKSIy4fIP0EM4alD jcF1ExIg/FsmcLcn8rs24Bfdg9K9wYoZX2aWsv1bkRy57V578Jj5FFC3bEAxhD4hWy5kBfoOqhU lVZVIqcLnRcKtVO1VPKXMH X-Google-Smtp-Source: AGHT+IEmB0prY00kHOsKXykH548IXaPXxfppFbbvu13bbSYlmmT/BQ2oYwszeiiVDOoywMQR5St25A== X-Received: by 2002:a17:903:1c5:b0:295:5945:2920 with SMTP id d9443c01a7336-2986a72c2dfmr169951025ad.34.1763431232086; Mon, 17 Nov 2025 18:00:32 -0800 (PST) Received: from localhost ([2a03:2880:2ff:73::]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2985c2b0d68sm155537775ad.61.2025.11.17.18.00.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Nov 2025 18:00:31 -0800 (PST) From: Bobby Eshleman Date: Mon, 17 Nov 2025 18:00:26 -0800 Subject: [PATCH net-next v10 03/11] vsock: reject bad VSOCK_NET_MODE_LOCAL configuration for G2H Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251117-vsock-vmtest-v10-3-df08f165bf3e@meta.com> References: <20251117-vsock-vmtest-v10-0-df08f165bf3e@meta.com> In-Reply-To: <20251117-vsock-vmtest-v10-0-df08f165bf3e@meta.com> To: Stefano Garzarella , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Stefan Hajnoczi , "Michael S. Tsirkin" , Jason Wang , =?utf-8?q?Eugenio_P=C3=A9rez?= , Xuan Zhuo , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Bryan Tan , Vishnu Dasa , Broadcom internal kernel review list , Shuah Khan Cc: linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, netdev@vger.kernel.org, kvm@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kselftest@vger.kernel.org, Sargun Dhillon , Bobby Eshleman , berrange@redhat.com, Bobby Eshleman X-Mailer: b4 0.14.3 From: Bobby Eshleman Reject setting VSOCK_NET_MODE_LOCAL with -EOPNOTSUPP if a G2H transport is operational. Additionally, reject G2H transport registration if there already exists a namespace in local mode. G2H sockets break in local mode because the G2H transports don't support namespacing yet. The current approach is to coerce packets coming out of G2H transports into VSOCK_NET_MODE_GLOBAL mode, but it is not possible to coerce sockets in the same way because it cannot be deduced which transport will be used by the socket. Specifically, when bound to VMADDR_CID_ANY in a nested VM (both G2H and H2G available), it is not until a packet is received and matched to the bound socket that we assign the transport. This presents a chicken-and-egg problem, because we need the namespace to lookup the socket and resolve the transport, but we need the transport to know how to use the namespace during lookup. For that reason, this patch prevents VSOCK_NET_MODE_LOCAL from being used on systems that support G2H, even nested systems that also have H2G transports. Local mode is blocked based on detecting the presence of G2H devices (when possible, as hyperv is special). This means that a host kernel with G2H support compiled in (or has the module loaded), will still support local mode if there is no G2H (e.g., virtio-vsock) device detected. This enables using the same kernel in the host and in the guest, as we do in kselftest. Systems with only namespace-aware transports (vhost-vsock, loopback) can still use both VSOCK_NET_MODE_GLOBAL and VSOCK_NET_MODE_LOCAL modes as intended. Add supports_local_mode() transport callback to indicate transport-specific local mode support. These restrictions can be lifted in a future patch series when G2H transports gain namespace support. Signed-off-by: Bobby Eshleman Suggested-by: Sargun Dhillon --- Changes in v10: - move this patch before any transports bring online namespacing (Stefano) - move vsock_net_mode_string into critical section (Stefano) - add ->supports_local_mode() callback to transports (Stefano) --- drivers/vhost/vsock.c | 6 +++++ include/net/af_vsock.h | 5 ++++ net/vmw_vsock/af_vsock.c | 50 ++++++++++++++++++++++++++++++++++--= ---- net/vmw_vsock/hyperv_transport.c | 6 +++++ net/vmw_vsock/virtio_transport.c | 13 +++++++++++ net/vmw_vsock/vmci_transport.c | 7 ++++++ net/vmw_vsock/vsock_loopback.c | 6 +++++ 7 files changed, 86 insertions(+), 7 deletions(-) diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c index 2c937a2df83b..c8319cd1c232 100644 --- a/drivers/vhost/vsock.c +++ b/drivers/vhost/vsock.c @@ -64,6 +64,11 @@ static u32 vhost_transport_get_local_cid(void) return VHOST_VSOCK_DEFAULT_HOST_CID; } =20 +static bool vhost_transport_supports_local_mode(void) +{ + return true; +} + /* Callers that dereference the return value must hold vhost_vsock_mutex o= r the * RCU read lock. */ @@ -412,6 +417,7 @@ static struct virtio_transport vhost_transport =3D { .module =3D THIS_MODULE, =20 .get_local_cid =3D vhost_transport_get_local_cid, + .supports_local_mode =3D vhost_transport_supports_local_mode, =20 .init =3D virtio_transport_do_socket_init, .destruct =3D virtio_transport_destruct, diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h index 59d97a143204..824d89657d41 100644 --- a/include/net/af_vsock.h +++ b/include/net/af_vsock.h @@ -180,6 +180,11 @@ struct vsock_transport { /* Addressing. */ u32 (*get_local_cid)(void); =20 + /* Return true if this transport supports VSOCK_NET_MODE_LOCAL. + * Otherwise, return false. + */ + bool (*supports_local_mode)(void); + /* Read a single skb */ int (*read_skb)(struct vsock_sock *, skb_read_actor_t); =20 diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index 54373ae101c3..7a235bb94437 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -91,6 +91,12 @@ * and locked down by a namespace manager. The default is "global". The = mode * is set per-namespace. * + * Note: LOCAL mode is only supported when using namespace-aware transpo= rts + * (vhost-vsock, loopback). If a guest-to-host transport (virtio-vsock, + * hyperv-vsock, vmci-vsock) is loaded, attempts to set LOCAL mode will = fail + * with EOPNOTSUPP, as these transports do not support per-namespace + * isolation. + * * The modes affect the allocation and accessibility of CIDs as follows: * * - global - access and allocation are all system-wide @@ -2765,17 +2771,30 @@ static int vsock_net_mode_string(const struct ctl_t= able *table, int write, if (*lenp >=3D sizeof(data)) return -EINVAL; =20 - if (!strncmp(data, VSOCK_NET_MODE_STR_GLOBAL, sizeof(data))) + ret =3D 0; + mutex_lock(&vsock_register_mutex); + if (!strncmp(data, VSOCK_NET_MODE_STR_GLOBAL, sizeof(data))) { mode =3D VSOCK_NET_MODE_GLOBAL; - else if (!strncmp(data, VSOCK_NET_MODE_STR_LOCAL, sizeof(data))) + } else if (!strncmp(data, VSOCK_NET_MODE_STR_LOCAL, sizeof(data))) { + if (transport_g2h && transport_g2h->supports_local_mode && + !transport_g2h->supports_local_mode()) { + ret =3D -EOPNOTSUPP; + goto out; + } mode =3D VSOCK_NET_MODE_LOCAL; - else - return -EINVAL; + } else { + ret =3D -EINVAL; + goto out; + } =20 - if (!vsock_net_write_mode(net, mode)) - return -EPERM; + if (!vsock_net_write_mode(net, mode)) { + ret =3D -EPERM; + goto out; + } =20 - return 0; +out: + mutex_unlock(&vsock_register_mutex); + return ret; } =20 static struct ctl_table vsock_table[] =3D { @@ -2916,6 +2935,7 @@ int vsock_core_register(const struct vsock_transport = *t, int features) { const struct vsock_transport *t_h2g, *t_g2h, *t_dgram, *t_local; int err =3D mutex_lock_interruptible(&vsock_register_mutex); + struct net *net; =20 if (err) return err; @@ -2938,6 +2958,22 @@ int vsock_core_register(const struct vsock_transport= *t, int features) err =3D -EBUSY; goto err_busy; } + + /* G2H sockets break in LOCAL mode namespaces because G2H + * transports don't support them yet. Block registering new G2H + * transports if we already have local mode namespaces on the + * system. + */ + rcu_read_lock(); + for_each_net_rcu(net) { + if (vsock_net_mode(net) =3D=3D VSOCK_NET_MODE_LOCAL) { + rcu_read_unlock(); + err =3D -EOPNOTSUPP; + goto err_busy; + } + } + rcu_read_unlock(); + t_g2h =3D t; } =20 diff --git a/net/vmw_vsock/hyperv_transport.c b/net/vmw_vsock/hyperv_transp= ort.c index 432fcbbd14d4..279f04fcd81a 100644 --- a/net/vmw_vsock/hyperv_transport.c +++ b/net/vmw_vsock/hyperv_transport.c @@ -833,10 +833,16 @@ int hvs_notify_set_rcvlowat(struct vsock_sock *vsk, i= nt val) return -EOPNOTSUPP; } =20 +static bool hvs_supports_local_mode(void) +{ + return false; +} + static struct vsock_transport hvs_transport =3D { .module =3D THIS_MODULE, =20 .get_local_cid =3D hvs_get_local_cid, + .supports_local_mode =3D hvs_supports_local_mode, =20 .init =3D hvs_sock_init, .destruct =3D hvs_destruct, diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transp= ort.c index 5d379ccf3770..e585cb66c6f5 100644 --- a/net/vmw_vsock/virtio_transport.c +++ b/net/vmw_vsock/virtio_transport.c @@ -94,6 +94,18 @@ static u32 virtio_transport_get_local_cid(void) return ret; } =20 +static bool virtio_transport_supports_local_mode(void) +{ + struct virtio_vsock *vsock; + + rcu_read_lock(); + vsock =3D rcu_dereference(the_virtio_vsock); + rcu_read_unlock(); + + /* Local mode is supported only when no G2H device is present. */ + return vsock ? false : true; +} + /* Caller need to hold vsock->tx_lock on vq */ static int virtio_transport_send_skb(struct sk_buff *skb, struct virtqueue= *vq, struct virtio_vsock *vsock, gfp_t gfp) @@ -544,6 +556,7 @@ static struct virtio_transport virtio_transport =3D { .module =3D THIS_MODULE, =20 .get_local_cid =3D virtio_transport_get_local_cid, + .supports_local_mode =3D virtio_transport_supports_local_mode, =20 .init =3D virtio_transport_do_socket_init, .destruct =3D virtio_transport_destruct, diff --git a/net/vmw_vsock/vmci_transport.c b/net/vmw_vsock/vmci_transport.c index 7eccd6708d66..da7c52ad7b2a 100644 --- a/net/vmw_vsock/vmci_transport.c +++ b/net/vmw_vsock/vmci_transport.c @@ -2033,6 +2033,12 @@ static u32 vmci_transport_get_local_cid(void) return vmci_get_context_id(); } =20 +static bool vmci_transport_supports_local_mode(void) +{ + /* Local mode is supported only when no device is present. */ + return vmci_transport_get_local_cid() =3D=3D VMCI_INVALID_ID; +} + static struct vsock_transport vmci_transport =3D { .module =3D THIS_MODULE, .init =3D vmci_transport_socket_init, @@ -2062,6 +2068,7 @@ static struct vsock_transport vmci_transport =3D { .notify_send_post_enqueue =3D vmci_transport_notify_send_post_enqueue, .shutdown =3D vmci_transport_shutdown, .get_local_cid =3D vmci_transport_get_local_cid, + .supports_local_mode =3D vmci_transport_supports_local_mode, }; =20 static bool vmci_check_transport(struct vsock_sock *vsk) diff --git a/net/vmw_vsock/vsock_loopback.c b/net/vmw_vsock/vsock_loopback.c index 8722337a4f80..1e25c1a6b43f 100644 --- a/net/vmw_vsock/vsock_loopback.c +++ b/net/vmw_vsock/vsock_loopback.c @@ -26,6 +26,11 @@ static u32 vsock_loopback_get_local_cid(void) return VMADDR_CID_LOCAL; } =20 +static bool vsock_loopback_supports_local_mode(void) +{ + return true; +} + static int vsock_loopback_send_pkt(struct sk_buff *skb) { struct vsock_loopback *vsock =3D &the_vsock_loopback; @@ -58,6 +63,7 @@ static struct virtio_transport loopback_transport =3D { .module =3D THIS_MODULE, =20 .get_local_cid =3D vsock_loopback_get_local_cid, + .supports_local_mode =3D vsock_loopback_supports_local_mode, =20 .init =3D virtio_transport_do_socket_init, .destruct =3D virtio_transport_destruct, --=20 2.47.3 From nobody Tue Dec 2 02:43:45 2025 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 49878325496 for ; Tue, 18 Nov 2025 02:00:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763431239; cv=none; b=CKfzOoCt94A8/z3xiQah6AH1jD+tLd+scGIYvQeWHuM0FiQNlQpedlV3QORXQ5WleZL0YeM50V/z+UcWtVCokbXa+BIOU0PW0Y2o54bBjOlvmTyM2KIXGe7MO+L36nQJffsUPqDoBOGp8EqnhLqceQHaZpcEvGoO991oHgC+E9c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763431239; c=relaxed/simple; bh=HJGq+8JznKPehBe3Auc7LlgZtPWKt50SOYvOScpGBw8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Up+6fbX7oy4SJByyJvyCRYddhyjPiB9JaWeiWqyERTa1RkvsebA5TFovzZghBrIzPaKYFU0rV4s+4i43WukBpajHYDwok6Z4cpxyM/pwShs712UST5HfA0A1yUzbMxtoqJ5AeVkA5f8KLp1PeVRMeR+GpEjyX1v1qIOR12k0xlM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=eUrWLcG1; arc=none smtp.client-ip=209.85.214.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="eUrWLcG1" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-297e239baecso54369875ad.1 for ; Mon, 17 Nov 2025 18:00:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763431234; x=1764036034; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=3hD0KYb00sKvkaOrJxVEtur1zHnF5j+8KCgDYEdmsvM=; b=eUrWLcG1t9oMdVVUHIY/p8VDYRVHSUxUwdMy6LZUlLOkrZ44wxarIlxDv1ZCvHyukL /q8DtaUrd07siEyd9pQ4YQqYZ7Grz55QTl9/sH2RLhxH2AlV5zqOk5Uy91EeIVJOZVuQ J5VHcJ1yP4Z+XqNEVWyFA9y8s3xsEFfpdT5czzjPEdpCSMyg/n6ymDqE1nld3C9171hf Gb0YvtxcINRYQtOLmB37LmMFU+YSuZvbQxWZkxmRkZ+NcVAWyJ3Sa5n4tRdSpmdcF83r 3Clf58/eFSyNFoPakSzinvEWwxUbQDWGZDyLTwUS8fjgqGgTtK1dYIzDpz8bA2HjqBmU 5f0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763431234; x=1764036034; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=3hD0KYb00sKvkaOrJxVEtur1zHnF5j+8KCgDYEdmsvM=; b=tOUPySReSpfTAoimTl5I/pSSJ/0rR+9NooS6lkrAAYCmsj7zLgRi0Wesi/2LphRO6W wL/tn4lqWYSsUVbE/DClcFPEDPH3ng14OI4ZJCi2swBC22pVoNEzYhf2zFykLh+Lp1qr MKEJPATux4U1OnQ5rKMDfGTdk2KUOWRhB/GILGXXb62ChSnCWEH3NzEIjQuTWSaBIDQl O/t+IMc72zphAd/3h2uP4A5RCk9sp7ueAKOIFlCm1+wkXjJfzfq9E6K9cpbKpq34p2K2 qcPSxk7Wg/OJseBJDHNr4a0s8O0FTt15qsuv61GHdrjHU3M+5EMaUQuMbE+IyVvvpAJv OG1A== X-Gm-Message-State: AOJu0YyB+VGjxXlsqxwP91aKVfnwHlR+NlvRiqKXrmzEwhPVcXC4rRgV iflyenWyQ9IK7MlBe55romPh4tsxbdmAVvwQpQBuFgntKN47MWuZa1++ X-Gm-Gg: ASbGncuzeB9IRamydw01VAmIaAKf6lSL/rVBf2OS+RTup01NTLZGt0bFLB1XVSuzBE8 6ZmkImnGAtR8Slje/IkWngCi8p0H64qC+Y0s0jR0A3aIIRXIvFhvoh0d3PDl7YuUJyh7JRqTu3X bIbaneTOhXfaTH0sJOjIVX/9mEmP1b2NnvVs+zqENZtmyrjsHm5AxR0MPGcnd0mFhovcs/GiKqE i+dVvlxKCcV98k5XtjerymNfYGGVqmA0AfzG8GOGaV1FkVS6dxQdTtNUoZngk86T6DHt/mmUPH0 +TZ7czKeNXDlM3WZT8pmSHJsSz6sG+NIvLgg7WXYR3ghjg723ts9rUiXxo3obyyuLRgKWxLiIZG YlzYCsEvqNh8iedYIfG0+/CIRLX8ZkFz415EVmZR+ibbLE8n01DbakLgf1Fqqid+u5hGKwoaqb0 KDmxlVzaF/ffWmwiIa8jwcj7MTtWcY2w== X-Google-Smtp-Source: AGHT+IF1CsQmfEx6EB8DX8w1tzs1Zdd3wDl7UiawoOG6oflh+1/zrlsscaroBegx2HOkAvflhjbKWg== X-Received: by 2002:a17:902:cf06:b0:298:5fde:5a77 with SMTP id d9443c01a7336-299f559e658mr18193015ad.22.1763431233593; Mon, 17 Nov 2025 18:00:33 -0800 (PST) Received: from localhost ([2a03:2880:2ff:6::]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2985c2345e3sm153683375ad.1.2025.11.17.18.00.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Nov 2025 18:00:32 -0800 (PST) From: Bobby Eshleman Date: Mon, 17 Nov 2025 18:00:27 -0800 Subject: [PATCH net-next v10 04/11] vsock: add netns support to virtio transports Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251117-vsock-vmtest-v10-4-df08f165bf3e@meta.com> References: <20251117-vsock-vmtest-v10-0-df08f165bf3e@meta.com> In-Reply-To: <20251117-vsock-vmtest-v10-0-df08f165bf3e@meta.com> To: Stefano Garzarella , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Stefan Hajnoczi , "Michael S. Tsirkin" , Jason Wang , =?utf-8?q?Eugenio_P=C3=A9rez?= , Xuan Zhuo , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Bryan Tan , Vishnu Dasa , Broadcom internal kernel review list , Shuah Khan Cc: linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, netdev@vger.kernel.org, kvm@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kselftest@vger.kernel.org, Sargun Dhillon , Bobby Eshleman , berrange@redhat.com, Bobby Eshleman X-Mailer: b4 0.14.3 From: Bobby Eshleman Add netns support to loopback and vhost. Keep netns disabled for virtio-vsock, but add necessary changes to comply with common API updates. Signed-off-by: Bobby Eshleman Suggested-by: Sargun Dhillon --- Changes in v10: - Splitting patches complicates the series with meaningless placeholder val= ues that eventually get replaced anyway, so to avoid that this patch combines into one. Links to previous patches here: - Link: https://lore.kernel.org/all/20251111-vsock-vmtest-v9-3-852787a37b= ed@meta.com/ - Link: https://lore.kernel.org/all/20251111-vsock-vmtest-v9-6-852787a37b= ed@meta.com/ - Link: https://lore.kernel.org/all/20251111-vsock-vmtest-v9-7-852787a37b= ed@meta.com/ - remove placeholder values (Stefano) - update comment describe net/net_mode for virtio_transport_reset_no_sock() --- drivers/vhost/vsock.c | 45 +++++++++++++++++------ include/linux/virtio_vsock.h | 8 +++-- net/vmw_vsock/virtio_transport.c | 10 ++++-- net/vmw_vsock/virtio_transport_common.c | 63 ++++++++++++++++++++++++-----= ---- net/vmw_vsock/vsock_loopback.c | 8 +++-- 5 files changed, 102 insertions(+), 32 deletions(-) diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c index c8319cd1c232..2846076d484f 100644 --- a/drivers/vhost/vsock.c +++ b/drivers/vhost/vsock.c @@ -46,6 +46,11 @@ static DEFINE_READ_MOSTLY_HASHTABLE(vhost_vsock_hash, 8); struct vhost_vsock { struct vhost_dev dev; struct vhost_virtqueue vqs[2]; + struct net *net; + netns_tracker ns_tracker; + + /* The ns mode at the time vhost_vsock was created */ + enum vsock_net_mode net_mode; =20 /* Link to global vhost_vsock_hash, writes use vhost_vsock_mutex */ struct hlist_node hash; @@ -72,7 +77,8 @@ static bool vhost_transport_supports_local_mode(void) /* Callers that dereference the return value must hold vhost_vsock_mutex o= r the * RCU read lock. */ -static struct vhost_vsock *vhost_vsock_get(u32 guest_cid) +static struct vhost_vsock *vhost_vsock_get(u32 guest_cid, struct net *net, + enum vsock_net_mode mode) { struct vhost_vsock *vsock; =20 @@ -83,9 +89,10 @@ static struct vhost_vsock *vhost_vsock_get(u32 guest_cid) if (other_cid =3D=3D 0) continue; =20 - if (other_cid =3D=3D guest_cid) + if (other_cid =3D=3D guest_cid && + vsock_net_check_mode(net, mode, vsock->net, + vsock->net_mode)) return vsock; - } =20 return NULL; @@ -274,7 +281,8 @@ static void vhost_transport_send_pkt_work(struct vhost_= work *work) } =20 static int -vhost_transport_send_pkt(struct sk_buff *skb) +vhost_transport_send_pkt(struct sk_buff *skb, struct net *net, + enum vsock_net_mode net_mode) { struct virtio_vsock_hdr *hdr =3D virtio_vsock_hdr(skb); struct vhost_vsock *vsock; @@ -283,7 +291,7 @@ vhost_transport_send_pkt(struct sk_buff *skb) rcu_read_lock(); =20 /* Find the vhost_vsock according to guest context id */ - vsock =3D vhost_vsock_get(le64_to_cpu(hdr->dst_cid)); + vsock =3D vhost_vsock_get(le64_to_cpu(hdr->dst_cid), net, net_mode); if (!vsock) { rcu_read_unlock(); kfree_skb(skb); @@ -310,7 +318,8 @@ vhost_transport_cancel_pkt(struct vsock_sock *vsk) rcu_read_lock(); =20 /* Find the vhost_vsock according to guest context id */ - vsock =3D vhost_vsock_get(vsk->remote_addr.svm_cid); + vsock =3D vhost_vsock_get(vsk->remote_addr.svm_cid, + sock_net(sk_vsock(vsk)), vsk->net_mode); if (!vsock) goto out; =20 @@ -470,11 +479,12 @@ static struct virtio_transport vhost_transport =3D { static bool vhost_transport_seqpacket_allow(struct vsock_sock *vsk, u32 remote_cid) { + struct net *net =3D sock_net(sk_vsock(vsk)); struct vhost_vsock *vsock; bool seqpacket_allow =3D false; =20 rcu_read_lock(); - vsock =3D vhost_vsock_get(remote_cid); + vsock =3D vhost_vsock_get(remote_cid, net, vsk->net_mode); =20 if (vsock) seqpacket_allow =3D vsock->seqpacket_allow; @@ -545,7 +555,8 @@ static void vhost_vsock_handle_tx_kick(struct vhost_wor= k *work) if (le64_to_cpu(hdr->src_cid) =3D=3D vsock->guest_cid && le64_to_cpu(hdr->dst_cid) =3D=3D vhost_transport_get_local_cid()) - virtio_transport_recv_pkt(&vhost_transport, skb); + virtio_transport_recv_pkt(&vhost_transport, skb, + vsock->net, vsock->net_mode); else kfree_skb(skb); =20 @@ -662,6 +673,7 @@ static int vhost_vsock_dev_open(struct inode *inode, st= ruct file *file) { struct vhost_virtqueue **vqs; struct vhost_vsock *vsock; + struct net *net; int ret; =20 /* This struct is large and allocation could fail, fall back to vmalloc @@ -677,6 +689,17 @@ static int vhost_vsock_dev_open(struct inode *inode, s= truct file *file) goto out; } =20 + net =3D current->nsproxy->net_ns; + vsock->net =3D get_net_track(net, &vsock->ns_tracker, GFP_KERNEL); + + /* Store the mode of the namespace at the time of creation. If this + * namespace later changes from "global" to "local", we want this vsock + * to continue operating normally and not suddenly break. For that + * reason, we save the mode here and later use it when performing + * socket lookups with vsock_net_check_mode() (see vhost_vsock_get()). + */ + vsock->net_mode =3D vsock_net_mode(net); + vsock->guest_cid =3D 0; /* no CID assigned yet */ vsock->seqpacket_allow =3D false; =20 @@ -716,7 +739,8 @@ static void vhost_vsock_reset_orphans(struct sock *sk) */ =20 /* If the peer is still valid, no need to reset connection */ - if (vhost_vsock_get(vsk->remote_addr.svm_cid)) + if (vhost_vsock_get(vsk->remote_addr.svm_cid, sock_net(sk), + vsk->net_mode)) return; =20 /* If the close timeout is pending, let it expire. This avoids races @@ -761,6 +785,7 @@ static int vhost_vsock_dev_release(struct inode *inode,= struct file *file) virtio_vsock_skb_queue_purge(&vsock->send_pkt_queue); =20 vhost_dev_cleanup(&vsock->dev); + put_net_track(vsock->net, &vsock->ns_tracker); kfree(vsock->dev.vqs); vhost_vsock_free(vsock); return 0; @@ -787,7 +812,7 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsoc= k, u64 guest_cid) =20 /* Refuse if CID is already in use */ mutex_lock(&vhost_vsock_mutex); - other =3D vhost_vsock_get(guest_cid); + other =3D vhost_vsock_get(guest_cid, vsock->net, vsock->net_mode); if (other && other !=3D vsock) { mutex_unlock(&vhost_vsock_mutex); return -EADDRINUSE; diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h index 0c67543a45c8..5ed6136a4ed4 100644 --- a/include/linux/virtio_vsock.h +++ b/include/linux/virtio_vsock.h @@ -173,6 +173,8 @@ struct virtio_vsock_pkt_info { u32 remote_cid, remote_port; struct vsock_sock *vsk; struct msghdr *msg; + struct net *net; + enum vsock_net_mode net_mode; u32 pkt_len; u16 type; u16 op; @@ -185,7 +187,8 @@ struct virtio_transport { struct vsock_transport transport; =20 /* Takes ownership of the packet */ - int (*send_pkt)(struct sk_buff *skb); + int (*send_pkt)(struct sk_buff *skb, struct net *net, + enum vsock_net_mode net_mode); =20 /* Used in MSG_ZEROCOPY mode. Checks, that provided data * (number of buffers) could be transmitted with zerocopy @@ -280,7 +283,8 @@ virtio_transport_dgram_enqueue(struct vsock_sock *vsk, void virtio_transport_destruct(struct vsock_sock *vsk); =20 void virtio_transport_recv_pkt(struct virtio_transport *t, - struct sk_buff *skb); + struct sk_buff *skb, struct net *net, + enum vsock_net_mode net_mode); void virtio_transport_inc_tx_pkt(struct virtio_vsock_sock *vvs, struct sk_= buff *skb); u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 wanted); void virtio_transport_put_credit(struct virtio_vsock_sock *vvs, u32 credit= ); diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transp= ort.c index e585cb66c6f5..bc266bdb7faa 100644 --- a/net/vmw_vsock/virtio_transport.c +++ b/net/vmw_vsock/virtio_transport.c @@ -243,7 +243,8 @@ static int virtio_transport_send_skb_fast_path(struct v= irtio_vsock *vsock, struc } =20 static int -virtio_transport_send_pkt(struct sk_buff *skb) +virtio_transport_send_pkt(struct sk_buff *skb, struct net *net, + enum vsock_net_mode net_mode) { struct virtio_vsock_hdr *hdr; struct virtio_vsock *vsock; @@ -675,7 +676,12 @@ static void virtio_transport_rx_work(struct work_struc= t *work) virtio_vsock_skb_put(skb, payload_len); =20 virtio_transport_deliver_tap_pkt(skb); - virtio_transport_recv_pkt(&virtio_transport, skb); + + /* Force virtio-transport into global mode since it + * does not yet support local-mode namespacing. + */ + virtio_transport_recv_pkt(&virtio_transport, skb, + NULL, VSOCK_NET_MODE_GLOBAL); } } while (!virtqueue_enable_cb(vq)); =20 diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio= _transport_common.c index dcc8a1d5851e..168e7517a3f0 100644 --- a/net/vmw_vsock/virtio_transport_common.c +++ b/net/vmw_vsock/virtio_transport_common.c @@ -413,7 +413,7 @@ static int virtio_transport_send_pkt_info(struct vsock_= sock *vsk, =20 virtio_transport_inc_tx_pkt(vvs, skb); =20 - ret =3D t_ops->send_pkt(skb); + ret =3D t_ops->send_pkt(skb, info->net, info->net_mode); if (ret < 0) break; =20 @@ -527,6 +527,8 @@ static int virtio_transport_send_credit_update(struct v= sock_sock *vsk) struct virtio_vsock_pkt_info info =3D { .op =3D VIRTIO_VSOCK_OP_CREDIT_UPDATE, .vsk =3D vsk, + .net =3D sock_net(sk_vsock(vsk)), + .net_mode =3D vsk->net_mode, }; =20 return virtio_transport_send_pkt_info(vsk, &info); @@ -1067,6 +1069,8 @@ int virtio_transport_connect(struct vsock_sock *vsk) struct virtio_vsock_pkt_info info =3D { .op =3D VIRTIO_VSOCK_OP_REQUEST, .vsk =3D vsk, + .net =3D sock_net(sk_vsock(vsk)), + .net_mode =3D vsk->net_mode, }; =20 return virtio_transport_send_pkt_info(vsk, &info); @@ -1082,6 +1086,8 @@ int virtio_transport_shutdown(struct vsock_sock *vsk,= int mode) (mode & SEND_SHUTDOWN ? VIRTIO_VSOCK_SHUTDOWN_SEND : 0), .vsk =3D vsk, + .net =3D sock_net(sk_vsock(vsk)), + .net_mode =3D vsk->net_mode, }; =20 return virtio_transport_send_pkt_info(vsk, &info); @@ -1108,6 +1114,8 @@ virtio_transport_stream_enqueue(struct vsock_sock *vs= k, .msg =3D msg, .pkt_len =3D len, .vsk =3D vsk, + .net =3D sock_net(sk_vsock(vsk)), + .net_mode =3D vsk->net_mode, }; =20 return virtio_transport_send_pkt_info(vsk, &info); @@ -1145,6 +1153,8 @@ static int virtio_transport_reset(struct vsock_sock *= vsk, .op =3D VIRTIO_VSOCK_OP_RST, .reply =3D !!skb, .vsk =3D vsk, + .net =3D sock_net(sk_vsock(vsk)), + .net_mode =3D vsk->net_mode, }; =20 /* Send RST only if the original pkt is not a RST pkt */ @@ -1156,15 +1166,27 @@ static int virtio_transport_reset(struct vsock_sock= *vsk, =20 /* Normally packets are associated with a socket. There may be no socket = if an * attempt was made to connect to a socket that does not exist. + * + * net and net_mode refer to the namespace of whoever sent the invalid mes= sage. + * For loopback, this is the namespace of the socket. For vhost, this is t= he + * namespace of the VM (i.e., vhost_vsock). */ static int virtio_transport_reset_no_sock(const struct virtio_transport *t, - struct sk_buff *skb) + struct sk_buff *skb, struct net *net, + enum vsock_net_mode net_mode) { struct virtio_vsock_hdr *hdr =3D virtio_vsock_hdr(skb); struct virtio_vsock_pkt_info info =3D { .op =3D VIRTIO_VSOCK_OP_RST, .type =3D le16_to_cpu(hdr->type), .reply =3D true, + + /* net or net_mode are not defined here because we pass + * net and net_mode directly to t->send_pkt(), instead of + * relying on virtio_transport_send_pkt_info() to pass them to + * t->send_pkt(). They are not needed by + * virtio_transport_alloc_skb(). + */ }; struct sk_buff *reply; =20 @@ -1183,7 +1205,7 @@ static int virtio_transport_reset_no_sock(const struc= t virtio_transport *t, if (!reply) return -ENOMEM; =20 - return t->send_pkt(reply); + return t->send_pkt(reply, net, net_mode); } =20 /* This function should be called with sk_lock held and SOCK_DONE set */ @@ -1465,6 +1487,8 @@ virtio_transport_send_response(struct vsock_sock *vsk, .remote_port =3D le32_to_cpu(hdr->src_port), .reply =3D true, .vsk =3D vsk, + .net =3D sock_net(sk_vsock(vsk)), + .net_mode =3D vsk->net_mode, }; =20 return virtio_transport_send_pkt_info(vsk, &info); @@ -1507,12 +1531,14 @@ virtio_transport_recv_listen(struct sock *sk, struc= t sk_buff *skb, int ret; =20 if (le16_to_cpu(hdr->op) !=3D VIRTIO_VSOCK_OP_REQUEST) { - virtio_transport_reset_no_sock(t, skb); + virtio_transport_reset_no_sock(t, skb, sock_net(sk), + vsk->net_mode); return -EINVAL; } =20 if (sk_acceptq_is_full(sk)) { - virtio_transport_reset_no_sock(t, skb); + virtio_transport_reset_no_sock(t, skb, sock_net(sk), + vsk->net_mode); return -ENOMEM; } =20 @@ -1520,13 +1546,15 @@ virtio_transport_recv_listen(struct sock *sk, struc= t sk_buff *skb, * Subsequent enqueues would lead to a memory leak. */ if (sk->sk_shutdown =3D=3D SHUTDOWN_MASK) { - virtio_transport_reset_no_sock(t, skb); + virtio_transport_reset_no_sock(t, skb, sock_net(sk), + vsk->net_mode); return -ESHUTDOWN; } =20 child =3D vsock_create_connected(sk); if (!child) { - virtio_transport_reset_no_sock(t, skb); + virtio_transport_reset_no_sock(t, skb, sock_net(sk), + vsk->net_mode); return -ENOMEM; } =20 @@ -1548,7 +1576,8 @@ virtio_transport_recv_listen(struct sock *sk, struct = sk_buff *skb, */ if (ret || vchild->transport !=3D &t->transport) { release_sock(child); - virtio_transport_reset_no_sock(t, skb); + virtio_transport_reset_no_sock(t, skb, sock_net(sk), + vsk->net_mode); sock_put(child); return ret; } @@ -1576,7 +1605,8 @@ static bool virtio_transport_valid_type(u16 type) * lock. */ void virtio_transport_recv_pkt(struct virtio_transport *t, - struct sk_buff *skb) + struct sk_buff *skb, struct net *net, + enum vsock_net_mode net_mode) { struct virtio_vsock_hdr *hdr =3D virtio_vsock_hdr(skb); struct sockaddr_vm src, dst; @@ -1599,24 +1629,25 @@ void virtio_transport_recv_pkt(struct virtio_transp= ort *t, le32_to_cpu(hdr->fwd_cnt)); =20 if (!virtio_transport_valid_type(le16_to_cpu(hdr->type))) { - (void)virtio_transport_reset_no_sock(t, skb); + (void)virtio_transport_reset_no_sock(t, skb, net, net_mode); goto free_pkt; } =20 /* The socket must be in connected or bound table * otherwise send reset back */ - sk =3D vsock_find_connected_socket(&src, &dst); + sk =3D vsock_find_connected_socket_net(&src, &dst, net, net_mode); if (!sk) { - sk =3D vsock_find_bound_socket(&dst); + sk =3D vsock_find_bound_socket_net(&dst, net, net_mode); if (!sk) { - (void)virtio_transport_reset_no_sock(t, skb); + (void)virtio_transport_reset_no_sock(t, skb, net, + net_mode); goto free_pkt; } } =20 if (virtio_transport_get_type(sk) !=3D le16_to_cpu(hdr->type)) { - (void)virtio_transport_reset_no_sock(t, skb); + (void)virtio_transport_reset_no_sock(t, skb, net, net_mode); sock_put(sk); goto free_pkt; } @@ -1635,7 +1666,7 @@ void virtio_transport_recv_pkt(struct virtio_transpor= t *t, */ if (sock_flag(sk, SOCK_DONE) || (sk->sk_state !=3D TCP_LISTEN && vsk->transport !=3D &t->transport)) { - (void)virtio_transport_reset_no_sock(t, skb); + (void)virtio_transport_reset_no_sock(t, skb, net, net_mode); release_sock(sk); sock_put(sk); goto free_pkt; @@ -1667,7 +1698,7 @@ void virtio_transport_recv_pkt(struct virtio_transpor= t *t, kfree_skb(skb); break; default: - (void)virtio_transport_reset_no_sock(t, skb); + (void)virtio_transport_reset_no_sock(t, skb, net, net_mode); kfree_skb(skb); break; } diff --git a/net/vmw_vsock/vsock_loopback.c b/net/vmw_vsock/vsock_loopback.c index 1e25c1a6b43f..a730fa74d2d9 100644 --- a/net/vmw_vsock/vsock_loopback.c +++ b/net/vmw_vsock/vsock_loopback.c @@ -31,7 +31,8 @@ static bool vsock_loopback_supports_local_mode(void) return true; } =20 -static int vsock_loopback_send_pkt(struct sk_buff *skb) +static int vsock_loopback_send_pkt(struct sk_buff *skb, struct net *net, + enum vsock_net_mode net_mode) { struct vsock_loopback *vsock =3D &the_vsock_loopback; int len =3D skb->len; @@ -138,7 +139,10 @@ static void vsock_loopback_work(struct work_struct *wo= rk) */ virtio_transport_consume_skb_sent(skb, false); virtio_transport_deliver_tap_pkt(skb); - virtio_transport_recv_pkt(&loopback_transport, skb); + + virtio_transport_recv_pkt(&loopback_transport, skb, + sock_net(skb->sk), + vsock_sk(skb->sk)->net_mode); } } =20 --=20 2.47.3 From nobody Tue Dec 2 02:43:45 2025 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E651B326922 for ; Tue, 18 Nov 2025 02:00:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763431239; cv=none; b=GkDuTqxNyybQ4pgGIHQklI3WCSRCLKo5tt8sr8GQeFAW+flt0CFx4glmAQxhGxljhIStknYqL2upbETgSO2JUEmfTq1IF7Ay4n6zZX53ctdDBLmSM1dh5hzA6DPbjP65ivjKkMtgHmiOGmhVzKL1DUl30xW8taspF2gHnPLGPg8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763431239; c=relaxed/simple; bh=uOW23oUD888uPdfswggyv7h4B3njoPEFvMHwF6iAAmo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=FGiWzrjUEUw7V0/cdwZdhI9eEHyIcYdqYut/z4AhzX/Jh+hI2UF7VnYVqHbKEJeyTZSS+8EDaLlUEHNmEnyidD5I2pJmMs+wKs8QdsWr4KMy0vxvw3nhRiOyF3ANgLlWIHKGUOmh6oXQ3GE+3UKPd7cufVZtefwfjslIG1r5iTs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=YvGyitWm; arc=none smtp.client-ip=209.85.216.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="YvGyitWm" Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-34372216275so5389385a91.2 for ; Mon, 17 Nov 2025 18:00:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763431235; x=1764036035; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=mhLUPvGLtyJcQ25GpVmgIbln5JiS+qaXLLDnH3A077U=; b=YvGyitWm5HtOLWDkacHtkEzVwBo6bLmw5bjRI5KahdU6vYZpie2LhsE7QRjfFmqpvV S1sg5FIzB3hunXYdxg+hCYhSEAgUXsIPtpwJXINmLFPJAPlzURuGMtG+a0TwuueynZtg NgPJIs2To4xQ0lc2YFdM5/Q8mC4H3KHf0wcgy5MSkk6oC9EynLVuPp38TwDBx2i1J4ZS 30Xj+vjwRrDyfDdXluTBYdaZFK6rukvRYbcBdh6aIkVPcAYWOYKrHvrD6XC34IACdZ9d Q0kQllovX2O+ohhUwjZj+ldw2jfhzUrMKxBGUwYVoG4jQIeihUvdopAKjJsrsz59WBi7 izwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763431235; x=1764036035; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=mhLUPvGLtyJcQ25GpVmgIbln5JiS+qaXLLDnH3A077U=; b=YhrWwZCJUk7cF7dPyFHqrxjiDu2Ltn2USP36sqvHjPyYQBhD0nlUxfyacrSDZmlxBJ F1u49awUQGbRhLvkNb3HRhFUfFzpx+O283wxwm92VDjNQ4pcr0GgjuBBi5TOjYPiJRKr zVTIQ4BACorNBvAvFnL3CTqTiUBKadjOLWeOPVwL8poFipcQZRZS9vq4SAA9/0rYBqmR nBrHXwamV+sPQSPTS4RQCHs1TWeqBZTIsR+I35iICvSKw8imDN4hNqsQG/JochkAnaJi 2ky8gMns3HK+U8I0WACrbiVuFAaR4mVRhwYbBm9J2U3KXBFWJ5SmmQ5p0bS7bXacNNDX wdPQ== X-Gm-Message-State: AOJu0YzJG27Fc69Gr4Qu8K0FCyrcYADCRyK882FDSoxubynlwspp2TYt 1qyqdC5BI4584LoMXDN58raLjR0hkw4S4eAXMdgXMxJ7E52gLeQqgXoG X-Gm-Gg: ASbGncs9LESW+EeWrJ481iaUwt5yBijomURXWkSXji/XRwNVN4GU6yZmk/O56/ofixZ I8hW3XcaOoWqwTMauYoZOH8sh5oDEr8zwElHs6UMHXBHoQCsooZA5ibUm2e9WsvgVmZzdOxyxNR 59jQ2VIsP2pHc2GoVQhpDWvQqi5q425Az/a7fkgQF3rOCEIs0diQ+On+urb8uUmwQN7SBCc8p+z nN+3l5tMkAfjnx6RJ4T6vOyP1rUWrGYuF0Ydsv51s+qbFBbSTgqlP+e9KOJ2Evg4FAXWrekPMFI clHrf7CkztcKaMrlxnnF5YUWCF+WMJiSUj+EwKuUjSlKbK+5mZwHD1X+gmOrKqQduWbIARu6+pc EQBX73s0V9IpAT6r/GU7LGfCv43Eh3DvSE7a+qTrD3jC5QSs8S9PSKxzItGgsckh65Ee58UGaLU rJ+mf+s3ACu+IYHPWVi1GEZJjRiXItig== X-Google-Smtp-Source: AGHT+IHLky6hFd2s4o/LoM/Lst3F8klAMSAkgSuH9Ql0+UHi5g9wFfThPyUZnxKhdSO4j0xQMtxpww== X-Received: by 2002:a17:90b:2d05:b0:32e:a10b:ce33 with SMTP id 98e67ed59e1d1-343fa6326e0mr15954772a91.21.1763431234714; Mon, 17 Nov 2025 18:00:34 -0800 (PST) Received: from localhost ([2a03:2880:2ff:9::]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-343e06fe521sm20082011a91.1.2025.11.17.18.00.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Nov 2025 18:00:34 -0800 (PST) From: Bobby Eshleman Date: Mon, 17 Nov 2025 18:00:28 -0800 Subject: [PATCH net-next v10 05/11] virtio: set skb owner of virtio_transport_reset_no_sock() reply Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251117-vsock-vmtest-v10-5-df08f165bf3e@meta.com> References: <20251117-vsock-vmtest-v10-0-df08f165bf3e@meta.com> In-Reply-To: <20251117-vsock-vmtest-v10-0-df08f165bf3e@meta.com> To: Stefano Garzarella , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Stefan Hajnoczi , "Michael S. Tsirkin" , Jason Wang , =?utf-8?q?Eugenio_P=C3=A9rez?= , Xuan Zhuo , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Bryan Tan , Vishnu Dasa , Broadcom internal kernel review list , Shuah Khan Cc: linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, netdev@vger.kernel.org, kvm@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kselftest@vger.kernel.org, Sargun Dhillon , Bobby Eshleman , berrange@redhat.com, Bobby Eshleman X-Mailer: b4 0.14.3 From: Bobby Eshleman Associate reply packets with the sending socket. When vsock must reply with an RST packet and there exists a sending socket (e.g., for loopback), setting the skb owner to the socket correctly handles reference counting between the skb and sk (i.e., the sk stays alive until the skb is freed). This allows the net namespace to be used for socket lookups for the duration of the reply skb's lifetime, preventing race conditions between the namespace lifecycle and vsock socket search using the namespace pointer. Signed-off-by: Bobby Eshleman Suggested-by: Sargun Dhillon --- Changes in v10: - break this out into its own patch for easy revert (Stefano) --- net/vmw_vsock/virtio_transport_common.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio= _transport_common.c index 168e7517a3f0..5bb498caa19e 100644 --- a/net/vmw_vsock/virtio_transport_common.c +++ b/net/vmw_vsock/virtio_transport_common.c @@ -1181,6 +1181,12 @@ static int virtio_transport_reset_no_sock(const stru= ct virtio_transport *t, .type =3D le16_to_cpu(hdr->type), .reply =3D true, =20 + /* Set sk owner to socket we are replying to (may be NULL for + * non-loopback). This keeps a reference to the sock and + * sock_net(sk) until the reply skb is freed. + */ + .vsk =3D vsock_sk(skb->sk), + /* net or net_mode are not defined here because we pass * net and net_mode directly to t->send_pkt(), instead of * relying on virtio_transport_send_pkt_info() to pass them to --=20 2.47.3 From nobody Tue Dec 2 02:43:45 2025 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 91D913254BE for ; Tue, 18 Nov 2025 02:00:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763431240; cv=none; b=GmxBYQj2gLhIc9KKd0ebQj56GwBglPbOalvTH/jYo7WWPjYFdvB12KEZYit7t6oahymd4RgYJBvCHd9xkk0BWkOe9qK6Iey4q/R4/OQ6GsCZrwXazSpbBR7DYMX4CCH0O+64EN/tqVrTUl1bDgdFdj+HQY1g+J3MENnMb24PmFc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763431240; c=relaxed/simple; bh=C74tPtLEcFlxKPQiq+20Ok+iNGw0X0cvYTmDWlPCWgM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=iVHR9os6oUCW/1ItT1yg4h0L7EaDLIk7bukuu/epMSpejeR5ksI8bJ7AsZHo1KqhYKZldzqRWmnE9fm+sFAix/eMtizdaKFHn9CrKgqFKErosj0GH1GhAzjRhj6A1sRzQ7Lhqx5O8bDpP52drv0p9moK3AnHtS/TMVTOi6ThTFQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=X3dpsW5V; arc=none smtp.client-ip=209.85.210.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="X3dpsW5V" Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-7ba55660769so3232744b3a.1 for ; Mon, 17 Nov 2025 18:00:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763431236; x=1764036036; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=p28zEBQeBUk36AvYw3frZ4faKmOpLSnPo9g0yADkH2g=; b=X3dpsW5VD1iHZMZBeS9LTTM+cu7GT7shshUYdIbgPDyWAkj5lxHNEk3tw8aCR33Y/C 6MmtHlbuW87dR1DK5O+R+oya3a7iSkQZ6ZtOaVF09e2+l5eyL5DCpAAhj9C62oT53WbC WxJi55It+J/zb8LdF7I/f5qTPUHxFdTy8/8kSRgyPOx6IBK1sfVX+V6a140r52ZhvXCW yzQRcTqzWnzEPkBtEWBNnFYYQD3cK3KsHTMbnb8mZ18V8jLOQDoSlp9w/+bur2Gf1/dT r9ci3lwe6GC9ErU0M43rrZXabJGZUjl10sDzLf2WrsmuWCbrfN70zdtIIpYIVFJXqJcn +ItA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763431236; x=1764036036; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=p28zEBQeBUk36AvYw3frZ4faKmOpLSnPo9g0yADkH2g=; b=ZDrfCtEcGIqrCp/Vn2an7MnFjtj2kFCPznjGUt7pWFBXHj8QnT8jji7J/vXb2cVKZy 6eq0wQz0CNlRiPwaUBtag8IzBfzGgnHkSbd3wu4ywGPZ1kDOzYsNpTheHlxJYOfHYrhN 4RaRuELkB3EerZDUjJEAFChSSPBbgpuWstiaVuuqT37eYjPppPlvf6uQjyDC3ro1GElA xFCyJA6Yh/kA1GrOMzSSO9hWpEytMAoRH551mm9v4ZSYok2S+nM8WHfsd/BZguk25DVp FPzkM0AjOj1x9E523pm9xPoNRBoOINV1KjI6ygXfsarQOJS9DWBoTb8o9n77A4sj6IJD NdLA== X-Gm-Message-State: AOJu0YwybH/z6H5xREcunDgp6t57zs7H+3cnKjERPeM7PVmmvp64zXNn SW++QtKEcz87u646P+fidscsc4Y8ZcA/V/RqdQ6eL750+MPA+Kis0zKq X-Gm-Gg: ASbGncuLwK8ZU/rPCLpDJ7dbnI93G3xIfUaBIhXORkzR4G7EsFKet2+Q5WwjNL5iPCC Oau+Q/ymwuPBPD5tRyVmemJEdU4NuwwEFYDh7AM+jmOZ1tfnrSAadbZUGfLURnfxiMsQUQg3x/F zM3YhmA+ujsTRZiy7xxORYuTt4yupMpvNDO5c+RnRWFPlnDTfyrQQmDrAa/dsIk4Oe/ZiMb3Pw8 ewteaWEDgFehTGW8B5v83TmMXuIBHcVTjHtHbqgKolkAlO5eOfhQoZeaO5pnjD5BlkhxlvE3M4h gulcIr7wDPZLcjxzNdRFwOjHe7aE26ThA6GLUGn9hGKmKwTkSf4QXjouZSg2Y6UBlYxQquQqUAQ GAFOM39yd6TEFdDOHtCqCXHisuMxKpxnX0yynOkN7R4NpGR5qAOfyq8pwlMiYNQsiDvFQjOLdty sMUo2P6ljNB/nfFsRoS/fe X-Google-Smtp-Source: AGHT+IEmbu2Oq+eI39o+T4uA9OHsNgBlxQx43D24whybJe4Uoof/z/T6SCWomsjNpxxj053tcafmCw== X-Received: by 2002:a05:6a00:2d1e:b0:7b8:c7f7:645e with SMTP id d2e1a72fcca58-7ba3c07eeebmr20196264b3a.17.1763431235927; Mon, 17 Nov 2025 18:00:35 -0800 (PST) Received: from localhost ([2a03:2880:2ff:70::]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7b92714e298sm14608613b3a.34.2025.11.17.18.00.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Nov 2025 18:00:35 -0800 (PST) From: Bobby Eshleman Date: Mon, 17 Nov 2025 18:00:29 -0800 Subject: [PATCH net-next v10 06/11] selftests/vsock: add namespace helpers to vmtest.sh Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251117-vsock-vmtest-v10-6-df08f165bf3e@meta.com> References: <20251117-vsock-vmtest-v10-0-df08f165bf3e@meta.com> In-Reply-To: <20251117-vsock-vmtest-v10-0-df08f165bf3e@meta.com> To: Stefano Garzarella , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Stefan Hajnoczi , "Michael S. Tsirkin" , Jason Wang , =?utf-8?q?Eugenio_P=C3=A9rez?= , Xuan Zhuo , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Bryan Tan , Vishnu Dasa , Broadcom internal kernel review list , Shuah Khan Cc: linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, netdev@vger.kernel.org, kvm@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kselftest@vger.kernel.org, Sargun Dhillon , Bobby Eshleman , berrange@redhat.com, Bobby Eshleman X-Mailer: b4 0.14.3 From: Bobby Eshleman Add functions for initializing namespaces with the different vsock NS modes. Callers can use add_namespaces() and del_namespaces() to create namespaces global0, global1, local0, and local1. The init_namespaces() function initializes global0, local0, etc... with their respective vsock NS mode. This function is separate so that tests that depend on this initialization can use it, while other tests that want to test the initialization interface itself can start with a clean slate by omitting this call. Remove namespaces upon exiting the program in cleanup(). This is unlikely to be needed for a healthy run, but it is useful for tests that are manually killed mid-test. In that case, this patch prevents the subsequent test run from finding stale namespaces with already-write-once-locked vsock ns modes. This patch is in preparation for later namespace tests. Signed-off-by: Bobby Eshleman Reviewed-by: Stefano Garzarella Suggested-by: Sargun Dhillon --- tools/testing/selftests/vsock/vmtest.sh | 41 +++++++++++++++++++++++++++++= ++++ 1 file changed, 41 insertions(+) diff --git a/tools/testing/selftests/vsock/vmtest.sh b/tools/testing/selfte= sts/vsock/vmtest.sh index c7b270dd77a9..f78cc574c274 100755 --- a/tools/testing/selftests/vsock/vmtest.sh +++ b/tools/testing/selftests/vsock/vmtest.sh @@ -49,6 +49,7 @@ readonly TEST_DESCS=3D( ) =20 readonly USE_SHARED_VM=3D(vm_server_host_client vm_client_host_server vm_l= oopback) +readonly NS_MODES=3D("local" "global") =20 VERBOSE=3D0 =20 @@ -103,6 +104,45 @@ check_result() { fi } =20 +add_namespaces() { + # add namespaces local0, local1, global0, and global1 + for mode in "${NS_MODES[@]}"; do + ip netns add "${mode}0" 2>/dev/null + ip netns add "${mode}1" 2>/dev/null + done +} + +init_namespaces() { + for mode in "${NS_MODES[@]}"; do + ns_set_mode "${mode}0" "${mode}" + ns_set_mode "${mode}1" "${mode}" + + log_host "set ns ${mode}0 to mode ${mode}" + log_host "set ns ${mode}1 to mode ${mode}" + + # we need lo for qemu port forwarding + ip netns exec "${mode}0" ip link set dev lo up + ip netns exec "${mode}1" ip link set dev lo up + done +} + +del_namespaces() { + for mode in "${NS_MODES[@]}"; do + ip netns del "${mode}0" &>/dev/null + ip netns del "${mode}1" &>/dev/null + log_host "removed ns ${mode}0" + log_host "removed ns ${mode}1" + done +} + +ns_set_mode() { + local ns=3D$1 + local mode=3D$2 + + echo "${mode}" | ip netns exec "${ns}" \ + tee /proc/sys/net/vsock/ns_mode &>/dev/null +} + vm_ssh() { ssh -q -o UserKnownHostsFile=3D/dev/null -p ${SSH_HOST_PORT} localhost "$= @" return $? @@ -110,6 +150,7 @@ vm_ssh() { =20 cleanup() { terminate_pidfiles "${!PIDFILES[@]}" + del_namespaces } =20 check_args() { --=20 2.47.3 From nobody Tue Dec 2 02:43:45 2025 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 24C1432721B for ; Tue, 18 Nov 2025 02:00:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763431241; cv=none; b=BRux3Ntf/37iQZUMimOWZ9LqamSMHbuiMVd5bzoTIwDId8SnRFRA431IKq2ZNNaIwrs/Q+L/jZ6WOxiFg6r9kH8pOoUdoq3XnqwJh16QX8eXqenw1x4A+PmQMqGQVAqC622VvbN3apD1flVbEw6ip0ISgTMYekeI6MkZdV03Ues= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763431241; c=relaxed/simple; bh=RSMm3eERoFaaRZ1jaG0xuUBAwDeKQLiQTsE/Ogn/QGQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=hsgQ4nC9IhHI4YwpBnuNIXZ3qIGZ6YYMbKWSVH4fXh3MUtUUahDqwJbR/4tTSEAjidSPPB6sCyaXQfRn73v1383C3FFUcRwbE0rs2bhiJLNRg9zfgmNouMxJPepVXRCipm/yfsfW0ZKLbEfFqH6cXT8ubR0eMnJFjJ7Qe1+WKXg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=TMHcTCzL; arc=none smtp.client-ip=209.85.210.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="TMHcTCzL" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-7ba92341f83so927967b3a.0 for ; Mon, 17 Nov 2025 18:00:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763431237; x=1764036037; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=9qtkcbdYM4WCxN5aS//lF0J6bT3rLhQLvWok9FW61UQ=; b=TMHcTCzLtRztB4vt5mMF0B72Q12NU6yINDQh9nFGcEhnhxHtFZuFvBzh6BAuPl+zFQ Y93bxfJqyJqiijPtPuGns+R+6lr7QZ3eojBhBIUZyOiodAs+Y/gPJMs0SJcpgT3kezSu 1MKU/6GrSV0kqco0oduyjQq6wGh6vGILI4i/XemiSPnKwGttqygmKOeOWUCfahVeyXjk /7JC+Wh52oL0RYcZ771U50CKNnfridZti3x9J3RCu3WetEvkTSyy82JxmOjSMMqIMuRR S/9BRT1nDfFNIkK99NbsD2ElhKU3Y3oQW+JuMdVJ1hlI14G+L4H067wnHV2TsGDLRuR8 pPDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763431237; x=1764036037; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=9qtkcbdYM4WCxN5aS//lF0J6bT3rLhQLvWok9FW61UQ=; b=iY2cRVnqDU7hrmw8bUzU7qD4k7OnBPM2UYc4Y9o7joEpFSreOwubzjKfI36YlgiNXK Yu5e7wgPpMCR/GcQU6FvfU7opo4ca8rPEFgYKLD6X7mkrv309rVojUR9ZPi3vJ+/aYfy 7Doy+DpjN758cEYBpJJOM+ljg7rSzsn6Da7ZFqTe0EAHy9O+Ij3Y0Me7ugtU4TE0OMLF lWXJfAY3WdmeePCzKuhc1lByuP2Lm5SVnefCZItMJJoicdHpMfwAC82kQRp12dF8ZQJT QQQBEaXdSHw3N/cyOngBnQTgHHBeDF4tceqkboI7btwoowxxORCZiCV/xoKPtCU3NXIV OlzA== X-Gm-Message-State: AOJu0Yw05GT0u9cexHtB2fn/fBI7jVEH98x8fA1394kQUhxBITPriRvK ij6aKaMm0vi+lKJnCnOgOnPuCUtuuSB4MR8Mz/fYk+PB8nyYCMOggJlY X-Gm-Gg: ASbGncuz9bd6Ul4IDQRV8jmdj3j7O/VC+gi6/cEc4KSnBYlkAaaMlodvDZBLVvSuiwG wGcv0UVDoPYm34u8T3eA6wD8j4nqaqJBEzpvY1kCXtEn3yV+9jCzA6mEum+kIALgtNJJ9vY5GeC /hQ/DEOJIWVD0+QYUXfJ0RE3POVIk3HEyCnksGWy1G+1o/cxnaTgR0rXgM2QsIasxdNOpcuNxLa yyVMtIY9o0ON2As2Bh4/Lb7QIrw59CD1N/oFgNBo8eTByzFZlAxUwq4qizEF4KV6b3tUz4Zm6dr wJ/BYpW10EZmL7U3LkJBwwCIy6T28O8/dHautrPC+D5Z2U9EZQBJ+QxXsoRVxTnyuh40z9tL1+8 7X31FAavlLfyl3G2okYGQFzuD7RTCiiIpA5ZKr+syxWGlxWa68h+txaSH96kgyYUf7towyUKihS N4QiSDni2eh0hme4026s9XS2odagi0CXQ= X-Google-Smtp-Source: AGHT+IG6339/NjJgXR+YTgVZQfk6Ocu2FLR6xEvhDIx9eY7e/aXidaGFs0oY4dZZOWsn9iPKWy6rGg== X-Received: by 2002:a05:6a21:999a:b0:34f:ce39:1f47 with SMTP id adf61e73a8af0-35ba1d8b9femr16998433637.38.1763431237085; Mon, 17 Nov 2025 18:00:37 -0800 (PST) Received: from localhost ([2a03:2880:2ff:4f::]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-bc36e8a58c4sm13363438a12.9.2025.11.17.18.00.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Nov 2025 18:00:36 -0800 (PST) From: Bobby Eshleman Date: Mon, 17 Nov 2025 18:00:30 -0800 Subject: [PATCH net-next v10 07/11] selftests/vsock: prepare vm management helpers for namespaces Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251117-vsock-vmtest-v10-7-df08f165bf3e@meta.com> References: <20251117-vsock-vmtest-v10-0-df08f165bf3e@meta.com> In-Reply-To: <20251117-vsock-vmtest-v10-0-df08f165bf3e@meta.com> To: Stefano Garzarella , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Stefan Hajnoczi , "Michael S. Tsirkin" , Jason Wang , =?utf-8?q?Eugenio_P=C3=A9rez?= , Xuan Zhuo , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Bryan Tan , Vishnu Dasa , Broadcom internal kernel review list , Shuah Khan Cc: linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, netdev@vger.kernel.org, kvm@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kselftest@vger.kernel.org, Sargun Dhillon , Bobby Eshleman , berrange@redhat.com, Bobby Eshleman X-Mailer: b4 0.14.3 From: Bobby Eshleman Add namespace support to vm management, ssh helpers, and vsock_test wrapper functions. This enables running VMs and test helpers in specific namespaces, which is required for upcoming namespace isolation tests. The functions still work correctly within the init ns, though the caller must now pass "init_ns" explicitly. No functional changes for existing tests. All have been updated to pass "init_ns" explicitly. Affected functions (such as vm_start() and vm_ssh()) now wrap their commands with 'ip netns exec' when executing commands in non-init namespaces. Reviewed-by: Stefano Garzarella Signed-off-by: Bobby Eshleman Suggested-by: Sargun Dhillon --- tools/testing/selftests/vsock/vmtest.sh | 102 +++++++++++++++++++++-------= ---- 1 file changed, 69 insertions(+), 33 deletions(-) diff --git a/tools/testing/selftests/vsock/vmtest.sh b/tools/testing/selfte= sts/vsock/vmtest.sh index f78cc574c274..1a7c810f282f 100755 --- a/tools/testing/selftests/vsock/vmtest.sh +++ b/tools/testing/selftests/vsock/vmtest.sh @@ -144,7 +144,18 @@ ns_set_mode() { } =20 vm_ssh() { - ssh -q -o UserKnownHostsFile=3D/dev/null -p ${SSH_HOST_PORT} localhost "$= @" + local ns_exec + + if [[ "${1}" =3D=3D init_ns ]]; then + ns_exec=3D"" + else + ns_exec=3D"ip netns exec ${1}" + fi + + shift + + ${ns_exec} ssh -q -o UserKnownHostsFile=3D/dev/null -p "${SSH_HOST_PORT}"= localhost "$@" + return $? } =20 @@ -267,10 +278,12 @@ terminate_pidfiles() { =20 vm_start() { local pidfile=3D$1 + local ns=3D$2 local logfile=3D/dev/null local verbose_opt=3D"" local kernel_opt=3D"" local qemu_opts=3D"" + local ns_exec=3D"" local qemu =20 qemu=3D$(command -v "${QEMU}") @@ -291,7 +304,11 @@ vm_start() { kernel_opt=3D"${KERNEL_CHECKOUT}" fi =20 - vng \ + if [[ "${ns}" !=3D "init_ns" ]]; then + ns_exec=3D"ip netns exec ${ns}" + fi + + ${ns_exec} vng \ --run \ ${kernel_opt} \ ${verbose_opt} \ @@ -306,6 +323,7 @@ vm_start() { } =20 vm_wait_for_ssh() { + local ns=3D$1 local i =20 i=3D0 @@ -313,7 +331,8 @@ vm_wait_for_ssh() { if [[ ${i} -gt ${WAIT_PERIOD_MAX} ]]; then die "Timed out waiting for guest ssh" fi - if vm_ssh -- true; then + + if vm_ssh "${ns}" -- true; then break fi i=3D$(( i + 1 )) @@ -347,30 +366,40 @@ wait_for_listener() } =20 vm_wait_for_listener() { - local port=3D$1 + local ns=3D$1 + local port=3D$2 =20 - vm_ssh <&1 | log_guest rc=3D$? else - vm_ssh -- "${VSOCK_TEST}" \ + vm_ssh "${ns}" -- "${VSOCK_TEST}" \ --mode=3Dserver \ --peer-cid=3D"${cid}" \ --control-port=3D"${port}" \ @@ -390,7 +419,7 @@ vm_vsock_test() { return $rc fi =20 - vm_wait_for_listener "${port}" + vm_wait_for_listener "${ns}" "${port}" rc=3D$? fi set +o pipefail @@ -399,22 +428,28 @@ vm_vsock_test() { } =20 host_vsock_test() { - local host=3D$1 - local cid=3D$2 - local port=3D$3 + local ns=3D$1 + local host=3D$2 + local cid=3D$3 + local port=3D$4 local rc =20 + local cmd=3D"${VSOCK_TEST}" + if [[ "${ns}" !=3D "init_ns" ]]; then + cmd=3D"ip netns exec ${ns} ${cmd}" + fi + # log output and use pipefail to respect vsock_test errors set -o pipefail if [[ "${host}" !=3D server ]]; then - ${VSOCK_TEST} \ + ${cmd} \ --mode=3Dclient \ --peer-cid=3D"${cid}" \ --control-host=3D"${host}" \ --control-port=3D"${port}" 2>&1 | log_host rc=3D$? else - ${VSOCK_TEST} \ + ${cmd} \ --mode=3Dserver \ --peer-cid=3D"${cid}" \ --control-port=3D"${port}" 2>&1 | log_host & @@ -425,7 +460,7 @@ host_vsock_test() { return $rc fi =20 - host_wait_for_listener "${port}" + host_wait_for_listener "${ns}" "${port}" rc=3D$? fi set +o pipefail @@ -469,11 +504,11 @@ log_guest() { } =20 test_vm_server_host_client() { - if ! vm_vsock_test "server" 2 "${TEST_GUEST_PORT}"; then + if ! vm_vsock_test "init_ns" "server" 2 "${TEST_GUEST_PORT}"; then return "${KSFT_FAIL}" fi =20 - if ! host_vsock_test "127.0.0.1" "${VSOCK_CID}" "${TEST_HOST_PORT}"; then + if ! host_vsock_test "init_ns" "127.0.0.1" "${VSOCK_CID}" "${TEST_HOST_PO= RT}"; then return "${KSFT_FAIL}" fi =20 @@ -481,11 +516,11 @@ test_vm_server_host_client() { } =20 test_vm_client_host_server() { - if ! host_vsock_test "server" "${VSOCK_CID}" "${TEST_HOST_PORT_LISTENER}"= ; then + if ! host_vsock_test "init_ns" "server" "${VSOCK_CID}" "${TEST_HOST_PORT_= LISTENER}"; then return "${KSFT_FAIL}" fi =20 - if ! vm_vsock_test "10.0.2.2" 2 "${TEST_HOST_PORT_LISTENER}"; then + if ! vm_vsock_test "init_ns" "10.0.2.2" 2 "${TEST_HOST_PORT_LISTENER}"; t= hen return "${KSFT_FAIL}" fi =20 @@ -495,13 +530,14 @@ test_vm_client_host_server() { test_vm_loopback() { local port=3D60000 # non-forwarded local port =20 - vm_ssh -- modprobe vsock_loopback &> /dev/null || : + vm_ssh "init_ns" -- modprobe vsock_loopback &> /dev/null || : =20 - if ! vm_vsock_test "server" 1 "${port}"; then + if ! vm_vsock_test "init_ns" "server" 1 "${port}"; then return "${KSFT_FAIL}" fi =20 - if ! vm_vsock_test "127.0.0.1" 1 "${port}"; then + + if ! vm_vsock_test "init_ns" "127.0.0.1" 1 "${port}"; then return "${KSFT_FAIL}" fi =20 @@ -559,8 +595,8 @@ run_shared_vm_test() { =20 host_oops_cnt_before=3D$(dmesg | grep -c -i 'Oops') host_warn_cnt_before=3D$(dmesg --level=3Dwarn | grep -c -i 'vsock') - vm_oops_cnt_before=3D$(vm_ssh -- dmesg | grep -c -i 'Oops') - vm_warn_cnt_before=3D$(vm_ssh -- dmesg --level=3Dwarn | grep -c -i 'vsock= ') + vm_oops_cnt_before=3D$(vm_ssh "init_ns" -- dmesg | grep -c -i 'Oops') + vm_warn_cnt_before=3D$(vm_ssh "init_ns" -- dmesg --level=3Dwarn | grep -c= -i 'vsock') =20 name=3D$(echo "${1}" | awk '{ print $1 }') eval test_"${name}" @@ -577,14 +613,14 @@ run_shared_vm_test() { echo "FAIL: kernel warning detected on host" | log_host rc=3D$KSFT_FAIL fi - - vm_oops_cnt_after=3D$(vm_ssh -- dmesg | grep -i 'Oops' | wc -l) + vm_oops_cnt_after=3D$(vm_ssh "init_ns" -- dmesg | grep -c -i 'Oops') + vm_oops_cnt_after=3D$(vm_ssh "init_ns" -- dmesg | grep -i 'Oops' | wc -l) if [[ ${vm_oops_cnt_after} -gt ${vm_oops_cnt_before} ]]; then echo "FAIL: kernel oops detected on vm" | log_host rc=3D$KSFT_FAIL fi =20 - vm_warn_cnt_after=3D$(vm_ssh -- dmesg --level=3Dwarn | grep -c -i 'vsock') + vm_warn_cnt_after=3D$(vm_ssh "init_ns" -- dmesg --level=3Dwarn | grep -c = -i 'vsock') if [[ ${vm_warn_cnt_after} -gt ${vm_warn_cnt_before} ]]; then echo "FAIL: kernel warning detected on vm" | log_host rc=3D$KSFT_FAIL @@ -630,8 +666,8 @@ cnt_total=3D0 if shared_vm_tests_requested "${ARGS[@]}"; then log_host "Booting up VM" pidfile=3D"$(create_pidfile)" - vm_start "${pidfile}" - vm_wait_for_ssh + vm_start "${pidfile}" "init_ns" + vm_wait_for_ssh "init_ns" log_host "VM booted up" =20 run_shared_vm_tests "${ARGS[@]}" --=20 2.47.3 From nobody Tue Dec 2 02:43:45 2025 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 96DE631A54B for ; Tue, 18 Nov 2025 02:00:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763431243; cv=none; b=k1yjkiy8av5i7KibH0kvGxyrT1f0iMVnvve0hTpnSqh4XSRgSjtOQApvNk1Al5uwOYmE8lx4Ah15L3yNe4iS2f11vwYBp9iTDpF1cyjFXuEeE/We3VqurJPnkk8uscvY+F/wse7CpE6T3V6AcOdanrCYaU8SxO9mQZ2eP8eN+3g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763431243; c=relaxed/simple; bh=EDKpHynwm2IXJnYAfSr1B0rR5fIaubQz+sZgrbvmdXc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=G2kCaNrUiU9yQOMhDu9M6vW9NrwVjP937NORzTPo6MF8JMkwYDKEl+XV2YwgdyIkWxwAe/bxu+rZz8cg8uL2mFkC3VS7TRHGGR52oc6qB6u6vdlH36ItLsRHQoaF/FUKJEb4jdWBSnCyeRl6S2z6xBKiY1UG9uoYrewnOsan2Fo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=OgTnRPqS; arc=none smtp.client-ip=209.85.214.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="OgTnRPqS" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-297d4a56f97so54857015ad.1 for ; Mon, 17 Nov 2025 18:00:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763431238; x=1764036038; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=m0+GxkorM2LcDX2VPjNLPm8evYFZWtz/czQUh0wFOVY=; b=OgTnRPqSHio44X3Hy7i0f9goigph+N/6kjbcDPDXD963/Wu/rIdLoE9IDMZztJpHqa f8/nli4bUm6L5FshB1Sw36mFUj+Nc6yVcmbcROhdBo+uYhoO20+oSJWpPNefChDt68H6 niIQHvbEl9ISqI2lgyQd+J6VTxdQewfLVFSKX1Au3HhNNJ73dkEh3Hyew0KUgZShjMUZ /JFx9BfENmmfkkm5BhIDRcpql1+7qlSapSDccom7h/Slp3mgcrwFiNrIo7fG1VGspcfv gywlk0xZAIIUWzDlEm3oSTxFjEQhZualDxve4Xir1isremd7zhlHI9c7/au7nV9+9jAc 9T1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763431238; x=1764036038; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=m0+GxkorM2LcDX2VPjNLPm8evYFZWtz/czQUh0wFOVY=; b=tBZHzqBOGGVZK9rjHk9c/UYdpNIQ+Rmm5TChjAbwvXu1J0vHSLhy1w1tlwMTAzPCSy uO7CiLLo/jVy7XLTUARuZi+zK5OyFfiEFVxuW/c9M+Iyk6CE7ryaBfoW8Tuwfv2OTRGN vX3gxUBaUk4jrzPfaVq3cmBp+KFaCViM9h5Z2Bd/uf+Y7rEkZsczWUavvETn9i7VZJoc /bzGvvT+YBJcmvNIvF1w+Xd49s7eL9m6UhznXR43js7NeaL7nIyL9DAvBqKvrpJ/k74S zTUzH2bY6c74MqH31ytMThJoaZB4Mtvg4PFSoQi1PG+1pVSDK1hxM9oOn/CF2WuP6QWU vHnw== X-Gm-Message-State: AOJu0Yx33tzlXnJpuo5t/rw+JwahjKMlLj1f9GuAm1uZREo909iL/I4x mNmbNNG5icdOAsTn5JU7Z3O1LoMroTKVIgXL0Eqwr49ej/311v8x+KN7 X-Gm-Gg: ASbGnctEq2OTYXOClh+9cBIKI/AFSrXmNKFwzH3Vs33kohkeEUFVYdhSCzDgSmTL7vY NIlbjD+TDrzpXKmXQdcBddw5SEU0b+qRGZtfL/5vm6r+BFa7ze/p/2WL5tbweqbKRKnLSggdIvU Rw/VMnvdk2RsaKD1CVyME6+xteyYtR5ciNfP3AFWZq6vliBygL7/pAUZH9LhJT+M0KWVr2RZQpL /SW2Z0UqzmPcbPss+VrSJYGRl7Vv94khT3DzU3V052FPLo4Rt2FoX4+HTa0LWFWryDQI1ogqeJO gunj3eR4SbLo/1txB+rMy6TkwWpYjKVYk6PxIdx9hvyRqTX/6Fl3wF7UpLVYd/VuI1Ho1zUnTmU 178YD9lZvjfGBSXQD5klkovqXrzH84DxTAqMztqTEpT2307A91AGVg7yaRpciS5QafvDov+6ypP 7NqtJakiK94u1/D8+UmeY= X-Google-Smtp-Source: AGHT+IG3X3koA5shIYbqXmwRDbexfix9ySOWhwJdVCEPNOxDdOG2tppTsbE7uvW6dejmpqz5SceW0g== X-Received: by 2002:a17:903:11c6:b0:297:e1f5:191b with SMTP id d9443c01a7336-2986a6abf3cmr183038875ad.11.1763431238087; Mon, 17 Nov 2025 18:00:38 -0800 (PST) Received: from localhost ([2a03:2880:2ff:9::]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2985c2346c3sm153027005ad.4.2025.11.17.18.00.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Nov 2025 18:00:37 -0800 (PST) From: Bobby Eshleman Date: Mon, 17 Nov 2025 18:00:31 -0800 Subject: [PATCH net-next v10 08/11] selftests/vsock: add tests for proc sys vsock ns_mode Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251117-vsock-vmtest-v10-8-df08f165bf3e@meta.com> References: <20251117-vsock-vmtest-v10-0-df08f165bf3e@meta.com> In-Reply-To: <20251117-vsock-vmtest-v10-0-df08f165bf3e@meta.com> To: Stefano Garzarella , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Stefan Hajnoczi , "Michael S. Tsirkin" , Jason Wang , =?utf-8?q?Eugenio_P=C3=A9rez?= , Xuan Zhuo , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Bryan Tan , Vishnu Dasa , Broadcom internal kernel review list , Shuah Khan Cc: linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, netdev@vger.kernel.org, kvm@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kselftest@vger.kernel.org, Sargun Dhillon , Bobby Eshleman , berrange@redhat.com, Bobby Eshleman X-Mailer: b4 0.14.3 From: Bobby Eshleman Add tests for the /proc/sys/net/vsock/ns_mode interface. Namely, that it accepts "global" and "local" strings and enforces a write-once policy. Start a convention of commenting the test name over the test description. Add test name comments over test descriptions that existed before this convention. Add a check_netns() function that checks if the test requires namespaces and if the current kernel supports namespaces. Skip tests that require namespaces if the system does not have namespace support. Add a test to verify that guest VMs with an active G2H transport (virtio-vsock) cannot set namespace mode to 'local'. This validates the mutual exclusion between G2H transports and LOCAL mode. This patch is the first to add tests that do *not* re-use the same shared VM. For that reason, it adds a run_tests() function to run these tests and filter out the shared VM tests. Signed-off-by: Bobby Eshleman Suggested-by: Sargun Dhillon --- Changes in v10: - Remove extraneous add_namespaces/del_namespaces calls. - Rename run_tests() to run_ns_tests() since it is designed to only run ns tests. Changes in v9: - add test ns_vm_local_mode_rejected to check that guests cannot use local mode --- tools/testing/selftests/vsock/vmtest.sh | 140 ++++++++++++++++++++++++++++= +++- 1 file changed, 138 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/vsock/vmtest.sh b/tools/testing/selfte= sts/vsock/vmtest.sh index 1a7c810f282f..86483249f490 100755 --- a/tools/testing/selftests/vsock/vmtest.sh +++ b/tools/testing/selftests/vsock/vmtest.sh @@ -41,14 +41,40 @@ readonly KERNEL_CMDLINE=3D"\ virtme.ssh virtme_ssh_channel=3Dtcp virtme_ssh_user=3D$USER \ " readonly LOG=3D$(mktemp /tmp/vsock_vmtest_XXXX.log) -readonly TEST_NAMES=3D(vm_server_host_client vm_client_host_server vm_loop= back) +readonly TEST_NAMES=3D( + vm_server_host_client + vm_client_host_server + vm_loopback + ns_host_vsock_ns_mode_ok + ns_host_vsock_ns_mode_write_once_ok + ns_vm_local_mode_rejected +) readonly TEST_DESCS=3D( + # vm_server_host_client "Run vsock_test in server mode on the VM and in client mode on the host." + + # vm_client_host_server "Run vsock_test in client mode on the VM and in server mode on the host." + + # vm_loopback "Run vsock_test using the loopback transport in the VM." + + # ns_host_vsock_ns_mode_ok + "Check /proc/sys/net/vsock/ns_mode strings on the host." + + # ns_host_vsock_ns_mode_write_once_ok + "Check /proc/sys/net/vsock/ns_mode is write-once on the host." + + # ns_vm_local_mode_rejected + "Test that guest VM with G2H transport cannot set namespace mode to 'loca= l'" ) =20 -readonly USE_SHARED_VM=3D(vm_server_host_client vm_client_host_server vm_l= oopback) +readonly USE_SHARED_VM=3D( + vm_server_host_client + vm_client_host_server + vm_loopback + ns_vm_local_mode_rejected +) readonly NS_MODES=3D("local" "global") =20 VERBOSE=3D0 @@ -205,6 +231,20 @@ check_deps() { fi } =20 +check_netns() { + local tname=3D$1 + + # If the test requires NS support, check if NS support exists + # using /proc/self/ns + if [[ "${tname}" =3D~ ^ns_ ]] && + [[ ! -e /proc/self/ns ]]; then + log_host "No NS support detected for test ${tname}" + return 1 + fi + + return 0 +} + check_vng() { local tested_versions local version @@ -503,6 +543,32 @@ log_guest() { LOG_PREFIX=3Dguest log "$@" } =20 +test_ns_host_vsock_ns_mode_ok() { + for mode in "${NS_MODES[@]}"; do + if ! ns_set_mode "${mode}0" "${mode}"; then + return "${KSFT_FAIL}" + fi + done + + return "${KSFT_PASS}" +} + +test_ns_host_vsock_ns_mode_write_once_ok() { + for mode in "${NS_MODES[@]}"; do + local ns=3D"${mode}0" + if ! ns_set_mode "${ns}" "${mode}"; then + return "${KSFT_FAIL}" + fi + + # try writing again and expect failure + if ns_set_mode "${ns}" "${mode}"; then + return "${KSFT_FAIL}" + fi + done + + return "${KSFT_PASS}" +} + test_vm_server_host_client() { if ! vm_vsock_test "init_ns" "server" 2 "${TEST_GUEST_PORT}"; then return "${KSFT_FAIL}" @@ -544,6 +610,26 @@ test_vm_loopback() { return "${KSFT_PASS}" } =20 +test_ns_vm_local_mode_rejected() { + # Guest VMs have a G2H transport (virtio-vsock) active, so they + # should not be able to set namespace mode to 'local'. + # This test verifies that the sysctl write fails as expected. + + # Try to set local mode in the guest's init_ns + if vm_ssh init_ns "echo local | tee /proc/sys/net/vsock/ns_mode &>/dev/nu= ll"; then + return "${KSFT_FAIL}" + fi + + # Verify mode is still 'global' + local mode + mode=3D$(vm_ssh init_ns "cat /proc/sys/net/vsock/ns_mode") + if [[ "${mode}" !=3D "global" ]]; then + return "${KSFT_FAIL}" + fi + + return "${KSFT_PASS}" +} + shared_vm_test() { local tname =20 @@ -576,6 +662,11 @@ run_shared_vm_tests() { continue fi =20 + if ! check_netns "${arg}"; then + check_result "${KSFT_SKIP}" "${arg}" + continue + fi + run_shared_vm_test "${arg}" check_result "$?" "${arg}" done @@ -629,6 +720,49 @@ run_shared_vm_test() { return "${rc}" } =20 +run_ns_tests() { + for arg in "${ARGS[@]}"; do + if shared_vm_test "${arg}"; then + continue + fi + + if ! check_netns "${arg}"; then + check_result "${KSFT_SKIP}" "${arg}" + continue + fi + + add_namespaces + + name=3D$(echo "${arg}" | awk '{ print $1 }') + log_host "Executing test_${name}" + + host_oops_before=3D$(dmesg 2>/dev/null | grep -c -i 'Oops') + host_warn_before=3D$(dmesg --level=3Dwarn 2>/dev/null | grep -c -i 'vsoc= k') + eval test_"${name}" + rc=3D$? + + host_oops_after=3D$(dmesg 2>/dev/null | grep -c -i 'Oops') + if [[ "${host_oops_after}" -gt "${host_oops_before}" ]]; then + echo "FAIL: kernel oops detected on host" | log_host + check_result "${KSFT_FAIL}" "${name}" + del_namespaces + continue + fi + + host_warn_after=3D$(dmesg --level=3Dwarn 2>/dev/null | grep -c -i 'vsock= ') + if [[ "${host_warn_after}" -gt "${host_warn_before}" ]]; then + echo "FAIL: kernel warning detected on host" | log_host + check_result "${KSFT_FAIL}" "${name}" + del_namespaces + continue + fi + + check_result "${rc}" "${name}" + + del_namespaces + done +} + BUILD=3D0 QEMU=3D"qemu-system-$(uname -m)" =20 @@ -674,6 +808,8 @@ if shared_vm_tests_requested "${ARGS[@]}"; then terminate_pidfiles "${pidfile}" fi =20 +run_ns_tests "${ARGS[@]}" + echo "SUMMARY: PASS=3D${cnt_pass} SKIP=3D${cnt_skip} FAIL=3D${cnt_fail}" echo "Log: ${LOG}" =20 --=20 2.47.3 From nobody Tue Dec 2 02:43:45 2025 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 25438324B2C for ; Tue, 18 Nov 2025 02:00:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763431244; cv=none; b=hreHlSOkgENlQNhauH95YShcAYUJw8dY5IJwacCOb2fRKvxnvjAmxZ8uBX+YYPDuMIJqii31lq4IBJjhP+9Vk5Tmb0bsrQaN/lGeTkXqcgIYy3wNGm7eQcf8l5Uf69hdeJRRTjXih0iX4RPfHmqi60IbZ5Z4KmjNGmJJnFsZAAE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763431244; c=relaxed/simple; bh=3zstnYgrjjZTcL85zX/bwEXTpc6YD365WK9ireof2ic=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=F0qvaM56b7IFxl+5zv01G+wJ2B2KaGJFfr0v38mbSO0Q0eZtRtO4kJttvz3zTk0AKzA2FPXM78PkCp3M64Y9LH4dfpjIkMAyQXPGvJYP4CW2yoGwm/npgEN4YcDSYuCJ7mDXMhk9SegWrQv5AaRywRiax7lC9zx3q5TXI/SkkG8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=eEuuUKxL; arc=none smtp.client-ip=209.85.216.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="eEuuUKxL" Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-34372216275so5389479a91.2 for ; Mon, 17 Nov 2025 18:00:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763431239; x=1764036039; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=Qhvm+dnH460Tt7FFn5Fd2rcGuVIIsnu9XpGRtebTbS8=; b=eEuuUKxL88H+xzP55NnbfD0ZfoPSM7SRsjmUxTXzBKeZWGRe3YuCFT7Ow3SHCJu9c/ 3LvT8dHQDa1umpIX81H9AGnkuHo3qo2ndLFYnupHNUBpP6PI/f0TSeegqQ/JBGxSmy36 +SOflAfMc4ddC+8qcaxNkkhfvDtaVP6xL+93AXfhgcR+7Ioy6VmHT18vjUK7hoAu5Ffj qX3tfHBKicu8PP5UWSuUOt70kcswnMlYtdmqZgL5aGAhymOT7dhRjHpzCqJzYYL1EnHA uFksu+WpZdSv4l5isS8OicvDeF3HSwB1ejJ+CF8NY/A6CQWqJAxhPhp15eqZYa1IG4QQ 7mYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763431239; x=1764036039; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=Qhvm+dnH460Tt7FFn5Fd2rcGuVIIsnu9XpGRtebTbS8=; b=LQOBIx7Idqn5vJRZrDxTNCnuKKX7U1nVK6uVPwtxzP+mwluW3tb3tOY9bsiaBB3m/t OQ0ZLgc6nUjPcETW4UlaKOPINkzZdx0P3TBTytjsKjtOmXgJBYCKLuIupqHC6PZ4Qvsc BynRVgzcK7+b8IEsp5zY4Fq57SJRJb90T99eqnsaN6kdsALoDM3hEK9XP1bXo/Vhmn2U JTRI1V97yCzBddvMoUNKeSzVODW/driMeqNejsuZzXV21gpqHdK4BZChxhSqpVN0pKc2 CcB/UrFc6JA8eaUip4uCndLqU0KeJOOh2H3bVP+hWK11u/AexWzT9I+Tum921WJOGK6O 1A9g== X-Gm-Message-State: AOJu0YxrObTr4CmOs4n1NOi+QgM1crf2OCPNKau+8Vkgqpnp/EvUC+i5 FueaiwRJR6HskjG2stTdmXv+5+c/yVAAruwT0uvNj/yW7ZSdxpoLjEoW X-Gm-Gg: ASbGnctgZa1mJme3F5dqIK5MWyh7zKAhg1eSxIQ/2i5r5SWaadD9QdpiJEg8VkS/d2S m7PlK89pZYXAXVpvcrZ7Pej4w5kKZPgVyHGdqa+iOUusgEX6p5FLGTzG485wZA7N8YjQDGWo514 RvE4TSX3xcFzzWi5m+5AYgX6Ke5RLp1img6zNhYazoYkjrfERtwZyJzcdvyt3iJ0VpavRt+JUPs Qgr7OLXPRYUvd07nnk164YXi1zDu0kHY/zM9GwHyEvoy2YUjlUTELMhs46zneN9naTWdKyFuH5y brAVefQTdMc7LUtcLEHPI0+yvFbP/iJxLFoiBV6x/GsKROBgy3RRdQ4tcNBwSk/NXxIebq7RstO fXFiTIznFErWYNzCrrsd//2JVjGqPAPK+TxFOlJBkKy/QCsWQUNmxmJTC1+VFkLsgOXvzaWnBl8 L6B5ClGCsUl9d+jer3HHoyXHF4fYljgQ== X-Google-Smtp-Source: AGHT+IFqV1GrHr3XrPIYmCcvEa4oEIM7omi6fW12jvd7AtWF00Di4zCzOTV0RIbzxFbKb2Lo+nb8Qw== X-Received: by 2002:a17:90b:2f08:b0:33b:dec9:d9aa with SMTP id 98e67ed59e1d1-343fa7493admr15317741a91.25.1763431239229; Mon, 17 Nov 2025 18:00:39 -0800 (PST) Received: from localhost ([2a03:2880:2ff:1::]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-345b0202b16sm369741a91.2.2025.11.17.18.00.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Nov 2025 18:00:38 -0800 (PST) From: Bobby Eshleman Date: Mon, 17 Nov 2025 18:00:32 -0800 Subject: [PATCH net-next v10 09/11] selftests/vsock: add namespace tests for CID collisions Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251117-vsock-vmtest-v10-9-df08f165bf3e@meta.com> References: <20251117-vsock-vmtest-v10-0-df08f165bf3e@meta.com> In-Reply-To: <20251117-vsock-vmtest-v10-0-df08f165bf3e@meta.com> To: Stefano Garzarella , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Stefan Hajnoczi , "Michael S. Tsirkin" , Jason Wang , =?utf-8?q?Eugenio_P=C3=A9rez?= , Xuan Zhuo , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Bryan Tan , Vishnu Dasa , Broadcom internal kernel review list , Shuah Khan Cc: linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, netdev@vger.kernel.org, kvm@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kselftest@vger.kernel.org, Sargun Dhillon , Bobby Eshleman , berrange@redhat.com, Bobby Eshleman X-Mailer: b4 0.14.3 From: Bobby Eshleman Add tests to verify CID collision rules across different vsock namespace modes. 1. Two VMs with the same CID cannot start in different global namespaces (ns_global_same_cid_fails) 2. Two VMs with the same CID can start in different local namespaces (ns_local_same_cid_ok) 3. VMs with the same CID can coexist when one is in a global namespace and another is in a local namespace (ns_global_local_same_cid_ok and ns_local_global_same_cid_ok) The tests ns_global_local_same_cid_ok and ns_local_global_same_cid_ok make sure that ordering does not matter. The tests use a shared helper function namespaces_can_boot_same_cid() that attempts to start two VMs with identical CIDs in the specified namespaces and verifies whether VM initialization failed or succeeded. Signed-off-by: Bobby Eshleman Suggested-by: Sargun Dhillon --- tools/testing/selftests/vsock/vmtest.sh | 73 +++++++++++++++++++++++++++++= ++++ 1 file changed, 73 insertions(+) diff --git a/tools/testing/selftests/vsock/vmtest.sh b/tools/testing/selfte= sts/vsock/vmtest.sh index 86483249f490..a8bf78a5075d 100755 --- a/tools/testing/selftests/vsock/vmtest.sh +++ b/tools/testing/selftests/vsock/vmtest.sh @@ -48,6 +48,10 @@ readonly TEST_NAMES=3D( ns_host_vsock_ns_mode_ok ns_host_vsock_ns_mode_write_once_ok ns_vm_local_mode_rejected + ns_global_same_cid_fails + ns_local_same_cid_ok + ns_global_local_same_cid_ok + ns_local_global_same_cid_ok ) readonly TEST_DESCS=3D( # vm_server_host_client @@ -67,6 +71,17 @@ readonly TEST_DESCS=3D( =20 # ns_vm_local_mode_rejected "Test that guest VM with G2H transport cannot set namespace mode to 'loca= l'" + # ns_global_same_cid_fails + "Check QEMU fails to start two VMs with same CID in two different global = namespaces." + + # ns_local_same_cid_ok + "Check QEMU successfully starts two VMs with same CID in two different lo= cal namespaces." + + # ns_global_local_same_cid_ok + "Check QEMU successfully starts one VM in a global ns and then another VM= in a local ns with the same CID." + + # ns_local_global_same_cid_ok + "Check QEMU successfully starts one VM in a local ns and then another VM = in a global ns with the same CID." ) =20 readonly USE_SHARED_VM=3D( @@ -553,6 +568,64 @@ test_ns_host_vsock_ns_mode_ok() { return "${KSFT_PASS}" } =20 +namespaces_can_boot_same_cid() { + local ns0=3D$1 + local ns1=3D$2 + local pidfile1 pidfile2 + local rc + + pidfile1=3D"$(create_pidfile)" + vm_start "${pidfile1}" "${ns0}" + + pidfile2=3D"$(create_pidfile)" + vm_start "${pidfile2}" "${ns1}" + + rc=3D$? + terminate_pidfiles "${pidfile1}" "${pidfile2}" + + return "${rc}" +} + +test_ns_global_same_cid_fails() { + init_namespaces + + if namespaces_can_boot_same_cid "global0" "global1"; then + return "${KSFT_FAIL}" + fi + + return "${KSFT_PASS}" +} + +test_ns_local_global_same_cid_ok() { + init_namespaces + + if namespaces_can_boot_same_cid "local0" "global0"; then + return "${KSFT_PASS}" + fi + + return "${KSFT_FAIL}" +} + +test_ns_global_local_same_cid_ok() { + init_namespaces + + if namespaces_can_boot_same_cid "global0" "local0"; then + return "${KSFT_PASS}" + fi + + return "${KSFT_FAIL}" +} + +test_ns_local_same_cid_ok() { + init_namespaces + + if namespaces_can_boot_same_cid "local0" "local0"; then + return "${KSFT_FAIL}" + fi + + return "${KSFT_PASS}" +} + test_ns_host_vsock_ns_mode_write_once_ok() { for mode in "${NS_MODES[@]}"; do local ns=3D"${mode}0" --=20 2.47.3 From nobody Tue Dec 2 02:43:45 2025 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6C0D11F7569 for ; Tue, 18 Nov 2025 02:00:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763431247; cv=none; b=GA6G1LjUriiyL0HMkBpCpY+Hrnfb3yhqJ6fPekjzYyOa0w4yiU2KBR9z69as+E6XOCymcN7dCLqoKP7dr/DhHTpq7zAOLZB2yz2HaCmDHJ0VBLCnkbxvxPwbjBynO7/wB3EL4066G5R4M0/4qur1tOfPrRf4JXE8mnZse+YgszE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763431247; c=relaxed/simple; bh=T4Q0xSf6R3podxO4R+Ma9r50o0a0RBOgmhxddA+GpUw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ZLip6ctJbKg8y0B1QrBOfxlIu+rtXtjwsde8ZR8XR4gO7jOUbWQru0LS3XcE3ck59zTSnyfd6wR5VWlvd5LBmpXJmq3g8jtX15GNcKeXEu4F2soMyUaZ9KY4V4dgOWDLcM0RDpkYFSs0sGJC/LKRrXDW4TFJhlrOKYH3mti/dcw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=IOjrw6JY; arc=none smtp.client-ip=209.85.214.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="IOjrw6JY" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-298456bb53aso55842615ad.0 for ; Mon, 17 Nov 2025 18:00:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763431240; x=1764036040; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=RSPkUpgEn2Y0MP2KCB5fV1UyMT32r8AXV208PKunv0k=; b=IOjrw6JYlxLkTnU96UvEHuKcrKFCkVgzkMyeGsnsJB07vk7sib8swFqaVhCU93Nrdk JVtxVRpPOtoaphn2CTjvhIPsPw/f1K92wJualg2Jcz4MOLm4Q1cR9kWHGIuQPhwjb3HF EdoWHMdNRXII2lcV4JmUWcmnUxn9EzRDwEogqCuOloGhOT1PBkRwIM2mq1Gp+QqGlUBH ZyQ+SH72DqeHSqN5hReEwrKIF4pkrk5muNuVScxUjmKf6uEVSSwYbhMDauJu4mA+rZIl ODbfCz6BYk24S/t16HfUsiXSFtGdc9ZGUsM20f7/u28TXa1xVcfH5aKmyRBRGF4OjGla 0huA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763431240; x=1764036040; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=RSPkUpgEn2Y0MP2KCB5fV1UyMT32r8AXV208PKunv0k=; b=dEP82130Qjv5n7M2EDCwyU0KbyrG45oUbfaQEoP4p/b7vNTSvPZk8LN6/XQKpEBldl tkvX1GTlBUmXJpWUprRyxe9q/buK1FYVCwtOZWM30QdByyrAna34mAj5jIhQEt+Knslk NBeBYRSMguiJ4gJEIhmhx1p9ZQOGinazn9sXxSO1g487ZJMPFHRP5gDgjXnSj39/RuXk 6Ag6VJ4ptXzg8v9bGoFWGhTBNZhO2pe2EHbcXf1cuPPl7mMe2G8Uec3xv8r4VtBRd0/U dHVd5wLi5Z0Z4h6HE9RGRZKUgy3fCCBVKOykbKAn7dGWVTEW39qK2IkoZ3ySF3H5mDYf Cx9w== X-Gm-Message-State: AOJu0YyGzvJ9KpNZxqEEOH0nJuEsXv2WbmUEZH5jRaqejVzJLW/G26A1 PrcY6Q0hPngU0xfnIlMIe040JL1Ia2ijcHE4szAtLj8KurjzFe6weqaA X-Gm-Gg: ASbGncugHlVCbLfc3QFpy22qxS9EnLOXZOj8nBPYQEnHn417ESLpYNBoqNVmG3aYB5V tP8hSZVKgIluWcVbom2B4tLFPF+ecmO7XgUo085D1ze852OWzk/2R7ckY3hqR+/9dPsflPADjFi JyouJ4d3IISmC2Weo5W53nhbDlHe+hMwK4e69Fer+SM8oMUym8ZX4i7mxQCBlGX1JF6csV3vq8R HsBp1izP6rf96dnCq+ObQ0+iUdvtEC+VBNRD2ML7/qUA7yG5+7l/XxFZgWYyfCjKQscgNQYLfrE GkSRtjGv4jIQT+ch1UfqCYFfP7C5/MAIj/kTu3dwl7AQkSRSOGGY3LusM4igYP/a5OJOloR37lk ZDUgh6caKKZxWnDMyBGQVSpuUodfPC0w15kJ5RRj0RmA6ZAdaqHMGmCMK2r5QizAFVFp6XLTtOP gKthakLsaHaokRpzdnwgndAeSUIlNYyg== X-Google-Smtp-Source: AGHT+IHAx36So0bPcvUrc+TUQe4tuwQofa8paulwJrfKRSnfE2RklDk+Wb1IvphsE5+ZCVqfKKehuQ== X-Received: by 2002:a17:902:f687:b0:295:82c6:dac3 with SMTP id d9443c01a7336-2986a7416e4mr174165575ad.32.1763431240168; Mon, 17 Nov 2025 18:00:40 -0800 (PST) Received: from localhost ([2a03:2880:2ff:9::]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2985c2b1088sm154893845ad.57.2025.11.17.18.00.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Nov 2025 18:00:39 -0800 (PST) From: Bobby Eshleman Date: Mon, 17 Nov 2025 18:00:33 -0800 Subject: [PATCH net-next v10 10/11] selftests/vsock: add tests for host <-> vm connectivity with namespaces Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251117-vsock-vmtest-v10-10-df08f165bf3e@meta.com> References: <20251117-vsock-vmtest-v10-0-df08f165bf3e@meta.com> In-Reply-To: <20251117-vsock-vmtest-v10-0-df08f165bf3e@meta.com> To: Stefano Garzarella , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Stefan Hajnoczi , "Michael S. Tsirkin" , Jason Wang , =?utf-8?q?Eugenio_P=C3=A9rez?= , Xuan Zhuo , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Bryan Tan , Vishnu Dasa , Broadcom internal kernel review list , Shuah Khan Cc: linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, netdev@vger.kernel.org, kvm@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kselftest@vger.kernel.org, Sargun Dhillon , Bobby Eshleman , berrange@redhat.com, Bobby Eshleman X-Mailer: b4 0.14.3 From: Bobby Eshleman Add tests to validate namespace correctness using vsock_test and socat. The vsock_test tool is used to validate expected success tests, but socat is used for expected failure tests. socat is used to ensure that connections are rejected outright instead of failing due to some other socket behavior (as tested in vsock_test). Additionally, socat is already required for tunneling TCP traffic from vsock_test. Using only one of the vsock_test tests like 'test_stream_client_close_client' would have yielded a similar result, but doing so wouldn't remove the socat dependency. Additionally, check for the dependency socat. socat needs special handling beyond just checking if it is on the path because it must be compiled with support for both vsock and unix. The function check_socat() checks that this support exists. Add more padding to test name printf strings because the tests added in this patch would otherwise overflow. Add vm_dmesg_start() and vm_dmesg_check() to encapsulate checking dmesg for oops and warnings. Signed-off-by: Bobby Eshleman Suggested-by: Sargun Dhillon --- Changes in v10: - add vm_dmesg_start() and vm_dmesg_check() Changes in v9: - consistent variable quoting --- tools/testing/selftests/vsock/vmtest.sh | 558 ++++++++++++++++++++++++++++= +++- 1 file changed, 556 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/vsock/vmtest.sh b/tools/testing/selfte= sts/vsock/vmtest.sh index a8bf78a5075d..9c12c1bd1edc 100755 --- a/tools/testing/selftests/vsock/vmtest.sh +++ b/tools/testing/selftests/vsock/vmtest.sh @@ -7,6 +7,7 @@ # * virtme-ng # * busybox-static (used by virtme-ng) # * qemu (used by virtme-ng) +# * socat # # shellcheck disable=3DSC2317,SC2119 =20 @@ -52,6 +53,19 @@ readonly TEST_NAMES=3D( ns_local_same_cid_ok ns_global_local_same_cid_ok ns_local_global_same_cid_ok + ns_diff_global_host_connect_to_global_vm_ok + ns_diff_global_host_connect_to_local_vm_fails + ns_diff_global_vm_connect_to_global_host_ok + ns_diff_global_vm_connect_to_local_host_fails + ns_diff_local_host_connect_to_local_vm_fails + ns_diff_local_vm_connect_to_local_host_fails + ns_diff_global_to_local_loopback_local_fails + ns_diff_local_to_global_loopback_fails + ns_diff_local_to_local_loopback_fails + ns_diff_global_to_global_loopback_ok + ns_same_local_loopback_ok + ns_same_local_host_connect_to_local_vm_ok + ns_same_local_vm_connect_to_local_host_ok ) readonly TEST_DESCS=3D( # vm_server_host_client @@ -82,6 +96,45 @@ readonly TEST_DESCS=3D( =20 # ns_local_global_same_cid_ok "Check QEMU successfully starts one VM in a local ns and then another VM = in a global ns with the same CID." + + # ns_diff_global_host_connect_to_global_vm_ok + "Run vsock_test client in global ns with server in VM in another global n= s." + + # ns_diff_global_host_connect_to_local_vm_fails + "Run socat to test a process in a global ns fails to connect to a VM in a= local ns." + + # ns_diff_global_vm_connect_to_global_host_ok + "Run vsock_test client in VM in a global ns with server in another global= ns." + + # ns_diff_global_vm_connect_to_local_host_fails + "Run socat to test a VM in a global ns fails to connect to a host process= in a local ns." + + # ns_diff_local_host_connect_to_local_vm_fails + "Run socat to test a host process in a local ns fails to connect to a VM = in another local ns." + + # ns_diff_local_vm_connect_to_local_host_fails + "Run socat to test a VM in a local ns fails to connect to a host process = in another local ns." + + # ns_diff_global_to_local_loopback_local_fails + "Run socat to test a loopback vsock in a global ns fails to connect to a = vsock in a local ns." + + # ns_diff_local_to_global_loopback_fails + "Run socat to test a loopback vsock in a local ns fails to connect to a v= sock in a global ns." + + # ns_diff_local_to_local_loopback_fails + "Run socat to test a loopback vsock in a local ns fails to connect to a v= sock in another local ns." + + # ns_diff_global_to_global_loopback_ok + "Run socat to test a loopback vsock in a global ns successfully connects = to a vsock in another global ns." + + # ns_same_local_loopback_ok + "Run socat to test a loopback vsock in a local ns successfully connects t= o a vsock in the same ns." + + # ns_same_local_host_connect_to_local_vm_ok + "Run vsock_test client in a local ns with server in VM in same ns." + + # ns_same_local_vm_connect_to_local_host_ok + "Run vsock_test client in VM in a local ns with server in same ns." ) =20 readonly USE_SHARED_VM=3D( @@ -113,7 +166,7 @@ usage() { for ((i =3D 0; i < ${#TEST_NAMES[@]}; i++)); do name=3D${TEST_NAMES[${i}]} desc=3D${TEST_DESCS[${i}]} - printf "\t%-35s%-35s\n" "${name}" "${desc}" + printf "\t%-55s%-35s\n" "${name}" "${desc}" done echo =20 @@ -232,7 +285,7 @@ check_args() { } =20 check_deps() { - for dep in vng ${QEMU} busybox pkill ssh; do + for dep in vng ${QEMU} busybox pkill ssh socat; do if [[ ! -x $(command -v "${dep}") ]]; then echo -e "skip: dependency ${dep} not found!\n" exit "${KSFT_SKIP}" @@ -283,6 +336,20 @@ check_vng() { fi } =20 +check_socat() { + local support_string + + support_string=3D"$(socat -V)" + + if [[ "${support_string}" !=3D *"WITH_VSOCK 1"* ]]; then + die "err: socat is missing vsock support" + fi + + if [[ "${support_string}" !=3D *"WITH_UNIX 1"* ]]; then + die "err: socat is missing unix support" + fi +} + handle_build() { if [[ ! "${BUILD}" -eq 1 ]]; then return @@ -331,6 +398,14 @@ terminate_pidfiles() { done } =20 +terminate_pids() { + local pid + + for pid in "$@"; do + kill -SIGTERM "${pid}" &>/dev/null || : + done +} + vm_start() { local pidfile=3D$1 local ns=3D$2 @@ -444,6 +519,40 @@ host_wait_for_listener() { fi } =20 +vm_dmesg_oops_count() { + local ns=3D$1 + + vm_ssh "${ns}" -- dmesg 2>/dev/null | grep -c -i 'Oops' +} + +vm_dmesg_warn_count() { + local ns=3D$1 + + vm_ssh "${ns}" -- dmesg --level=3Dwarn 2>/dev/null | grep -c -i 'vsock' +} + +vm_dmesg_check() { + local pidfile=3D$1 + local ns=3D$2 + local oops_before=3D$3 + local warn_before=3D$4 + local oops_after warn_after + + oops_after=3D$(vm_ssh "${ns}" -- dmesg 2>/dev/null | grep -c -i 'Oops') + if [[ "${oops_after}" -gt "${oops_before}" ]]; then + echo "FAIL: kernel oops detected on vm in ns ${ns}" | log_host + return 1 + fi + + warn_after=3D$(vm_ssh "${ns}" -- dmesg --level=3Dwarn 2>/dev/null | grep = -c -i 'vsock') + if [[ "${warn_after}" -gt "${warn_before}" ]]; then + echo "FAIL: kernel warning detected on vm in ns ${ns}" | log_host + return 1 + fi + + return 0 +} + vm_vsock_test() { local ns=3D$1 local host=3D$2 @@ -568,6 +677,450 @@ test_ns_host_vsock_ns_mode_ok() { return "${KSFT_PASS}" } =20 +test_ns_diff_global_host_connect_to_global_vm_ok() { + local oops_before warn_before + local pids pid pidfile + local ns0 ns1 port + declare -a pids + local unixfile + ns0=3D"global0" + ns1=3D"global1" + port=3D1234 + local rc + + init_namespaces + + pidfile=3D"$(create_pidfile)" + + if ! vm_start "${pidfile}" "${ns0}"; then + return "${KSFT_FAIL}" + fi + + vm_wait_for_ssh "${ns0}" + oops_before=3D$(vm_dmesg_oops_count "${ns0}") + warn_before=3D$(vm_dmesg_warn_count "${ns0}") + + unixfile=3D$(mktemp -u /tmp/XXXX.sock) + ip netns exec "${ns1}" \ + socat TCP-LISTEN:"${TEST_HOST_PORT}",fork \ + UNIX-CONNECT:"${unixfile}" & + pids+=3D($!) + host_wait_for_listener "${ns1}" "${TEST_HOST_PORT}" + + ip netns exec "${ns0}" socat UNIX-LISTEN:"${unixfile}",fork \ + TCP-CONNECT:localhost:"${TEST_HOST_PORT}" & + pids+=3D($!) + + vm_vsock_test "${ns0}" "server" 2 "${TEST_GUEST_PORT}" + vm_wait_for_listener "${ns0}" "${TEST_GUEST_PORT}" + host_vsock_test "${ns1}" "127.0.0.1" "${VSOCK_CID}" "${TEST_HOST_PORT}" + rc=3D$? + + vm_dmesg_check "${pidfile}" "${ns0}" "${oops_before}" "${warn_before}" + dmesg_rc=3D$? + + terminate_pids "${pids[@]}" + terminate_pidfiles "${pidfile}" + + if [[ "${rc}" -ne 0 ]] || [[ "${dmesg_rc}" -ne 0 ]]; then + return "${KSFT_FAIL}" + fi + + return "${KSFT_PASS}" +} + +test_ns_diff_global_host_connect_to_local_vm_fails() { + local oops_before warn_before + local ns0=3D"global0" + local ns1=3D"local0" + local port=3D12345 + local dmesg_rc + local pidfile + local result + local pid + + init_namespaces + + outfile=3D$(mktemp) + + pidfile=3D"$(create_pidfile)" + if ! vm_start "${pidfile}" "${ns1}"; then + log_host "failed to start vm (cid=3D${VSOCK_CID}, ns=3D${ns0})" + return "${KSFT_FAIL}" + fi + + vm_wait_for_ssh "${ns1}" + oops_before=3D$(vm_dmesg_oops_count "${ns1}") + warn_before=3D$(vm_dmesg_warn_count "${ns1}") + + vm_ssh "${ns1}" -- socat VSOCK-LISTEN:"${port}" STDOUT > "${outfile}" & + echo TEST | ip netns exec "${ns0}" \ + socat STDIN VSOCK-CONNECT:"${VSOCK_CID}":"${port}" 2>/dev/null + + vm_dmesg_check "${pidfile}" "${ns1}" "${oops_before}" "${warn_before}" + dmesg_rc=3D$? + + terminate_pidfiles "${pidfile}" + result=3D$(cat "${outfile}") + rm -f "${outfile}" + + if [[ "${result}" =3D=3D "TEST" ]] || [[ "${dmesg_rc}" -ne 0 ]]; then + return "${KSFT_FAIL}" + fi + + return "${KSFT_PASS}" +} + +test_ns_diff_global_vm_connect_to_global_host_ok() { + local oops_before warn_before + local ns0=3D"global0" + local ns1=3D"global1" + local port=3D12345 + local unixfile + local dmesg_rc + local pidfile + local pids + local rc + + init_namespaces + + declare -a pids + + log_host "Setup socat bridge from ns ${ns0} to ns ${ns1} over port ${port= }" + + unixfile=3D$(mktemp -u /tmp/XXXX.sock) + + ip netns exec "${ns0}" \ + socat TCP-LISTEN:"${port}" UNIX-CONNECT:"${unixfile}" & + pids+=3D($!) + + ip netns exec "${ns1}" \ + socat UNIX-LISTEN:"${unixfile}" TCP-CONNECT:127.0.0.1:"${port}" & + pids+=3D($!) + + log_host "Launching ${VSOCK_TEST} in ns ${ns1}" + host_vsock_test "${ns1}" "server" "${VSOCK_CID}" "${port}" + + pidfile=3D"$(create_pidfile)" + if ! vm_start "${pidfile}" "${ns0}"; then + log_host "failed to start vm (cid=3D${cid}, ns=3D${ns0})" + terminate_pids "${pids[@]}" + rm -f "${unixfile}" + return "${KSFT_FAIL}" + fi + + vm_wait_for_ssh "${ns0}" + + oops_before=3D$(vm_dmesg_oops_count "${ns0}") + warn_before=3D$(vm_dmesg_warn_count "${ns0}") + + vm_vsock_test "${ns0}" "10.0.2.2" 2 "${port}" + rc=3D$? + + vm_dmesg_check "${pidfile}" "${ns0}" "${oops_before}" "${warn_before}" + dmesg_rc=3D$? + + terminate_pidfiles "${pidfile}" + terminate_pids "${pids[@]}" + rm -f "${unixfile}" + + if [[ "${rc}" -ne 0 ]] || [[ "${dmesg_rc}" -ne 0 ]]; then + return "${KSFT_FAIL}" + fi + + return "${KSFT_PASS}" + +} + +test_ns_diff_global_vm_connect_to_local_host_fails() { + local ns0=3D"global0" + local ns1=3D"local0" + local port=3D12345 + local oops_before warn_before + local dmesg_rc + local pidfile + local result + local pid + + init_namespaces + + log_host "Launching socat in ns ${ns1}" + outfile=3D$(mktemp) + ip netns exec "${ns1}" socat VSOCK-LISTEN:"${port}" STDOUT &> "${outfile}= " & + pid=3D$! + + pidfile=3D"$(create_pidfile)" + if ! vm_start "${pidfile}" "${ns0}"; then + log_host "failed to start vm (cid=3D${cid}, ns=3D${ns0})" + terminate_pids "${pid}" + rm -f "${outfile}" + return "${KSFT_FAIL}" + fi + + vm_wait_for_ssh "${ns0}" + + oops_before=3D$(vm_dmesg_oops_count "${ns0}") + warn_before=3D$(vm_dmesg_warn_count "${ns0}") + + vm_ssh "${ns0}" -- \ + bash -c "echo TEST | socat STDIN VSOCK-CONNECT:2:${port}" 2>&1 | log_gue= st + + vm_dmesg_check "${pidfile}" "${ns0}" "${oops_before}" "${warn_before}" + dmesg_rc=3D$? + + terminate_pidfiles "${pidfile}" + terminate_pids "${pid}" + + result=3D$(cat "${outfile}") + rm -f "${outfile}" + + if [[ "${result}" !=3D TEST ]] && [[ "${dmesg_rc}" -eq 0 ]]; then + return "${KSFT_PASS}" + fi + + return "${KSFT_FAIL}" +} + +test_ns_diff_local_host_connect_to_local_vm_fails() { + local ns0=3D"local0" + local ns1=3D"local1" + local port=3D12345 + local oops_before warn_before + local dmesg_rc + local pidfile + local result + local pid + + init_namespaces + + outfile=3D$(mktemp) + + pidfile=3D"$(create_pidfile)" + if ! vm_start "${pidfile}" "${ns1}"; then + log_host "failed to start vm (cid=3D${cid}, ns=3D${ns0})" + return "${KSFT_FAIL}" + fi + + vm_wait_for_ssh "${ns1}" + oops_before=3D$(vm_dmesg_oops_count "${ns1}") + warn_before=3D$(vm_dmesg_warn_count "${ns1}") + + vm_ssh "${ns1}" -- socat VSOCK-LISTEN:"${port}" STDOUT > "${outfile}" & + echo TEST | ip netns exec "${ns0}" \ + socat STDIN VSOCK-CONNECT:"${VSOCK_CID}":"${port}" 2>/dev/null + + vm_dmesg_check "${pidfile}" "${ns1}" "${oops_before}" "${warn_before}" + dmesg_rc=3D$? + + terminate_pidfiles "${pidfile}" + + result=3D$(cat "${outfile}") + rm -f "${outfile}" + + if [[ "${result}" !=3D TEST ]] && [[ "${dmesg_rc}" -eq 0 ]]; then + return "${KSFT_PASS}" + fi + + return "${KSFT_FAIL}" +} + +test_ns_diff_local_vm_connect_to_local_host_fails() { + local oops_before warn_before + local ns0=3D"local0" + local ns1=3D"local1" + local port=3D12345 + local dmesg_rc + local pidfile + local result + local pid + + init_namespaces + + log_host "Launching socat in ns ${ns1}" + outfile=3D$(mktemp) + ip netns exec "${ns1}" socat VSOCK-LISTEN:"${port}" STDOUT &> "${outfile}= " & + pid=3D$! + + pidfile=3D"$(create_pidfile)" + if ! vm_start "${pidfile}" "${ns0}"; then + log_host "failed to start vm (cid=3D${cid}, ns=3D${ns0})" + rm -f "${outfile}" + return "${KSFT_FAIL}" + fi + + vm_wait_for_ssh "${ns0}" + oops_before=3D$(vm_dmesg_oops_count "${ns0}") + warn_before=3D$(vm_dmesg_warn_count "${ns0}") + + vm_ssh "${ns0}" -- \ + bash -c "echo TEST | socat STDIN VSOCK-CONNECT:2:${port}" 2>&1 | log_gue= st + + vm_dmesg_check "${pidfile}" "${ns0}" "${oops_before}" "${warn_before}" + dmesg_rc=3D$? + + terminate_pidfiles "${pidfile}" + terminate_pids "${pid}" + + result=3D$(cat "${outfile}") + rm -f "${outfile}" + + if [[ "${result}" !=3D TEST ]] && [[ "${dmesg_rc}" -eq 0 ]]; then + return "${KSFT_PASS}" + fi + + return "${KSFT_FAIL}" +} + +__test_loopback_two_netns() { + local ns0=3D$1 + local ns1=3D$2 + local port=3D12345 + local result + local pid + + modprobe vsock_loopback &> /dev/null || : + + log_host "Launching socat in ns ${ns1}" + outfile=3D$(mktemp) + ip netns exec "${ns1}" socat VSOCK-LISTEN:"${port}" STDOUT > "${outfile}"= 2>/dev/null & + pid=3D$! + + log_host "Launching socat in ns ${ns0}" + echo TEST | ip netns exec "${ns0}" socat STDIN VSOCK-CONNECT:1:"${port}" = 2>/dev/null + terminate_pids "${pid}" + + result=3D$(cat "${outfile}") + rm -f "${outfile}" + + if [[ "${result}" =3D=3D TEST ]]; then + return 0 + fi + + return 1 +} + +test_ns_diff_global_to_local_loopback_local_fails() { + init_namespaces + + if ! __test_loopback_two_netns "global0" "local0"; then + return "${KSFT_PASS}" + fi + + return "${KSFT_FAIL}" +} + +test_ns_diff_local_to_global_loopback_fails() { + init_namespaces + + if ! __test_loopback_two_netns "local0" "global0"; then + return "${KSFT_PASS}" + fi + + return "${KSFT_FAIL}" +} + +test_ns_diff_local_to_local_loopback_fails() { + init_namespaces + + if ! __test_loopback_two_netns "local0" "local1"; then + return "${KSFT_PASS}" + fi + + return "${KSFT_FAIL}" +} + +test_ns_diff_global_to_global_loopback_ok() { + init_namespaces + + if __test_loopback_two_netns "global0" "global1"; then + return "${KSFT_PASS}" + fi + + return "${KSFT_FAIL}" +} + +test_ns_same_local_loopback_ok() { + init_namespaces + + if __test_loopback_two_netns "local0" "local0"; then + return "${KSFT_PASS}" + fi + + return "${KSFT_FAIL}" +} + +test_ns_same_local_host_connect_to_local_vm_ok() { + local oops_before warn_before + local ns=3D"local0" + local port=3D1234 + local dmesg_rc + local pidfile + local rc + + init_namespaces + + pidfile=3D"$(create_pidfile)" + + if ! vm_start "${pidfile}" "${ns}"; then + return "${KSFT_FAIL}" + fi + + vm_wait_for_ssh "${ns}" + oops_before=3D$(vm_dmesg_oops_count "${ns}") + warn_before=3D$(vm_dmesg_warn_count "${ns}") + + vm_vsock_test "${ns}" "server" 2 "${TEST_GUEST_PORT}" + host_vsock_test "${ns}" "127.0.0.1" "${VSOCK_CID}" "${TEST_HOST_PORT}" + rc=3D$? + + vm_dmesg_check "${pidfile}" "${ns}" "${oops_before}" "${warn_before}" + dmesg_rc=3D$? + + terminate_pidfiles "${pidfile}" + + if [[ "${rc}" -ne 0 ]] || [[ "${dmesg_rc}" -ne 0 ]]; then + return "${KSFT_FAIL}" + fi + + return "${KSFT_PASS}" +} + +test_ns_same_local_vm_connect_to_local_host_ok() { + local oops_before warn_before + local ns=3D"local0" + local port=3D1234 + local dmesg_rc + local pidfile + local rc + + init_namespaces + + pidfile=3D"$(create_pidfile)" + + if ! vm_start "${pidfile}" "${ns}"; then + return "${KSFT_FAIL}" + fi + + vm_wait_for_ssh "${ns}" + oops_before=3D$(vm_dmesg_oops_count "${ns}") + warn_before=3D$(vm_dmesg_warn_count "${ns}") + + vm_vsock_test "${ns}" "server" 2 "${TEST_GUEST_PORT}" + host_vsock_test "${ns}" "127.0.0.1" "${VSOCK_CID}" "${TEST_HOST_PORT}" + rc=3D$? + + vm_dmesg_check "${pidfile}" "${ns}" "${oops_before}" "${warn_before}" + dmesg_rc=3D$? + + terminate_pidfiles "${pidfile}" + + if [[ "${rc}" -ne 0 ]] || [[ "${dmesg_rc}" -ne 0 ]]; then + return "${KSFT_FAIL}" + fi + + return "${KSFT_PASS}" +} + namespaces_can_boot_same_cid() { local ns0=3D$1 local ns1=3D$2 @@ -861,6 +1414,7 @@ fi check_args "${ARGS[@]}" check_deps check_vng +check_socat handle_build =20 echo "1..${#ARGS[@]}" --=20 2.47.3 From nobody Tue Dec 2 02:43:45 2025 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3EA0632C31B for ; Tue, 18 Nov 2025 02:00:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763431248; cv=none; b=u1VUiMUhTXZ1BLb5mWgb0FK/HL67kYkz2ZmOsU77W+0dG+rPoglJus7aSKeqX/n8jYL1kJ8fU7g/tJEngRnFAcb7sDF6/1jKXEBDSklRODimXmlk2C/gb6O5QsZPWpVI3sG2PDRWQsTipnbMGQwzGsIoB3XuZ/SyZjDNPMSkZWI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763431248; c=relaxed/simple; bh=GOHovVPP3sa3rEV3HpfajQ6Y99pnpQNi1sXPkKn61uE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nefQCeiaJri3UxPLQQ/b6NEnvFcYtFAcIWdTvm8HeeawYHo22mlgRnVdQOa+zL3DaYGRWiAjRIvjwk5R6QRQKIEmP5ZcGL3BIY4Q3dOfskltl77P5Xx4lrSPs6QHVnI4byxZUtTlgRwAotZqRptjcaYdg9/gIK+7DiFvOzZa0hM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=nZNODQ4H; arc=none smtp.client-ip=209.85.216.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="nZNODQ4H" Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-3437ea05540so4652391a91.0 for ; Mon, 17 Nov 2025 18:00:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763431241; x=1764036041; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=ojZg+AuTuP1uKFswsO3FbXsz8LEDmbWQ1i6mei7DtIc=; b=nZNODQ4H2f7XTLYLGw02jIujpFewzLlQkk+2XthbzqMnBO/sC/MjK52J+cPNhdAlb3 QQgmza5nhSi362YGfWUSoTZ1hm//7JHD7Luf/jQdobw5/6jbjVHTkRYIPM3DHERfSrk2 rVlg82c9iII4zIS8C6lILgO614pG2ZKmo6Q2vhIdjHUZLtZCAeZArYpFvOAzXDbkgPPn 4Oavt4W1aPXmmvBK6InivMMrA0fZ+yVhfNhAZlBqmoP7wJkn29AVuaZFNyXPufWDYrjw 2IrtKqofF8ivybf7gb1KdwRKhz/wqCPHji+Esc86k1GHxkUBdLe05tBz6T9wJdsMuRHh 7LgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763431241; x=1764036041; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=ojZg+AuTuP1uKFswsO3FbXsz8LEDmbWQ1i6mei7DtIc=; b=FfONMF0Nmuydvu+W0PukibwUGCCjK8kCKVpzPAbah81nMD62EKHn22JYlJQ6qzuXN6 y+co6l8pwrZzqT947236WXUfD5WxN633VYj/z/DIahY1eCSMrYocnDdsnxaNIT7x8TRQ rygInwQavUAg28tjlLZtPjJRfbaAtWbSiidQO1soy9mH3PvXNdM5g/8EYxKDhSSYV36N gHUkiHOz7fpObXHHNpGNT32oTINYgeaD5XTD3B8Be0MIgK71yqRzqkUnoqJvnJcmq0QQ wgzNtlrj0CzqApYBBil6a3DZEVUT+JCxr+TSTS3IOkaonWEqDz7oeUPsbp734OTxqmCW uyEw== X-Gm-Message-State: AOJu0YyuJRfmvfIQvDpbX1lBoAmqtaBnSoR1oHz0vDZqtQ3MybPeFcnz IB5sS3JkU6BPV+tXvI7OLk16Usu7YTPuquz65wZEj1BUabn3sFvjGN9S X-Gm-Gg: ASbGnctamOs2Uto834vzg2QEivvJNvg6+O2DT1a8EiR0tV3K/HEKIFAknq6P0t0jxly IH1WyC2crd/LkGXtHbjM67gce7UhFtxMUEX6Eu0fxmRdmffSRBHhrSfEWZP5qQRqwQ7mOTnYLrK 8QEotoo5l5BUDfe+xDXG1kbkURnhthS4DOM5O2pU0WCvPdg5a26OZL1ZIvs54ck50Fo/rH6jkIv GYS7qe96IfHwmyLVn4yL9D9g96XMpd9HRIcc2yZ7mvL0A6FQTqjUu2C11TQmmNZB8p4vWbmxFId AZj+kw23/ysnCRxQcxgFFCP/4fnuMphBVS9dqgtnRqeSJMQkGmCo6OlDiI7vRB4q07pt2+58JA1 Txo0i2oNjLZt4Mxo2gn5Q/KTxxnJD3EwqC5RY0hii6OApi7+9+G+Gcts7nVrJSzh/7cpqkDjx+c /WTGKWsoppWTq7O8EjgZ1SSZBsbmSnAg== X-Google-Smtp-Source: AGHT+IFmXdK6Jbwd3vBn123y4KjundCkIApjrLwMuiHqG6Ct/ZwKD1VG2QzQPrnW/V2jQffbQoI3yw== X-Received: by 2002:a17:90b:388c:b0:341:88d5:a74e with SMTP id 98e67ed59e1d1-343fa63dc85mr15304600a91.29.1763431241236; Mon, 17 Nov 2025 18:00:41 -0800 (PST) Received: from localhost ([2a03:2880:2ff:4::]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-343e07952f8sm19773658a91.9.2025.11.17.18.00.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Nov 2025 18:00:40 -0800 (PST) From: Bobby Eshleman Date: Mon, 17 Nov 2025 18:00:34 -0800 Subject: [PATCH net-next v10 11/11] selftests/vsock: add tests for namespace deletion and mode changes Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251117-vsock-vmtest-v10-11-df08f165bf3e@meta.com> References: <20251117-vsock-vmtest-v10-0-df08f165bf3e@meta.com> In-Reply-To: <20251117-vsock-vmtest-v10-0-df08f165bf3e@meta.com> To: Stefano Garzarella , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Stefan Hajnoczi , "Michael S. Tsirkin" , Jason Wang , =?utf-8?q?Eugenio_P=C3=A9rez?= , Xuan Zhuo , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Bryan Tan , Vishnu Dasa , Broadcom internal kernel review list , Shuah Khan Cc: linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, netdev@vger.kernel.org, kvm@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kselftest@vger.kernel.org, Sargun Dhillon , Bobby Eshleman , berrange@redhat.com, Bobby Eshleman X-Mailer: b4 0.14.3 From: Bobby Eshleman Add tests that validate vsock sockets are resilient to deleting namespaces or changing namespace modes from global to local. The vsock sockets should still function normally. The function check_ns_changes_dont_break_connection() is added to re-use the step-by-step logic of 1) setup connections, 2) do something that would maybe break the connections, 3) check that the connections are still ok. Signed-off-by: Bobby Eshleman Suggested-by: Sargun Dhillon --- Changes in v9: - more consistent shell style - clarify -u usage comment for pipefile --- tools/testing/selftests/vsock/vmtest.sh | 123 ++++++++++++++++++++++++++++= ++++ 1 file changed, 123 insertions(+) diff --git a/tools/testing/selftests/vsock/vmtest.sh b/tools/testing/selfte= sts/vsock/vmtest.sh index 9c12c1bd1edc..2b6e94aafc19 100755 --- a/tools/testing/selftests/vsock/vmtest.sh +++ b/tools/testing/selftests/vsock/vmtest.sh @@ -66,6 +66,12 @@ readonly TEST_NAMES=3D( ns_same_local_loopback_ok ns_same_local_host_connect_to_local_vm_ok ns_same_local_vm_connect_to_local_host_ok + ns_mode_change_connection_continue_vm_ok + ns_mode_change_connection_continue_host_ok + ns_mode_change_connection_continue_both_ok + ns_delete_vm_ok + ns_delete_host_ok + ns_delete_both_ok ) readonly TEST_DESCS=3D( # vm_server_host_client @@ -135,6 +141,24 @@ readonly TEST_DESCS=3D( =20 # ns_same_local_vm_connect_to_local_host_ok "Run vsock_test client in VM in a local ns with server in same ns." + + # ns_mode_change_connection_continue_vm_ok + "Check that changing NS mode of VM namespace from global to local after a= connection is established doesn't break the connection" + + # ns_mode_change_connection_continue_host_ok + "Check that changing NS mode of host namespace from global to local after= a connection is established doesn't break the connection" + + # ns_mode_change_connection_continue_both_ok + "Check that changing NS mode of host and VM namespaces from global to loc= al after a connection is established doesn't break the connection" + + # ns_delete_vm_ok + "Check that deleting the VM's namespace does not break the socket connect= ion" + + # ns_delete_host_ok + "Check that deleting the host's namespace does not break the socket conne= ction" + + # ns_delete_both_ok + "Check that deleting the VM and host's namespaces does not break the sock= et connection" ) =20 readonly USE_SHARED_VM=3D( @@ -1256,6 +1280,105 @@ test_ns_vm_local_mode_rejected() { return "${KSFT_PASS}" } =20 +check_ns_changes_dont_break_connection() { + local pipefile pidfile outfile + local ns0=3D"global0" + local ns1=3D"global1" + local port=3D12345 + local pids=3D() + local rc=3D0 + + init_namespaces + + pidfile=3D"$(create_pidfile)" + if ! vm_start "${pidfile}" "${ns0}"; then + return "${KSFT_FAIL}" + fi + vm_wait_for_ssh "${ns0}" + + outfile=3D$(mktemp) + vm_ssh "${ns0}" -- \ + socat VSOCK-LISTEN:"${port}",fork STDOUT > "${outfile}" 2>/dev/null & + pids+=3D($!) + + # wait_for_listener() does not work for vsock because vsock does not + # export socket state to /proc/net/. Instead, we have no choice but to + # sleep for some hardcoded time. + sleep "${WAIT_PERIOD}" + + # We use a pipe here so that we can echo into the pipe instead of using + # socat and a unix socket file. We just need a name for the pipe (not a + # regular file) so use -u. + pipefile=3D$(mktemp -u /tmp/vmtest_pipe_XXXX) + ip netns exec "${ns1}" \ + socat PIPE:"${pipefile}" VSOCK-CONNECT:"${VSOCK_CID}":"${port}" & + pids+=3D($!) + + timeout "${WAIT_PERIOD}" \ + bash -c 'while [[ ! -e '"${pipefile}"' ]]; do sleep 1; done; exit 0' + + if [[ $2 =3D=3D "delete" ]]; then + if [[ "$1" =3D=3D "vm" ]]; then + ip netns del "${ns0}" + elif [[ "$1" =3D=3D "host" ]]; then + ip netns del "${ns1}" + elif [[ "$1" =3D=3D "both" ]]; then + ip netns del "${ns0}" + ip netns del "${ns1}" + fi + elif [[ $2 =3D=3D "change_mode" ]]; then + if [[ "$1" =3D=3D "vm" ]]; then + ns_set_mode "${ns0}" "local" + elif [[ "$1" =3D=3D "host" ]]; then + ns_set_mode "${ns1}" "local" + elif [[ "$1" =3D=3D "both" ]]; then + ns_set_mode "${ns0}" "local" + ns_set_mode "${ns1}" "local" + fi + fi + + echo "TEST" > "${pipefile}" + + timeout "${WAIT_PERIOD}" \ + bash -c 'while [[ ! -s '"${outfile}"' ]]; do sleep 1; done; exit 0' + + if grep -q "TEST" "${outfile}"; then + rc=3D"${KSFT_PASS}" + else + rc=3D"${KSFT_FAIL}" + fi + + terminate_pidfiles "${pidfile}" + terminate_pids "${pids[@]}" + rm -f "${outfile}" + + return "${rc}" +} + +test_ns_mode_change_connection_continue_vm_ok() { + check_ns_changes_dont_break_connection "vm" "change_mode" +} + +test_ns_mode_change_connection_continue_host_ok() { + check_ns_changes_dont_break_connection "host" "change_mode" +} + +test_ns_mode_change_connection_continue_both_ok() { + check_ns_changes_dont_break_connection "both" "change_mode" +} + +test_ns_delete_vm_ok() { + check_ns_changes_dont_break_connection "vm" "delete" +} + +test_ns_delete_host_ok() { + check_ns_changes_dont_break_connection "host" "delete" +} + +test_ns_delete_both_ok() { + check_ns_changes_dont_break_connection "both" "delete" +} + shared_vm_test() { local tname =20 --=20 2.47.3