vsock: add namespace support to vhost-vsock and loopback
Subsystems (12)
v16 cover vsock: add namespace support to vhost-vsock and loopback
This series adds namespace support to vhost-vsock and loopback. It does not add namespaces to any of the other guest transports (virtio-vsock, hyperv, or vmci).
The current revision supports two modes: local and global. Local mode is complete isolation of namespaces, while global mode is complete sharing between namespaces of CIDs (the original behavior).
The mode is set using the parent namespace's /proc/sys/net/vsock/child_ns_mode and inherited when a new namespace is created. The mode of the current namespace can be queried by reading /proc/sys/net/vsock/ns_mode. The mode can not change after the namespace has been created.
Modes are per-netns. This allows a system to configure namespaces independently (some may share CIDs, others are completely isolated). This also supports future possible mixed use cases, where there may be namespaces in global mode spinning up VMs while there are mixed mode namespaces that provide services to the VMs, but are not allowed to allocate from the global CID pool (this mode is not implemented in this series).
Additionally, added tests for the new namespace features:
tools/testing/selftests/vsock/vmtest.sh 1..25
ok 1 vm_server_host_client
ok 2 vm_client_host_server
ok 3 vm_loopback
ok 4 ns_host_vsock_ns_mode_ok
ok 5 ns_host_vsock_child_ns_mode_ok
ok 6 ns_global_same_cid_fails
ok 7 ns_local_same_cid_ok
ok 8 ns_global_local_same_cid_ok
ok 9 ns_local_global_same_cid_ok
ok 10 ns_diff_global_host_connect_to_global_vm_ok ok 11 ns_diff_global_host_connect_to_local_vm_fails ok 12 ns_diff_global_vm_connect_to_global_host_ok ok 13 ns_diff_global_vm_connect_to_local_host_fails ok 14 ns_diff_local_host_connect_to_local_vm_fails ok 15 ns_diff_local_vm_connect_to_local_host_fails ok 16 ns_diff_global_to_local_loopback_local_fails ok 17 ns_diff_local_to_global_loopback_fails ok 18 ns_diff_local_to_local_loopback_fails ok 19 ns_diff_global_to_global_loopback_ok ok 20 ns_same_local_loopback_ok
ok 21 ns_same_local_host_connect_to_local_vm_ok ok 22 ns_same_local_vm_connect_to_local_host_ok ok 23 ns_delete_vm_ok
ok 24 ns_delete_host_ok
ok 25 ns_delete_both_ok
SUMMARY: PASS=25 SKIP=0 FAIL=0
Thanks again for everyone's help and reviews!
Changes in v16:
- updated comments/docs/commit msg (vsock_find_* funcs, init net
mode, why change random port alloc)
- removed init ns mode cmdline
- fixed the missing ${ns} arg for vm_ssh in vmtest.sh
- Link to v15: https://lore.kernel.org/r/[email protected]
Changes in v15:
- see per-patch change notes in 'vsock: add netns to vsock core'
- Link to v14: https://lore.kernel.org/r/[email protected]
Changes in v14:
- squashed 'vsock: add per-net vsock NS mode state' into 'vsock: add
netns to vsock core' (MST)
- remove RFC tag
- fixed base-commit (still had b4 configured to depend on old vmtest.sh series)
- Link to v13: https://lore.kernel.org/all/[email protected]/
Changes in v13:
- add support for immutable sysfs ns_mode and inheritance from sysfs child_ns_mode
- remove passing around of net_mode, can be accessed now via vsock_net_mode(net) since it is immutable
- update tests for new uAPI
- add one patch to extend the kselftest timeout (it was starting to
fail with the new tests added)
- Link to v12: https://lore.kernel.org/r/[email protected]
Changes in v12:
- add ns mode checking to _allow() callbacks to reject local mode for incompatible transports (Stefano)
- flip vhost/loopback to return true for stream_allow() and seqpacket_allow() in "vsock: add netns support to virtio transports" (Stefano)
- add VMADDR_CID_ANY + local mode documentation in af_vsock.c (Stefano)
- change "selftests/vsock: add tests for host <-> vm connectivity with namespaces" to skip test 29 in vsock_test for namespace local
vsock_test calls in a host local-mode namespace. There is a false-positive edge case for that test encountered with the ->stream_allow() approach. More details in that patch.
- updated cover letter with new test output
- Link to v11: https://lore.kernel.org/r/[email protected]
Changes in v11:
- vmtest: add a patch to use ss in wait_for_listener functions and
support vsock, tcp, and unix. Change all patches to use the new functions.
- vmtest: add a patch to re-use vm dmesg / warn counting functions
- Link to v10: https://lore.kernel.org/r/[email protected]
Changes in v10:
- Combine virtio common patches into one (Stefano)
- Resolve vsock_loopback virtio_transport_reset_no_sock() issue
with info->vsk setting. This eliminates the need for skb->cb,
so remove skb->cb patches.
- many line width 80 fixes
- Link to v9: https://lore.kernel.org/all/[email protected]
Changes in v9:
- reorder loopback patch after patch for virtio transport common code
- remove module ordering tests patch because loopback no longer depends
on pernet ops
- major simplifications in vsock_loopback
- added a new patch for blocking local mode for guests, added test case
to check
- add net ref tracking to vsock_loopback patch
- Link to v8: https://lore.kernel.org/r/[email protected]
Changes in v8:
- Break generic cleanup/refactoring patches into standalone series,
remove those from this series
- Link to dependency: https://lore.kernel.org/all/20251022-vsock-selftests-fixes-and-improvements-v1-0-edeb179d6463@meta.com/
- Link to v7: https://lore.kernel.org/r/[email protected]
Changes in v7:
- fix hv_sock build
- break out vmtest patches into distinct, more well-scoped patches
- change orig_net_mode to net_mode
- many fixes and style changes in per-patch change sets (see individual patches for specific changes)
- optimize virtio_vsock_skb_cb layout
- update commit messages with more useful descriptions
- vsock_loopback: use orig_net_mode instead of current net mode
- add tests for edge cases (ns deletion, mode changing, loopback module load ordering)
- Link to v6: https://lore.kernel.org/r/[email protected]
Changes in v6:
- define behavior when mode changes to local while socket/VM is alive
- af_vsock: clarify description of CID behavior
- af_vsock: use stronger langauge around CID rules (dont use "may")
- af_vsock: improve naming of buf/buffer
- af_vsock: improve string length checking on proc writes
- vsock_loopback: add space in struct to clarify lock protection
- vsock_loopback: do proper cleanup/unregister on vsock_loopback_exit()
- vsock_loopback: use virtio_vsock_skb_net() instead of sock_net()
- vsock_loopback: set loopback to NULL after kfree()
- vsock_loopback: use pernet_operations and remove callback mechanism
- vsock_loopback: add macros for "global" and "local"
- vsock_loopback: fix length checking
- vmtest.sh: check for namespace support in vmtest.sh
- Link to v5: https://lore.kernel.org/r/[email protected]
Changes in v5:
- /proc/net/vsock_ns_mode -> /proc/sys/net/vsock/ns_mode
- vsock_global_net -> vsock_global_dummy_net
- fix netns lookup in vhost_vsock to respect pid namespaces
- add callbacks for vsock_loopback to avoid circular dependency
- vmtest.sh loads vsock_loopback module
- remove vsock_net_mode_can_set()
- change vsock_net_write_mode() to return true/false based on success
- make vsock_net_mode enum instead of u8
- Link to v4: https://lore.kernel.org/r/[email protected]
Changes in v4:
- removed RFC tag
- implemented loopback support
- renamed new tests to better reflect behavior
- completed suite of tests with permutations of ns modes and vsock_test as guest/host
- simplified socat bridging with unix socket instead of tcp + veth
- only use vsock_test for success case, socat for failure case (context in commit message)
- lots of cleanup
Changes in v3:
- add notion of "modes"
- add procfs /proc/net/vsock_ns_mode
- local and global modes only
- no /dev/vhost-vsock-netns
- vmtest.sh already merged, so new patch just adds new tests for NS
- Link to v2:
https://lore.kernel.org/kvm/[email protected]
Changes in v2:
- only support vhost-vsock namespaces
- all g2h namespaces retain old behavior, only common API changes impacted by vhost-vsock changes
- add /dev/vhost-vsock-netns for "opt-in"
- leave /dev/vhost-vsock to old behavior
- removed netns module param
- Link to v1:
https://lore.kernel.org/r/[email protected]
Changes in v1:
- added 'netns' module param to vsock.ko to enable the network namespace support (disabled by default)
- added 'vsock_net_eq()' to check the "net" assigned to a socket only when 'netns' support is enabled
- Link to RFC: https://patchwork.ozlabs.org/cover/1202235/
---
Bobby Eshleman (12):
vsock: add netns to vsock core
virtio: set skb owner of virtio_transport_reset_no_sock() reply
vsock: add netns support to virtio transports selftests/vsock: increase timeout to 1200
selftests/vsock: add namespace helpers to vmtest.sh selftests/vsock: prepare vm management helpers for namespaces selftests/vsock: add vm_dmesg_{warn,oops}_count() helpers selftests/vsock: use ss to wait for listeners instead of /proc/net selftests/vsock: add tests for proc sys vsock ns_mode selftests/vsock: add namespace tests for CID collisions selftests/vsock: add tests for host <-> vm connectivity with namespaces selftests/vsock: add tests for namespace deletion
MAINTAINERS | 1 + drivers/vhost/vsock.c | 44 +- include/linux/virtio_vsock.h | 9 +- include/net/af_vsock.h | 61 +- include/net/net_namespace.h | 4 + include/net/netns/vsock.h | 21 + net/vmw_vsock/af_vsock.c | 335 +++++++++- net/vmw_vsock/hyperv_transport.c | 7 +- net/vmw_vsock/virtio_transport.c | 22 +- net/vmw_vsock/virtio_transport_common.c | 62 +- net/vmw_vsock/vmci_transport.c | 26 +- net/vmw_vsock/vsock_loopback.c | 22 +- tools/testing/selftests/vsock/settings | 2 +- tools/testing/selftests/vsock/vmtest.sh | 1055 +++++++++++++++++++++++++++++-- 14 files changed, 1531 insertions(+), 140 deletions(-)
---
base-commit: d8f87aa5fa0a4276491fa8ef436cd22605a3f9ba
change-id: 20250325-vsock-vmtest-b3a21d2102c2
Best regards,
Signature · 1 line
Bobby Eshleman <[email protected]>
PATCH v16 1/12 vsock: add netns to vsock core
From: Bobby Eshleman <[email protected]>
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 to report the mode and /proc/sys/net/vsock/child_ns_mode to set the mode for new namespaces.
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.
This patch changes the allocation of random ports for connectible vsocks in order to avoid leaking the random port range starting point to other namespaces.
dgram_allow(), stream_allow(), and seqpacket_allow() callbacks are modified to take a vsk in order to perform logic on namespace modes. In future patches, the net will also be used for socket
lookups in these functions.
---
Changes in v16:
- remove vsock_init_ns_mode kernel cmdline
- add documentation at top of af_vsock.c about init_net being global and unmodifiable
- add comments for vsock_find* and vsock_find*_net functions to clarify which to choose for which transports
Changes in v15:
- make static port in __vsock_bind_connectible per-netns
- remove __net_initdata because we want the ops beyond just boot
- add vsock_init_ns_mode kernel cmdline parameter to set init ns mode
- use if (ret || !write) in __vsock_net_mode_string() (Stefano)
- add vsock_net_mode_global() (Stefano)
- hide !net == VSOCK_NET_MODE_GLOBAL inside vsock_net_mode() (Stefano)
- clarify af_vsock.c comments on ns_mode/child_ns_mode (Stefano)
Changes in v14:
- include linux/sysctl.h in af_vsock.c
- squash patch 'vsock: add per-net vsock NS mode state' into this patch (prior version can be found here):
https://lore.kernel.org/all/[email protected]/)
Changes in v13:
- remove net_mode and replace with direct accesses to net->vsock.mode, since this is now immutable.
- update comments about mode behavior and mutability, and sysctl API
- only pass NULL for net when wanting global, instead of net_mode == VSOCK_NET_MODE_GLOBAL. This reflects the new logic
of vsock_net_check_mode() that only requires net pointers (not net_mode).
- refactor sysctl string code into a re-usable function, because child_ns_mode and ns_mode both handle the same strings.
- remove redundant vsock_net_init(&init_net) call in module init because pernet registration calls the callback on the init_net too
Changes in v12:
- return true in dgram_allow(), stream_allow(), and seqpacket_allow() only if net_mode == VSOCK_NET_MODE_GLOBAL (Stefano)
- document bind(VMADDR_CID_ANY) case in af_vsock.c (Stefano)
- change order of stream_allow() call in vmci so we can pass vsk to it
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=NULL + net_mode=VSOCK_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
--- MAINTAINERS | 1 + drivers/vhost/vsock.c | 6 +- include/linux/virtio_vsock.h | 4 +- include/net/af_vsock.h | 61 +++++- include/net/net_namespace.h | 4 + include/net/netns/vsock.h | 21 ++ net/vmw_vsock/af_vsock.c | 335 +++++++++++++++++++++++++++++--- net/vmw_vsock/hyperv_transport.c | 7 +- net/vmw_vsock/virtio_transport.c | 9 +- net/vmw_vsock/virtio_transport_common.c | 6 +- net/vmw_vsock/vmci_transport.c | 26 ++- net/vmw_vsock/vsock_loopback.c | 8 +- 12 files changed, 437 insertions(+), 51 deletions(-)
MAINTAINERS +1 -0
@@ -27549,6 +27549,7 @@ L: [email protected] 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
drivers/vhost/vsock.c +4 -2
@@ -407,7 +407,8 @@ static bool vhost_transport_msgzerocopy_allow(void)return true; } -static bool vhost_transport_seqpacket_allow (u32 remote_cid); +static bool vhost_transport_seqpacket_allow (struct vsock_sock *vsk, +u32 remote_cid);static struct virtio_transport vhost_transport = { .transport = {
@@ -463,7 +464,8 @@ static struct virtio_transport vhost_transport = { .send_pkt = vhost_transport_send_pkt, }; -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 = false;
include/linux/virtio_vsock.h +2 -2
@@ -256,10 +256,10 @@ void virtio_transport_notify_buffer_size(struct vsock_sock *vsk, u64 *val);u64 virtio_transport_stream_rcvhiwat (struct vsock_sock *vsk);bool virtio_transport_stream_is_active (struct vsock_sock *vsk); -bool virtio_transport_stream_allow (u32 cid,u32 port); +bool virtio_transport_stream_allow (struct vsock_sock *vsk,u32 cid,u32 port);int virtio_transport_dgram_bind (struct vsock_sock *vsk,struct sockaddr_vm *addr); -bool virtio_transport_dgram_allow (u32 cid,u32 port); +bool virtio_transport_dgram_allow (struct vsock_sock *vsk,u32 cid,u32 port);int virtio_transport_connect (struct vsock_sock *vsk);
include/net/af_vsock.h +58 -3
95 lines not on this page · raw message
include/net/net_namespace.h +4 -0
@@ -37,6 +37,7 @@#include <net/netns/smc.h> #include <net/netns/bpf.h> #include <net/netns/mctp.h> +#include <net/netns/vsock.h> #include <net/net_trackers.h> #include <linux/ns_common.h> #include <linux/idr.h>
@@ -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;#include <linux/seq_file_net.h>
include/net/netns/vsock.h new file +21 -0
@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __NET_NET_NAMESPACE_VSOCK_H +#define __NET_NET_NAMESPACE_VSOCK_H + +#include <linux/types.h> + +enum vsock_net_mode { +VSOCK_NET_MODE_GLOBAL , +VSOCK_NET_MODE_LOCAL , +}; + +struct netns_vsock { +struct ctl_table_header *sysctl_hdr; + +/* protected by the vsock_table_lock in af_vsock.c */ +u32 port; + +enum vsock_net_mode mode; +enum vsock_net_mode child_ns_mode; +}; +#endif /* __NET_NET_NAMESPACE_VSOCK_H */
net/vmw_vsock/af_vsock.c +308 -27
481 lines not on this page · raw message
net/vmw_vsock/hyperv_transport.c +5 -2
@@ -570,7 +570,7 @@ static int hvs_dgram_enqueue(struct vsock_sock *vsk,return -EOPNOTSUPP ; } -static bool hvs_dgram_allow (u32 cid,u32 port) +static bool hvs_dgram_allow (struct vsock_sock *vsk,u32 cid,u32 port) {return false; }
@@ -745,8 +745,11 @@ static bool hvs_stream_is_active(struct vsock_sock *vsk)return hvs->chan !=NULL ; } -static bool hvs_stream_allow (u32 cid,u32 port) +static bool hvs_stream_allow (struct vsock_sock *vsk,u32 cid,u32 port) { +if (!vsock_net_mode_global (vsk)) +return false; +if (cid ==VMADDR_CID_HOST )return true;
net/vmw_vsock/virtio_transport.c +7 -2
@@ -536,7 +536,8 @@ static bool virtio_transport_msgzerocopy_allow(void)return true; } -static bool virtio_transport_seqpacket_allow (u32 remote_cid); +static bool virtio_transport_seqpacket_allow (struct vsock_sock *vsk, +u32 remote_cid);static struct virtio_transport virtio_transport = { .transport = {
@@ -593,11 +594,15 @@ static struct virtio_transport virtio_transport = { .can_msgzerocopy = virtio_transport_can_msgzerocopy, }; -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; +if (!vsock_net_mode_global (vsk)) +return false; + seqpacket_allow = false;rcu_read_lock (); vsock =rcu_dereference (the_virtio_vsock);
net/vmw_vsock/virtio_transport_common.c +3 -3
@@ -1043,9 +1043,9 @@ bool virtio_transport_stream_is_active(struct vsock_sock *vsk) }EXPORT_SYMBOL_GPL (virtio_transport_stream_is_active); -bool virtio_transport_stream_allow (u32 cid,u32 port) +bool virtio_transport_stream_allow (struct vsock_sock *vsk,u32 cid,u32 port) { -return true; +return vsock_net_mode (sock_net (sk_vsock (vsk))) ==VSOCK_NET_MODE_GLOBAL ; }EXPORT_SYMBOL_GPL (virtio_transport_stream_allow);
@@ -1056,7 +1056,7 @@ int virtio_transport_dgram_bind(struct vsock_sock *vsk, }EXPORT_SYMBOL_GPL (virtio_transport_dgram_bind); -bool virtio_transport_dgram_allow (u32 cid,u32 port) +bool virtio_transport_dgram_allow (struct vsock_sock *vsk,u32 cid,u32 port) {return false; }
net/vmw_vsock/vmci_transport.c +19 -7
@@ -646,13 +646,17 @@ static int vmci_transport_recv_dgram_cb(void *data, struct vmci_datagram *dg)return VMCI_SUCCESS ; } -static bool vmci_transport_stream_allow (u32 cid,u32 port) +static bool vmci_transport_stream_allow (struct vsock_sock *vsk,u32 cid, +u32 port) {static const u32 non_socket_contexts[] = {VMADDR_CID_LOCAL , };int i; +if (!vsock_net_mode_global (vsk)) +return false; +BUILD_BUG_ON (sizeof (cid) !=sizeof (*non_socket_contexts));for (i =0 ; i <ARRAY_SIZE (non_socket_contexts); i++) {
@@ -682,12 +686,10 @@ static int vmci_transport_recv_stream_cb(void *data, struct vmci_datagram *dg) err =VMCI_SUCCESS ; bh_process_pkt = false; -/* Ignore incoming packets from contexts without sockets, or resources -* that aren't vsock implementations. +/* Ignore incoming packets from resources that aren't vsock +* implementations. */ - -if (!vmci_transport_stream_allow (dg->src.context,-1 ) - ||vmci_transport_peer_rid (dg->src.context) != dg->src.resource) +if (vmci_transport_peer_rid (dg->src.context) != dg->src.resource)return VMCI_ERROR_NO_ACCESS ;if (VMCI_DG_SIZE (dg) <sizeof (*pkt))
@@ -749,6 +751,12 @@ static int vmci_transport_recv_stream_cb(void *data, struct vmci_datagram *dg) goto out; } +/* Ignore incoming packets from contexts without sockets. */ +if (!vmci_transport_stream_allow (vsk, dg->src.context,-1 )) { + err =VMCI_ERROR_NO_ACCESS ; + goto out; + } +/* We do most everything in a work queue, but let's fast path the * notification of reads and writes to help data transfer performance. * We can only do this if there is no process context code executing
@@ -1784,8 +1792,12 @@ static int vmci_transport_dgram_dequeue(struct vsock_sock *vsk,return err; } -static bool vmci_transport_dgram_allow (u32 cid,u32 port) +static bool vmci_transport_dgram_allow (struct vsock_sock *vsk,u32 cid, +u32 port) { +if (!vsock_net_mode_global (vsk)) +return false; +if (cid ==VMADDR_CID_HYPERVISOR ) {/* Registrations of PBRPC Servers do not modify VMX/Hypervisor * state and are allowed.
net/vmw_vsock/vsock_loopback.c +5 -3
@@ -46,7 +46,8 @@ static int vsock_loopback_cancel_pkt(struct vsock_sock *vsk)return 0 ; } -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,9 +107,10 @@ static struct virtio_transport loopback_transport = { .send_pkt = vsock_loopback_send_pkt, }; -static bool vsock_loopback_seqpacket_allow (u32 remote_cid) +static bool +vsock_loopback_seqpacket_allow (struct vsock_sock *vsk,u32 remote_cid) { -return true; +return vsock_net_mode_global (vsk); }static void vsock_loopback_work (struct work_struct *work)
Signature · 1 line
2.47.3
Stefano Garzarella
maintainerRe: [PATCH net-next v16 01/12] vsock: add netns to vsock core
Hi,
Quoted 6 lines · Bobby Eshleman
From: Bobby Eshleman <[email protected]>
Add netns logic to vsock core. Additionally, modify transport hook prototypes to be used by later transport-specific patches (e.g.,
*_seqpacket_allow()).
Quoted 8 lines
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_modeto report the mode and/proc/sys/net/vsock/child_ns_modeto set the mode for new namespaces.
talking about this new feature with Daan (in CC) we were discussing a possible change to child_ns_mode.
Currently, if two or more administrator processes in the same namespace set child_ns_mode, they compete. Obviously, after unshare()/clone(), the process can always access ns_mode to check if everything went well and eventually retry.
Daan suggested a more conservative approach, allowing child_ns_mode to be written only once (a bit like we did in the old version when the child could change the mode only once). This way, most users who want isolation write local in child_ns_mode at startup in the init_ns. At that point the user and can be sure that no other process (including administrators, e.g., container managers) can change it, so all new namespaces will have local mode.
I think we should support this option in some way, because it seems to simplify the user space in most common cases (ensure isolation). I see few options for doing this:
1. Change the behavior of child_ns_mode to be written only once, but this would limit other possible use cases where child_ns_mode can be changed more than once (I don't know if Bobby had any in mind).
2. Add a new sysctl child_ns_mode_lockin (or something similar), which can only be written once with a mode (local or global). A write on this will also locks child_ns_mode, of course.
3. Add a new local-locked mode, reusing the same sysctl.
If we go for 1, maybe we can do it in 7.0, or not?
2 and 3, on the other hand, may have to wait until the next release.
What do you think? Any comments?
Thanks,
Stefano
Jakub Kicinski
maintainerRe: [PATCH net-next v16 01/12] vsock: add netns to vsock core
Quoted 1 line · On Tue, 17 Feb 2026 16:08:33 +0100 Stefano Garzarella wrote:
If we go for 1, maybe we can do it in 7.0, or not?
Just my preference but changing the behavior ASAP in 7.0 seems better, but the code must be ready very soon for that to happen.
PATCH v16 2/12 virtio: set skb owner of virtio_transport_reset_no_sock() reply
In reply to Bobby EshlemanFrom: Bobby Eshleman <[email protected]>
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.
---
Changes in v11:
- move before adding to netns support (Stefano)
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(+)
net/vmw_vsock/virtio_transport_common.c +6 -0
@@ -1165,6 +1165,12 @@ static int virtio_transport_reset_no_sock(const struct virtio_transport *t, .op = VIRTIO_VSOCK_OP_RST, .type = le16_to_cpu(hdr->type), .reply = true, + +/* 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 = vsock_sk(skb->sk), };struct sk_buff *reply;
Signature · 1 line
2.47.3
PATCH v16 3/12 vsock: add netns support to virtio transports
In reply to Bobby EshlemanFrom: Bobby Eshleman <[email protected]>
Add netns support to loopback and vhost. Keep netns disabled for virtio-vsock, but add necessary changes to comply with common API updates.
This is the patch in the series when vhost-vsock namespaces actually come online.
---
Changes in v15:
- add vsock_net_mode_global() (Stefano)
Changes in v14:
- fixed merge conflicts in drivers/vhost/vsock.c
Changes in v13:
- do not store or pass the mode around now that net->vsock.mode is immutable
- move virtio_transport_stream_allow() into virtio_transport.c because virtio is the only caller now
Changes in v12:
- change seqpacket_allow() and stream_allow() to return true for loopback and vhost (Stefano)
Changes in v11:
- reorder with the skb ownership patch for loopback (Stefano)
- toggle vhost_transport_supports_local_mode() to true
Changes in v10:
- Splitting patches complicates the series with meaningless placeholder values 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/[email protected]/
- Link: https://lore.kernel.org/all/[email protected]/
- Link: https://lore.kernel.org/all/[email protected]/
- remove placeholder values (Stefano)
- update comment describe net/net_mode for virtio_transport_reset_no_sock()
--- drivers/vhost/vsock.c | 38 ++++++++++++++++------- include/linux/virtio_vsock.h | 5 +-- net/vmw_vsock/virtio_transport.c | 13 ++++++-- net/vmw_vsock/virtio_transport_common.c | 54 +++++++++++++++++++-------------- net/vmw_vsock/vsock_loopback.c | 14 +++++++-- 5 files changed, 84 insertions(+), 40 deletions(-)
drivers/vhost/vsock.c +27 -11
@@ -48,6 +48,8 @@ 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;/* Link to global vhost_vsock_hash, writes use vhost_vsock_mutex */ struct hlist_node hash;
@@ -69,7 +71,7 @@ static u32 vhost_transport_get_local_cid(void)/* Callers must be in an RCU read section or hold the vhost_vsock_mutex. * The return value can only be dereferenced while within the section. */ -static struct vhost_vsock *vhost_vsock_get (u32 guest_cid) +static struct vhost_vsock *vhost_vsock_get (u32 guest_cid,struct net *net) {struct vhost_vsock *vsock;
@@ -81,9 +83,9 @@ static struct vhost_vsock *vhost_vsock_get(u32 guest_cid)if (other_cid ==0 )continue ; -if (other_cid == guest_cid) +if (other_cid == guest_cid && +vsock_net_check_mode (net, vsock->net))return vsock; - }return NULL ;
@@ -272,7 +274,7 @@ static void vhost_transport_send_pkt_work(struct vhost_work *work) }static int -vhost_transport_send_pkt (struct sk_buff *skb) +vhost_transport_send_pkt (struct sk_buff *skb,struct net *net) {struct virtio_vsock_hdr *hdr =virtio_vsock_hdr (skb);struct vhost_vsock *vsock;
@@ -281,7 +283,7 @@ vhost_transport_send_pkt(struct sk_buff *skb)rcu_read_lock ();/* Find the vhost_vsock according to guest context id */ - vsock =vhost_vsock_get (le64_to_cpu (hdr->dst_cid)); + vsock =vhost_vsock_get (le64_to_cpu (hdr->dst_cid), net);if (!vsock) {rcu_read_unlock ();kfree_skb (skb);
@@ -308,7 +310,8 @@ vhost_transport_cancel_pkt(struct vsock_sock *vsk)rcu_read_lock ();/* Find the vhost_vsock according to guest context id */ - vsock =vhost_vsock_get (vsk->remote_addr.svm_cid); + vsock =vhost_vsock_get (vsk->remote_addr.svm_cid, +sock_net (sk_vsock (vsk)));if (!vsock) goto out;
@@ -410,6 +413,12 @@ static bool vhost_transport_msgzerocopy_allow(void)static bool vhost_transport_seqpacket_allow (struct vsock_sock *vsk,u32 remote_cid); +static bool +vhost_transport_stream_allow (struct vsock_sock *vsk,u32 cid,u32 port) +{ +return true; +} +static struct virtio_transport vhost_transport = { .transport = { .module =THIS_MODULE ,
@@ -434,7 +443,7 @@ static struct virtio_transport vhost_transport = { .stream_has_space = virtio_transport_stream_has_space, .stream_rcvhiwat = virtio_transport_stream_rcvhiwat, .stream_is_active = virtio_transport_stream_is_active, - .stream_allow = virtio_transport_stream_allow, + .stream_allow = vhost_transport_stream_allow, .seqpacket_dequeue = virtio_transport_seqpacket_dequeue, .seqpacket_enqueue = virtio_transport_seqpacket_enqueue,
@@ -467,11 +476,12 @@ static struct virtio_transport vhost_transport = {static bool vhost_transport_seqpacket_allow (struct vsock_sock *vsk,u32 remote_cid) { +struct net *net =sock_net (sk_vsock (vsk));struct vhost_vsock *vsock;bool seqpacket_allow = false;rcu_read_lock (); - vsock =vhost_vsock_get (remote_cid); + vsock =vhost_vsock_get (remote_cid, net);if (vsock) seqpacket_allow = vsock->seqpacket_allow;
@@ -542,7 +552,8 @@ static void vhost_vsock_handle_tx_kick(struct vhost_work *work)if (le64_to_cpu (hdr->src_cid) == vsock->guest_cid &&le64_to_cpu (hdr->dst_cid) ==vhost_transport_get_local_cid ()) -virtio_transport_recv_pkt (&vhost_transport, skb); +virtio_transport_recv_pkt (&vhost_transport, skb, + vsock->net);else kfree_skb (skb);
@@ -659,6 +670,7 @@ static int vhost_vsock_dev_open(struct inode *inode, struct file *file) {struct vhost_virtqueue **vqs;struct vhost_vsock *vsock; +struct net *net;int ret;/* This struct is large and allocation could fail, fall back to vmalloc
@@ -674,6 +686,9 @@ static int vhost_vsock_dev_open(struct inode *inode, struct file *file) goto out; } + net = current->nsproxy->net_ns; + vsock->net =get_net_track (net, &vsock->ns_tracker,GFP_KERNEL ); + vsock->guest_cid =0 ;/* no CID assigned yet */ vsock->seqpacket_allow = false;
@@ -715,7 +730,7 @@ static void vhost_vsock_reset_orphans(struct sock *sk)rcu_read_lock ();/* 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))) {rcu_read_unlock ();return ; }
@@ -764,6 +779,7 @@ static int vhost_vsock_dev_release(struct inode *inode, struct file *file)virtio_vsock_skb_queue_purge (&vsock->send_pkt_queue);vhost_dev_cleanup (&vsock->dev); +put_net_track (vsock->net, &vsock->ns_tracker);kfree (vsock->dev.vqs);vhost_vsock_free (vsock);return 0 ;
@@ -790,7 +806,7 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)/* Refuse if CID is already in use */ mutex_lock (&vhost_vsock_mutex); - other =vhost_vsock_get (guest_cid); + other =vhost_vsock_get (guest_cid, vsock->net);if (other && other != vsock) {mutex_unlock (&vhost_vsock_mutex);return -EADDRINUSE ;
include/linux/virtio_vsock.h +3 -2
@@ -173,6 +173,7 @@ struct virtio_vsock_pkt_info {u32 remote_cid, remote_port;struct vsock_sock *vsk;struct msghdr *msg; +struct net *net;u32 pkt_len;u16 type;u16 op;
@@ -185,7 +186,7 @@ struct virtio_transport {struct vsock_transport transport;/* Takes ownership of the packet */ -int (*send_pkt)(struct sk_buff *skb); +int (*send_pkt)(struct sk_buff *skb,struct net *net);/* Used in MSG_ZEROCOPY mode. Checks, that provided data * (number of buffers) could be transmitted with zerocopy
@@ -280,7 +281,7 @@ virtio_transport_dgram_enqueue(struct vsock_sock *vsk,void virtio_transport_destruct (struct vsock_sock *vsk);void virtio_transport_recv_pkt (struct virtio_transport *t, -struct sk_buff *skb); +struct sk_buff *skb,struct net *net);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);
net/vmw_vsock/virtio_transport.c +11 -2
@@ -231,7 +231,7 @@ static int virtio_transport_send_skb_fast_path(struct virtio_vsock *vsock, struc }static int -virtio_transport_send_pkt (struct sk_buff *skb) +virtio_transport_send_pkt (struct sk_buff *skb,struct net *net) {struct virtio_vsock_hdr *hdr;struct virtio_vsock *vsock;
@@ -536,6 +536,11 @@ static bool virtio_transport_msgzerocopy_allow(void)return true; } +bool virtio_transport_stream_allow (struct vsock_sock *vsk,u32 cid,u32 port) +{ +return vsock_net_mode_global (vsk); +} +static bool virtio_transport_seqpacket_allow (struct vsock_sock *vsk,u32 remote_cid);
@@ -665,7 +670,11 @@ static void virtio_transport_rx_work(struct work_struct *work)virtio_vsock_skb_put (skb, payload_len);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 ); } }while (!virtqueue_enable_cb (vq));
net/vmw_vsock/virtio_transport_common.c +32 -22
186 lines not on this page · raw message
net/vmw_vsock/vsock_loopback.c +11 -3
@@ -26,7 +26,7 @@ static u32 vsock_loopback_get_local_cid(void)return VMADDR_CID_LOCAL ; } -static int vsock_loopback_send_pkt (struct sk_buff *skb) +static int vsock_loopback_send_pkt (struct sk_buff *skb,struct net *net) {struct vsock_loopback *vsock = &the_vsock_loopback;int len = skb->len;
@@ -48,6 +48,13 @@ static int vsock_loopback_cancel_pkt(struct vsock_sock *vsk)static bool vsock_loopback_seqpacket_allow (struct vsock_sock *vsk,u32 remote_cid); + +static bool vsock_loopback_stream_allow (struct vsock_sock *vsk,u32 cid, +u32 port) +{ +return true; +} +static bool vsock_loopback_msgzerocopy_allow (void ) {return true;
@@ -77,7 +84,7 @@ static struct virtio_transport loopback_transport = { .stream_has_space = virtio_transport_stream_has_space, .stream_rcvhiwat = virtio_transport_stream_rcvhiwat, .stream_is_active = virtio_transport_stream_is_active, - .stream_allow = virtio_transport_stream_allow, + .stream_allow = vsock_loopback_stream_allow, .seqpacket_dequeue = virtio_transport_seqpacket_dequeue, .seqpacket_enqueue = virtio_transport_seqpacket_enqueue,
@@ -132,7 +139,8 @@ static void vsock_loopback_work(struct work_struct *work)*/ 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)); } }
Signature · 1 line
2.47.3
PATCH v16 4/12 selftests/vsock: increase timeout to 1200
In reply to Bobby EshlemanFrom: Bobby Eshleman <[email protected]>
Increase the timeout from 300s to 1200s. On a modern bare metal server my last run showed the new set of tests taking ~400s. Multiply by an (arbitrary) factor of three to account for slower/nested runners.
--- tools/testing/selftests/vsock/settings | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/vsock/settings +1 -1
@@ -1 +1 @@ -timeout=300 +timeout=1200
Signature · 1 line
2.47.3
PATCH v16 5/12 selftests/vsock: add namespace helpers to vmtest.sh
In reply to Bobby EshlemanFrom: Bobby Eshleman <[email protected]>
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 add_namespaces() function initializes global0, local0, etc... with their respective vsock NS mode by toggling child_ns_mode before creating the namespace.
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.
This patch is in preparation for later namespace tests.
---
Changes in v13:
- intialize namespaces to use the child_ns_mode mechanism
- remove setting modes from init_namespaces() function (this function only sets up the lo device now)
- remove ns_set_mode(ns) because ns_mode is no longer mutable
--- tools/testing/selftests/vsock/vmtest.sh | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)
tools/testing/selftests/vsock/vmtest.sh +32 -0
@@ -49,6 +49,7 @@ readonly TEST_DESCS=( ) readonly USE_SHARED_VM=(vm_server_host_client vm_client_host_server vm_loopback) +readonly NS_MODES=("local" "global") VERBOSE=0
@@ -103,6 +104,36 @@ check_result() { fi } +add_namespaces() { + local orig_mode + orig_mode=$(cat /proc/sys/net/vsock/child_ns_mode) + + for mode in "${NS_MODES[@]}"; do + echo "${mode}" > /proc/sys/net/vsock/child_ns_mode + ip netns add "${mode}0" 2>/dev/null + ip netns add "${mode}1" 2>/dev/null + done + + echo "${orig_mode}" > /proc/sys/net/vsock/child_ns_mode +} + +init_namespaces() { + for mode in "${NS_MODES[@]}"; do + # 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 +} + vm_ssh() { ssh -q -o UserKnownHostsFile=/dev/null -p ${SSH_HOST_PORT} localhost "$@" return $?
@@ -110,6 +141,7 @@ vm_ssh() { cleanup() { terminate_pidfiles "${!PIDFILES[@]}" + del_namespaces } check_args() {
Signature · 1 line
2.47.3
PATCH v16 6/12 selftests/vsock: prepare vm management helpers for namespaces
In reply to Bobby EshlemanFrom: Bobby Eshleman <[email protected]>
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.
---
Changes in v16:
- add "init_ns" to vm_ssh calls in run_shared_vm_tests
--- tools/testing/selftests/vsock/vmtest.sh | 101 ++++++++++++++++++++++---------- 1 file changed, 69 insertions(+), 32 deletions(-)
tools/testing/selftests/vsock/vmtest.sh +69 -32
@@ -135,7 +135,18 @@ del_namespaces() { } vm_ssh() { - ssh -q -o UserKnownHostsFile=/dev/null -p ${SSH_HOST_PORT} localhost "$@" + local ns_exec + + if [[ "${1}" == init_ns ]]; then + ns_exec="" + else + ns_exec="ip netns exec ${1}" + fi + + shift + + ${ns_exec} ssh -q -o UserKnownHostsFile=/dev/null -p "${SSH_HOST_PORT}" localhost "$@" + return $? }
@@ -258,10 +269,12 @@ terminate_pidfiles() { vm_start() { local pidfile=$1 + local ns=$2 local logfile=/dev/null local verbose_opt="" local kernel_opt="" local qemu_opts="" + local ns_exec="" local qemu qemu=$(command -v "${QEMU}")
@@ -282,7 +295,11 @@ vm_start() { kernel_opt="${KERNEL_CHECKOUT}" fi - vng \ + if [[ "${ns}" != "init_ns" ]]; then + ns_exec="ip netns exec ${ns}" + fi + + ${ns_exec} vng \ --run \ ${kernel_opt} \ ${verbose_opt} \
@@ -297,6 +314,7 @@ vm_start() { } vm_wait_for_ssh() { + local ns=$1 local i i=0
@@ -304,7 +322,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=$(( i + 1 ))
@@ -338,30 +357,41 @@ wait_for_listener() } vm_wait_for_listener() { - local port=$1 + local ns=$1 + local port=$2 - vm_ssh <<EOF + vm_ssh "${ns}" <<EOF $(declare -f wait_for_listener) wait_for_listener ${port} ${WAIT_PERIOD} ${WAIT_PERIOD_MAX} EOF } host_wait_for_listener() { - local port=$1 + local ns=$1 + local port=$2 - wait_for_listener "${port}" "${WAIT_PERIOD}" "${WAIT_PERIOD_MAX}" + if [[ "${ns}" == "init_ns" ]]; then + wait_for_listener "${port}" "${WAIT_PERIOD}" "${WAIT_PERIOD_MAX}" + else + ip netns exec "${ns}" bash <<-EOF + $(declare -f wait_for_listener) + wait_for_listener ${port} ${WAIT_PERIOD} ${WAIT_PERIOD_MAX} + EOF + fi } + vm_vsock_test() { - local host=$1 - local cid=$2 - local port=$3 + local ns=$1 + local host=$2 + local cid=$3 + local port=$4 local rc # log output and use pipefail to respect vsock_test errors set -o pipefail if [[ "${host}" != server ]]; then - vm_ssh -- "${VSOCK_TEST}" \ + vm_ssh "${ns}" -- "${VSOCK_TEST}" \ --mode=client \ --control-host="${host}" \ --peer-cid="${cid}" \
@@ -369,7 +399,7 @@ vm_vsock_test() { 2>&1 | log_guest rc=$? else - vm_ssh -- "${VSOCK_TEST}" \ + vm_ssh "${ns}" -- "${VSOCK_TEST}" \ --mode=server \ --peer-cid="${cid}" \ --control-port="${port}" \
@@ -381,7 +411,7 @@ vm_vsock_test() { return $rc fi - vm_wait_for_listener "${port}" + vm_wait_for_listener "${ns}" "${port}" rc=$? fi set +o pipefail
@@ -390,22 +420,28 @@ vm_vsock_test() { } host_vsock_test() { - local host=$1 - local cid=$2 - local port=$3 + local ns=$1 + local host=$2 + local cid=$3 + local port=$4 local rc + local cmd="${VSOCK_TEST}" + if [[ "${ns}" != "init_ns" ]]; then + cmd="ip netns exec ${ns} ${cmd}" + fi + # log output and use pipefail to respect vsock_test errors set -o pipefail if [[ "${host}" != server ]]; then - ${VSOCK_TEST} \ + ${cmd} \ --mode=client \ --peer-cid="${cid}" \ --control-host="${host}" \ --control-port="${port}" 2>&1 | log_host rc=$? else - ${VSOCK_TEST} \ + ${cmd} \ --mode=server \ --peer-cid="${cid}" \ --control-port="${port}" 2>&1 | log_host &
@@ -416,7 +452,7 @@ host_vsock_test() { return $rc fi - host_wait_for_listener "${port}" + host_wait_for_listener "${ns}" "${port}" rc=$? fi set +o pipefail
@@ -460,11 +496,11 @@ log_guest() { } 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 - 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_PORT}"; then return "${KSFT_FAIL}" fi
@@ -472,11 +508,11 @@ test_vm_server_host_client() { } 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 - 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}"; then return "${KSFT_FAIL}" fi
@@ -486,13 +522,14 @@ test_vm_client_host_server() { test_vm_loopback() { local port=60000 # non-forwarded local port - vm_ssh -- modprobe vsock_loopback &> /dev/null || : + vm_ssh "init_ns" -- modprobe vsock_loopback &> /dev/null || : - if ! vm_vsock_test "server" 1 "${port}"; then + if ! vm_vsock_test "init_ns" "server" 1 "${port}"; then return "${KSFT_FAIL}" fi - 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
@@ -550,8 +587,8 @@ run_shared_vm_test() { host_oops_cnt_before=$(dmesg | grep -c -i 'Oops') host_warn_cnt_before=$(dmesg --level=warn | grep -c -i 'vsock') - vm_oops_cnt_before=$(vm_ssh -- dmesg | grep -c -i 'Oops') - vm_warn_cnt_before=$(vm_ssh -- dmesg --level=warn | grep -c -i 'vsock') + vm_oops_cnt_before=$(vm_ssh "init_ns" -- dmesg | grep -c -i 'Oops') + vm_warn_cnt_before=$(vm_ssh "init_ns" -- dmesg --level=warn | grep -c -i 'vsock') name=$(echo "${1}" | awk '{ print $1 }') eval test_"${name}"
@@ -569,13 +606,13 @@ run_shared_vm_test() { rc=$KSFT_FAIL fi - vm_oops_cnt_after=$(vm_ssh -- dmesg | grep -i 'Oops' | wc -l) + vm_oops_cnt_after=$(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=$KSFT_FAIL fi - vm_warn_cnt_after=$(vm_ssh -- dmesg --level=warn | grep -c -i 'vsock') + vm_warn_cnt_after=$(vm_ssh "init_ns" -- dmesg --level=warn | 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=$KSFT_FAIL
@@ -621,8 +658,8 @@ cnt_total=0 if shared_vm_tests_requested "${ARGS[@]}"; then log_host "Booting up VM" pidfile="$(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" run_shared_vm_tests "${ARGS[@]}"
Signature · 1 line
2.47.3
PATCH v16 7/12 selftests/vsock: add vm_dmesg_{warn,oops}_count() helpers
In reply to Bobby EshlemanFrom: Bobby Eshleman <[email protected]>
These functions are reused by the VM tests to collect and compare dmesg warnings and oops counts. The future VM-specific tests use them heavily. This patches relies on vm_ssh() already supporting namespaces.
---
Changes in v11:
- break these out into an earlier patch so that they can be used directly in new patches (instead of causing churn by adding this later)
--- tools/testing/selftests/vsock/vmtest.sh | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)
tools/testing/selftests/vsock/vmtest.sh +15 -4
@@ -380,6 +380,17 @@ host_wait_for_listener() { fi } +vm_dmesg_oops_count() { + local ns=$1 + + vm_ssh "${ns}" -- dmesg 2>/dev/null | grep -c -i 'Oops' +} + +vm_dmesg_warn_count() { + local ns=$1 + + vm_ssh "${ns}" -- dmesg --level=warn 2>/dev/null | grep -c -i 'vsock' +} vm_vsock_test() { local ns=$1
@@ -587,8 +598,8 @@ run_shared_vm_test() { host_oops_cnt_before=$(dmesg | grep -c -i 'Oops') host_warn_cnt_before=$(dmesg --level=warn | grep -c -i 'vsock') - vm_oops_cnt_before=$(vm_ssh "init_ns" -- dmesg | grep -c -i 'Oops') - vm_warn_cnt_before=$(vm_ssh "init_ns" -- dmesg --level=warn | grep -c -i 'vsock') + vm_oops_cnt_before=$(vm_dmesg_oops_count "init_ns") + vm_warn_cnt_before=$(vm_dmesg_warn_count "init_ns") name=$(echo "${1}" | awk '{ print $1 }') eval test_"${name}"
@@ -606,13 +617,13 @@ run_shared_vm_test() { rc=$KSFT_FAIL fi - vm_oops_cnt_after=$(vm_ssh "init_ns" -- dmesg | grep -i 'Oops' | wc -l) + vm_oops_cnt_after=$(vm_dmesg_oops_count "init_ns") if [[ ${vm_oops_cnt_after} -gt ${vm_oops_cnt_before} ]]; then echo "FAIL: kernel oops detected on vm" | log_host rc=$KSFT_FAIL fi - vm_warn_cnt_after=$(vm_ssh "init_ns" -- dmesg --level=warn | grep -c -i 'vsock') + vm_warn_cnt_after=$(vm_dmesg_warn_count "init_ns") if [[ ${vm_warn_cnt_after} -gt ${vm_warn_cnt_before} ]]; then echo "FAIL: kernel warning detected on vm" | log_host rc=$KSFT_FAIL
Signature · 1 line
2.47.3
PATCH v16 8/12 selftests/vsock: use ss to wait for listeners instead of /proc/net
In reply to Bobby EshlemanFrom: Bobby Eshleman <[email protected]>
Replace /proc/net parsing with ss(8) for detecting listening sockets in wait_for_listener() functions and add support for TCP, VSOCK, and Unix socket protocols.
The previous implementation parsed /proc/net/tcp using awk to detect listening sockets, but this approach could not support vsock because vsock does not export socket information to /proc/net/.
Instead, use ss so that we can detect listeners on tcp, vsock, and unix.
The protocol parameter is now required for all wait_for_listener family functions (wait_for_listener, vm_wait_for_listener, host_wait_for_listener) to explicitly specify which socket type to wait for.
ss is added to the dependency check in check_deps().
--- tools/testing/selftests/vsock/vmtest.sh | 47 +++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 17 deletions(-)
tools/testing/selftests/vsock/vmtest.sh +30 -17
@@ -182,7 +182,7 @@ check_args() { } check_deps() { - for dep in vng ${QEMU} busybox pkill ssh; do + for dep in vng ${QEMU} busybox pkill ssh ss; do if [[ ! -x $(command -v "${dep}") ]]; then echo -e "skip: dependency ${dep} not found!\n" exit "${KSFT_SKIP}"
@@ -337,21 +337,32 @@ wait_for_listener() local port=$1 local interval=$2 local max_intervals=$3 - local protocol=tcp - local pattern + local protocol=$4 local i - pattern=":$(printf "%04X" "${port}") " - - # for tcp protocol additionally check the socket state - [ "${protocol}" = "tcp" ] && pattern="${pattern}0A" - for i in $(seq "${max_intervals}"); do - if awk -v pattern="${pattern}" \ - 'BEGIN {rc=1} $2" "$4 ~ pattern {rc=0} END {exit rc}' \ - /proc/net/"${protocol}"*; then + case "${protocol}" in + tcp) + if ss --listening --tcp --numeric | grep -q ":${port} "; then + break + fi + ;; + vsock) + if ss --listening --vsock --numeric | grep -q ":${port} "; then + break + fi + ;; + unix) + # For unix sockets, port is actually the socket path + if ss --listening --unix | grep -q "${port}"; then + break + fi + ;; + *) + echo "Unknown protocol: ${protocol}" >&2 break - fi + ;; + esac sleep "${interval}" done }
@@ -359,23 +370,25 @@ wait_for_listener() vm_wait_for_listener() { local ns=$1 local port=$2 + local protocol=$3 vm_ssh "${ns}" <<EOF $(declare -f wait_for_listener) -wait_for_listener ${port} ${WAIT_PERIOD} ${WAIT_PERIOD_MAX} +wait_for_listener ${port} ${WAIT_PERIOD} ${WAIT_PERIOD_MAX} ${protocol} EOF } host_wait_for_listener() { local ns=$1 local port=$2 + local protocol=$3 if [[ "${ns}" == "init_ns" ]]; then - wait_for_listener "${port}" "${WAIT_PERIOD}" "${WAIT_PERIOD_MAX}" + wait_for_listener "${port}" "${WAIT_PERIOD}" "${WAIT_PERIOD_MAX}" "${protocol}" else ip netns exec "${ns}" bash <<-EOF $(declare -f wait_for_listener) - wait_for_listener ${port} ${WAIT_PERIOD} ${WAIT_PERIOD_MAX} + wait_for_listener ${port} ${WAIT_PERIOD} ${WAIT_PERIOD_MAX} ${protocol} EOF fi }
@@ -422,7 +435,7 @@ vm_vsock_test() { return $rc fi - vm_wait_for_listener "${ns}" "${port}" + vm_wait_for_listener "${ns}" "${port}" "tcp" rc=$? fi set +o pipefail
@@ -463,7 +476,7 @@ host_vsock_test() { return $rc fi - host_wait_for_listener "${ns}" "${port}" + host_wait_for_listener "${ns}" "${port}" "tcp" rc=$? fi set +o pipefail
Signature · 1 line
2.47.3
PATCH v16 9/12 selftests/vsock: add tests for proc sys vsock ns_mode
In reply to Bobby EshlemanFrom: Bobby Eshleman <[email protected]>
Add tests for the /proc/sys/net/vsock/{ns_mode,child_ns_mode} interfaces. Namely, that they accept/report "global" and "local" strings and enforce their access policies.
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.
This patch is the first to add tests that do *not* re-use the same shared VM. For that reason, it adds a run_ns_tests() function to run these tests and filter out the shared VM tests.
---
Changes in v13:
- remove write-once test ns_host_vsock_ns_mode_write_once_ok to reflect removing the write-once policy
- add child_ns_mode test test_ns_host_vsock_child_ns_mode_ok
- modify test_ns_host_vsock_ns_mode_ok() to check that the correct mode was inherited from child_ns_mode
Changes in v12:
- remove ns_vm_local_mode_rejected test, due to dropping that constraint
Changes in v11:
- Document ns_ prefix above TEST_NAMES (Stefano)
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(-)
tools/testing/selftests/vsock/vmtest.sh +138 -2
@@ -41,14 +41,38 @@ readonly KERNEL_CMDLINE="\ virtme.ssh virtme_ssh_channel=tcp virtme_ssh_user=$USER \ " readonly LOG=$(mktemp /tmp/vsock_vmtest_XXXX.log) -readonly TEST_NAMES=(vm_server_host_client vm_client_host_server vm_loopback) + +# Namespace tests must use the ns_ prefix. This is checked in check_netns() and +# is used to determine if a test needs namespace setup before test execution. +readonly TEST_NAMES=( + vm_server_host_client + vm_client_host_server + vm_loopback + ns_host_vsock_ns_mode_ok + ns_host_vsock_child_ns_mode_ok +) readonly TEST_DESCS=( + # 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_child_ns_mode_ok + "Check /proc/sys/net/vsock/ns_mode is read-only and child_ns_mode is writable." ) -readonly USE_SHARED_VM=(vm_server_host_client vm_client_host_server vm_loopback) +readonly USE_SHARED_VM=( + vm_server_host_client + vm_client_host_server + vm_loopback +) readonly NS_MODES=("local" "global") VERBOSE=0
@@ -196,6 +220,20 @@ check_deps() { fi } +check_netns() { + local tname=$1 + + # If the test requires NS support, check if NS support exists + # using /proc/self/ns + if [[ "${tname}" =~ ^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
@@ -519,6 +557,54 @@ log_guest() { LOG_PREFIX=guest log "$@" } +ns_get_mode() { + local ns=$1 + + ip netns exec "${ns}" cat /proc/sys/net/vsock/ns_mode 2>/dev/null +} + +test_ns_host_vsock_ns_mode_ok() { + for mode in "${NS_MODES[@]}"; do + local actual + + actual=$(ns_get_mode "${mode}0") + if [[ "${actual}" != "${mode}" ]]; then + log_host "expected mode ${mode}, got ${actual}" + return "${KSFT_FAIL}" + fi + done + + return "${KSFT_PASS}" +} + +test_ns_host_vsock_child_ns_mode_ok() { + local orig_mode + local rc + + orig_mode=$(cat /proc/sys/net/vsock/child_ns_mode) + + rc="${KSFT_PASS}" + for mode in "${NS_MODES[@]}"; do + local ns="${mode}0" + + if echo "${mode}" 2>/dev/null > /proc/sys/net/vsock/ns_mode; then + log_host "ns_mode should be read-only but write succeeded" + rc="${KSFT_FAIL}" + continue + fi + + if ! echo "${mode}" > /proc/sys/net/vsock/child_ns_mode; then + log_host "child_ns_mode should be writable to ${mode}" + rc="${KSFT_FAIL}" + continue + fi + done + + echo "${orig_mode}" > /proc/sys/net/vsock/child_ns_mode + + return "${rc}" +} + test_vm_server_host_client() { if ! vm_vsock_test "init_ns" "server" 2 "${TEST_GUEST_PORT}"; then return "${KSFT_FAIL}"
@@ -592,6 +678,11 @@ run_shared_vm_tests() { continue fi + if ! check_netns "${arg}"; then + check_result "${KSFT_SKIP}" "${arg}" + continue + fi + run_shared_vm_test "${arg}" check_result "$?" "${arg}" done
@@ -645,6 +736,49 @@ run_shared_vm_test() { return "${rc}" } +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=$(echo "${arg}" | awk '{ print $1 }') + log_host "Executing test_${name}" + + host_oops_before=$(dmesg 2>/dev/null | grep -c -i 'Oops') + host_warn_before=$(dmesg --level=warn 2>/dev/null | grep -c -i 'vsock') + eval test_"${name}" + rc=$? + + host_oops_after=$(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=$(dmesg --level=warn 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=0 QEMU="qemu-system-$(uname -m)"
@@ -690,6 +824,8 @@ if shared_vm_tests_requested "${ARGS[@]}"; then terminate_pidfiles "${pidfile}" fi +run_ns_tests "${ARGS[@]}" + echo "SUMMARY: PASS=${cnt_pass} SKIP=${cnt_skip} FAIL=${cnt_fail}" echo "Log: ${LOG}"
Signature · 1 line
2.47.3
PATCH v16 10/12 selftests/vsock: add namespace tests for CID collisions
In reply to Bobby EshlemanFrom: Bobby Eshleman <[email protected]>
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.
---
Changes in v11:
- check vm_start() rc in namespaces_can_boot_same_cid() (Stefano)
- fix ns_local_same_cid_ok() to use local0 and local1 instead of reusing local0 twice. This check should pass, ensuring local namespaces do not collide (Stefano)
--- tools/testing/selftests/vsock/vmtest.sh | 78 +++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+)
tools/testing/selftests/vsock/vmtest.sh +78 -0
@@ -50,6 +50,10 @@ readonly TEST_NAMES=( vm_loopback ns_host_vsock_ns_mode_ok ns_host_vsock_child_ns_mode_ok + 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=( # vm_server_host_client
@@ -66,6 +70,18 @@ readonly TEST_DESCS=( # ns_host_vsock_child_ns_mode_ok "Check /proc/sys/net/vsock/ns_mode is read-only and child_ns_mode is writable." + + # 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 local 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." ) readonly USE_SHARED_VM=(
@@ -577,6 +593,68 @@ test_ns_host_vsock_ns_mode_ok() { return "${KSFT_PASS}" } +namespaces_can_boot_same_cid() { + local ns0=$1 + local ns1=$2 + local pidfile1 pidfile2 + local rc + + pidfile1="$(create_pidfile)" + + # The first VM should be able to start. If it can't then we have + # problems and need to return non-zero. + if ! vm_start "${pidfile1}" "${ns0}"; then + return 1 + fi + + pidfile2="$(create_pidfile)" + vm_start "${pidfile2}" "${ns1}" + rc=$? + 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" "local1"; then + return "${KSFT_PASS}" + fi + + return "${KSFT_FAIL}" +} + test_ns_host_vsock_child_ns_mode_ok() { local orig_mode local rc
Signature · 1 line
2.47.3
PATCH v16 11/12 selftests/vsock: add tests for host <-> vm connectivity with namespaces
In reply to Bobby EshlemanFrom: Bobby Eshleman <[email protected]>
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_* helpers to encapsulate checking dmesg for oops and warnings.
Add ability to pass extra args to host-side vsock_test so that tests that cause false positives may be skipped with arg --skip.
---
Changes in v12:
- add test skip (vsock_test test 29) when host_vsock_test() uses client mode in a local namespace. Test 29 causes a false positive to trigger.
Changes in v11:
- add 'sleep "${WAIT_PERIOD}"' after any non-TCP socat LISTEN cmd (Stefano)
- add host_wait_for_listener() after any socat TCP-LISTEN (Stefano)
- reuse vm_dmesg_{oops,warn}_count() inside vm_dmesg_check()
- fix copy-paste in test_ns_same_local_vm_connect_to_local_host_ok() (Stefano)
Changes in v10:
- add vm_dmesg_start() and vm_dmesg_check()
Changes in v9:
- consistent variable quoting
--- tools/testing/selftests/vsock/vmtest.sh | 572 +++++++++++++++++++++++++++++++- 1 file changed, 568 insertions(+), 4 deletions(-)
tools/testing/selftests/vsock/vmtest.sh +568 -4
@@ -7,6 +7,7 @@ # * virtme-ng # * busybox-static (used by virtme-ng) # * qemu (used by virtme-ng) +# * socat # # shellcheck disable=SC2317,SC2119
@@ -54,6 +55,19 @@ readonly TEST_NAMES=( 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=( # vm_server_host_client
@@ -82,6 +96,45 @@ readonly TEST_DESCS=( # 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 ns." + + # 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 vsock 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 vsock 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 to 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." ) readonly USE_SHARED_VM=(
@@ -112,7 +165,7 @@ usage() { for ((i = 0; i < ${#TEST_NAMES[@]}; i++)); do name=${TEST_NAMES[${i}]} desc=${TEST_DESCS[${i}]} - printf "\t%-35s%-35s\n" "${name}" "${desc}" + printf "\t%-55s%-35s\n" "${name}" "${desc}" done echo
@@ -222,7 +275,7 @@ check_args() { } check_deps() { - for dep in vng ${QEMU} busybox pkill ssh ss; do + for dep in vng ${QEMU} busybox pkill ssh ss socat; do if [[ ! -x $(command -v "${dep}") ]]; then echo -e "skip: dependency ${dep} not found!\n" exit "${KSFT_SKIP}"
@@ -273,6 +326,20 @@ check_vng() { fi } +check_socat() { + local support_string + + support_string="$(socat -V)" + + if [[ "${support_string}" != *"WITH_VSOCK 1"* ]]; then + die "err: socat is missing vsock support" + fi + + if [[ "${support_string}" != *"WITH_UNIX 1"* ]]; then + die "err: socat is missing unix support" + fi +} + handle_build() { if [[ ! "${BUILD}" -eq 1 ]]; then return
@@ -321,6 +388,14 @@ terminate_pidfiles() { done } +terminate_pids() { + local pid + + for pid in "$@"; do + kill -SIGTERM "${pid}" &>/dev/null || : + done +} + vm_start() { local pidfile=$1 local ns=$2
@@ -459,6 +534,28 @@ vm_dmesg_warn_count() { vm_ssh "${ns}" -- dmesg --level=warn 2>/dev/null | grep -c -i 'vsock' } +vm_dmesg_check() { + local pidfile=$1 + local ns=$2 + local oops_before=$3 + local warn_before=$4 + local oops_after warn_after + + oops_after=$(vm_dmesg_oops_count "${ns}") + if [[ "${oops_after}" -gt "${oops_before}" ]]; then + echo "FAIL: kernel oops detected on vm in ns ${ns}" | log_host + return 1 + fi + + warn_after=$(vm_dmesg_warn_count "${ns}") + 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=$1 local host=$2
@@ -502,6 +599,8 @@ host_vsock_test() { local host=$2 local cid=$3 local port=$4 + shift 4 + local extra_args=("$@") local rc local cmd="${VSOCK_TEST}"
@@ -516,13 +615,15 @@ host_vsock_test() { --mode=client \ --peer-cid="${cid}" \ --control-host="${host}" \ - --control-port="${port}" 2>&1 | log_host + --control-port="${port}" \ + "${extra_args[@]}" 2>&1 | log_host rc=$? else ${cmd} \ --mode=server \ --peer-cid="${cid}" \ - --control-port="${port}" 2>&1 | log_host & + --control-port="${port}" \ + "${extra_args[@]}" 2>&1 | log_host & rc=$? if [[ $rc -ne 0 ]]; then
@@ -593,6 +694,468 @@ test_ns_host_vsock_ns_mode_ok() { return "${KSFT_PASS}" } +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="global0" + ns1="global1" + port=1234 + local rc + + init_namespaces + + pidfile="$(create_pidfile)" + + if ! vm_start "${pidfile}" "${ns0}"; then + return "${KSFT_FAIL}" + fi + + vm_wait_for_ssh "${ns0}" + oops_before=$(vm_dmesg_oops_count "${ns0}") + warn_before=$(vm_dmesg_warn_count "${ns0}") + + unixfile=$(mktemp -u /tmp/XXXX.sock) + ip netns exec "${ns1}" \ + socat TCP-LISTEN:"${TEST_HOST_PORT}",fork \ + UNIX-CONNECT:"${unixfile}" & + pids+=($!) + host_wait_for_listener "${ns1}" "${TEST_HOST_PORT}" "tcp" + + ip netns exec "${ns0}" socat UNIX-LISTEN:"${unixfile}",fork \ + TCP-CONNECT:localhost:"${TEST_HOST_PORT}" & + pids+=($!) + host_wait_for_listener "${ns0}" "${unixfile}" "unix" + + vm_vsock_test "${ns0}" "server" 2 "${TEST_GUEST_PORT}" + vm_wait_for_listener "${ns0}" "${TEST_GUEST_PORT}" "tcp" + host_vsock_test "${ns1}" "127.0.0.1" "${VSOCK_CID}" "${TEST_HOST_PORT}" + rc=$? + + vm_dmesg_check "${pidfile}" "${ns0}" "${oops_before}" "${warn_before}" + dmesg_rc=$? + + 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="global0" + local ns1="local0" + local port=12345 + local dmesg_rc + local pidfile + local result + local pid + + init_namespaces + + outfile=$(mktemp) + + pidfile="$(create_pidfile)" + if ! vm_start "${pidfile}" "${ns1}"; then + log_host "failed to start vm (cid=${VSOCK_CID}, ns=${ns0})" + return "${KSFT_FAIL}" + fi + + vm_wait_for_ssh "${ns1}" + oops_before=$(vm_dmesg_oops_count "${ns1}") + warn_before=$(vm_dmesg_warn_count "${ns1}") + + vm_ssh "${ns1}" -- socat VSOCK-LISTEN:"${port}" STDOUT > "${outfile}" & + vm_wait_for_listener "${ns1}" "${port}" "vsock" + 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=$? + + terminate_pidfiles "${pidfile}" + result=$(cat "${outfile}") + rm -f "${outfile}" + + if [[ "${result}" == "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="global0" + local ns1="global1" + local port=12345 + 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=$(mktemp -u /tmp/XXXX.sock) + + ip netns exec "${ns0}" \ + socat TCP-LISTEN:"${port}" UNIX-CONNECT:"${unixfile}" & + pids+=($!) + host_wait_for_listener "${ns0}" "${port}" "tcp" + + ip netns exec "${ns1}" \ + socat UNIX-LISTEN:"${unixfile}" TCP-CONNECT:127.0.0.1:"${port}" & + pids+=($!) + host_wait_for_listener "${ns1}" "${unixfile}" "unix" + + log_host "Launching ${VSOCK_TEST} in ns ${ns1}" + host_vsock_test "${ns1}" "server" "${VSOCK_CID}" "${port}" + + pidfile="$(create_pidfile)" + if ! vm_start "${pidfile}" "${ns0}"; then + log_host "failed to start vm (cid=${cid}, ns=${ns0})" + terminate_pids "${pids[@]}" + rm -f "${unixfile}" + return "${KSFT_FAIL}" + fi + + vm_wait_for_ssh "${ns0}" + + oops_before=$(vm_dmesg_oops_count "${ns0}") + warn_before=$(vm_dmesg_warn_count "${ns0}") + + vm_vsock_test "${ns0}" "10.0.2.2" 2 "${port}" + rc=$? + + vm_dmesg_check "${pidfile}" "${ns0}" "${oops_before}" "${warn_before}" + dmesg_rc=$? + + 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="global0" + local ns1="local0" + local port=12345 + local oops_before warn_before + local dmesg_rc + local pidfile + local result + local pid + + init_namespaces + + log_host "Launching socat in ns ${ns1}" + outfile=$(mktemp) + + ip netns exec "${ns1}" socat VSOCK-LISTEN:"${port}" STDOUT &> "${outfile}" & + pid=$! + host_wait_for_listener "${ns1}" "${port}" "vsock" + + pidfile="$(create_pidfile)" + if ! vm_start "${pidfile}" "${ns0}"; then + log_host "failed to start vm (cid=${cid}, ns=${ns0})" + terminate_pids "${pid}" + rm -f "${outfile}" + return "${KSFT_FAIL}" + fi + + vm_wait_for_ssh "${ns0}" + + oops_before=$(vm_dmesg_oops_count "${ns0}") + warn_before=$(vm_dmesg_warn_count "${ns0}") + + vm_ssh "${ns0}" -- \ + bash -c "echo TEST | socat STDIN VSOCK-CONNECT:2:${port}" 2>&1 | log_guest + + vm_dmesg_check "${pidfile}" "${ns0}" "${oops_before}" "${warn_before}" + dmesg_rc=$? + + terminate_pidfiles "${pidfile}" + terminate_pids "${pid}" + + result=$(cat "${outfile}") + rm -f "${outfile}" + + if [[ "${result}" != 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="local0" + local ns1="local1" + local port=12345 + local oops_before warn_before + local dmesg_rc + local pidfile + local result + local pid + + init_namespaces + + outfile=$(mktemp) + + pidfile="$(create_pidfile)" + if ! vm_start "${pidfile}" "${ns1}"; then + log_host "failed to start vm (cid=${cid}, ns=${ns0})" + return "${KSFT_FAIL}" + fi + + vm_wait_for_ssh "${ns1}" + oops_before=$(vm_dmesg_oops_count "${ns1}") + warn_before=$(vm_dmesg_warn_count "${ns1}") + + vm_ssh "${ns1}" -- socat VSOCK-LISTEN:"${port}" STDOUT > "${outfile}" & + vm_wait_for_listener "${ns1}" "${port}" "vsock" + + 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=$? + + terminate_pidfiles "${pidfile}" + + result=$(cat "${outfile}") + rm -f "${outfile}" + + if [[ "${result}" != 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="local0" + local ns1="local1" + local port=12345 + local dmesg_rc + local pidfile + local result + local pid + + init_namespaces + + log_host "Launching socat in ns ${ns1}" + outfile=$(mktemp) + ip netns exec "${ns1}" socat VSOCK-LISTEN:"${port}" STDOUT &> "${outfile}" & + pid=$! + host_wait_for_listener "${ns1}" "${port}" "vsock" + + pidfile="$(create_pidfile)" + if ! vm_start "${pidfile}" "${ns0}"; then + log_host "failed to start vm (cid=${cid}, ns=${ns0})" + rm -f "${outfile}" + return "${KSFT_FAIL}" + fi + + vm_wait_for_ssh "${ns0}" + oops_before=$(vm_dmesg_oops_count "${ns0}") + warn_before=$(vm_dmesg_warn_count "${ns0}") + + vm_ssh "${ns0}" -- \ + bash -c "echo TEST | socat STDIN VSOCK-CONNECT:2:${port}" 2>&1 | log_guest + + vm_dmesg_check "${pidfile}" "${ns0}" "${oops_before}" "${warn_before}" + dmesg_rc=$? + + terminate_pidfiles "${pidfile}" + terminate_pids "${pid}" + + result=$(cat "${outfile}") + rm -f "${outfile}" + + if [[ "${result}" != TEST ]] && [[ "${dmesg_rc}" -eq 0 ]]; then + return "${KSFT_PASS}" + fi + + return "${KSFT_FAIL}" +} + +__test_loopback_two_netns() { + local ns0=$1 + local ns1=$2 + local port=12345 + local result + local pid + + modprobe vsock_loopback &> /dev/null || : + + log_host "Launching socat in ns ${ns1}" + outfile=$(mktemp) + + ip netns exec "${ns1}" socat VSOCK-LISTEN:"${port}" STDOUT > "${outfile}" 2>/dev/null & + pid=$! + host_wait_for_listener "${ns1}" "${port}" "vsock" + + 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=$(cat "${outfile}") + rm -f "${outfile}" + + if [[ "${result}" == 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="local0" + local port=1234 + local dmesg_rc + local pidfile + local rc + + init_namespaces + + pidfile="$(create_pidfile)" + + if ! vm_start "${pidfile}" "${ns}"; then + return "${KSFT_FAIL}" + fi + + vm_wait_for_ssh "${ns}" + oops_before=$(vm_dmesg_oops_count "${ns}") + warn_before=$(vm_dmesg_warn_count "${ns}") + + vm_vsock_test "${ns}" "server" 2 "${TEST_GUEST_PORT}" + + # Skip test 29 (transport release use-after-free): This test attempts + # binding both G2H and H2G CIDs. Because virtio-vsock (G2H) doesn't + # support local namespaces the test will fail when + # transport_g2h->stream_allow() returns false. This edge case only + # happens for vsock_test in client mode on the host in a local + # namespace. This is a false positive. + host_vsock_test "${ns}" "127.0.0.1" "${VSOCK_CID}" "${TEST_HOST_PORT}" --skip=29 + rc=$? + + vm_dmesg_check "${pidfile}" "${ns}" "${oops_before}" "${warn_before}" + dmesg_rc=$? + + 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="local0" + local port=1234 + local dmesg_rc + local pidfile + local rc + + init_namespaces + + pidfile="$(create_pidfile)" + + if ! vm_start "${pidfile}" "${ns}"; then + return "${KSFT_FAIL}" + fi + + vm_wait_for_ssh "${ns}" + oops_before=$(vm_dmesg_oops_count "${ns}") + warn_before=$(vm_dmesg_warn_count "${ns}") + + host_vsock_test "${ns}" "server" "${VSOCK_CID}" "${port}" + vm_vsock_test "${ns}" "10.0.2.2" 2 "${port}" + rc=$? + + vm_dmesg_check "${pidfile}" "${ns}" "${oops_before}" "${warn_before}" + dmesg_rc=$? + + 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=$1 local ns1=$2
@@ -882,6 +1445,7 @@ fi check_args "${ARGS[@]}" check_deps check_vng +check_socat handle_build echo "1..${#ARGS[@]}"
Signature · 1 line
2.47.3
PATCH v16 12/12 selftests/vsock: add tests for namespace deletion
In reply to Bobby EshlemanFrom: Bobby Eshleman <[email protected]>
Add tests that validate vsock sockets are resilient to deleting namespaces. The vsock sockets should still function normally.
The function check_ns_delete_doesnt_break_connection() is added to re-use the step-by-step logic of 1) setup connections, 2) delete ns,
3) check that the connections are still ok.
---
Changes in v13:
- remove tests that change the mode after socket creation (this is not supported behavior now and the immutability property is tested in other tests)
- remove "change_mode" behavior of check_ns_changes_dont_break_connection() and rename to check_ns_delete_doesnt_break_connection() because we only need to test namespace deletion (other tests confirm that the mode cannot change)
Changes in v11:
- remove pipefile (Stefano)
Changes in v9:
- more consistent shell style
- clarify -u usage comment for pipefile
--- tools/testing/selftests/vsock/vmtest.sh | 84 +++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+)
tools/testing/selftests/vsock/vmtest.sh +84 -0
@@ -68,6 +68,9 @@ readonly TEST_NAMES=( ns_same_local_loopback_ok ns_same_local_host_connect_to_local_vm_ok ns_same_local_vm_connect_to_local_host_ok + ns_delete_vm_ok + ns_delete_host_ok + ns_delete_both_ok ) readonly TEST_DESCS=( # vm_server_host_client
@@ -135,6 +138,15 @@ readonly TEST_DESCS=( # 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_delete_vm_ok + "Check that deleting the VM's namespace does not break the socket connection" + + # ns_delete_host_ok + "Check that deleting the host's namespace does not break the socket connection" + + # ns_delete_both_ok + "Check that deleting the VM and host's namespaces does not break the socket connection" ) readonly USE_SHARED_VM=(
@@ -1287,6 +1299,78 @@ test_vm_loopback() { return "${KSFT_PASS}" } +check_ns_delete_doesnt_break_connection() { + local pipefile pidfile outfile + local ns0="global0" + local ns1="global1" + local port=12345 + local pids=() + local rc=0 + + init_namespaces + + pidfile="$(create_pidfile)" + if ! vm_start "${pidfile}" "${ns0}"; then + return "${KSFT_FAIL}" + fi + vm_wait_for_ssh "${ns0}" + + outfile=$(mktemp) + vm_ssh "${ns0}" -- \ + socat VSOCK-LISTEN:"${port}",fork STDOUT > "${outfile}" 2>/dev/null & + pids+=($!) + vm_wait_for_listener "${ns0}" "${port}" "vsock" + + # 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=$(mktemp -u /tmp/vmtest_pipe_XXXX) + ip netns exec "${ns1}" \ + socat PIPE:"${pipefile}" VSOCK-CONNECT:"${VSOCK_CID}":"${port}" & + pids+=($!) + + timeout "${WAIT_PERIOD}" \ + bash -c 'while [[ ! -e '"${pipefile}"' ]]; do sleep 1; done; exit 0' + + if [[ "$1" == "vm" ]]; then + ip netns del "${ns0}" + elif [[ "$1" == "host" ]]; then + ip netns del "${ns1}" + elif [[ "$1" == "both" ]]; then + ip netns del "${ns0}" + ip netns del "${ns1}" + 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="${KSFT_PASS}" + else + rc="${KSFT_FAIL}" + fi + + terminate_pidfiles "${pidfile}" + terminate_pids "${pids[@]}" + rm -f "${outfile}" "${pipefile}" + + return "${rc}" +} + +test_ns_delete_vm_ok() { + check_ns_delete_doesnt_break_connection "vm" +} + +test_ns_delete_host_ok() { + check_ns_delete_doesnt_break_connection "host" +} + +test_ns_delete_both_ok() { + check_ns_delete_doesnt_break_connection "both" +} + shared_vm_test() { local tname
Signature · 1 line
2.47.3
Stefano Garzarella
maintainerQuoted 53 lines · Bobby Eshleman
This series adds namespace support to vhost-vsock and loopback. It does not add namespaces to any of the other guest transports (virtio-vsock, hyperv, or vmci).
The current revision supports two modes: local and global. Local mode is complete isolation of namespaces, while global mode is complete sharing between namespaces of CIDs (the original behavior).
The mode is set using the parent namespace's
/proc/sys/net/vsock/child_ns_modeand inherited when a new namespace is created. The mode of the current namespace can be queried by reading/proc/sys/net/vsock/ns_mode. The mode can not change after the namespace has been created.Modes are per-netns. This allows a system to configure namespaces independently (some may share CIDs, others are completely isolated). This also supports future possible mixed use cases, where there may be namespaces in global mode spinning up VMs while there are mixed mode namespaces that provide services to the VMs, but are not allowed to allocate from the global CID pool (this mode is not implemented in this series).
Additionally, added tests for the new namespace features:
tools/testing/selftests/vsock/vmtest.sh1..25
ok 1vm_server_host_client
ok 2vm_client_host_server
ok 3vm_loopback
ok 4ns_host_vsock_ns_mode_ok
ok 5ns_host_vsock_child_ns_mode_ok
ok 6ns_global_same_cid_fails
ok 7ns_local_same_cid_ok
ok 8ns_global_local_same_cid_ok
ok 9ns_local_global_same_cid_ok
ok 10ns_diff_global_host_connect_to_global_vm_okok 11ns_diff_global_host_connect_to_local_vm_failsok 12ns_diff_global_vm_connect_to_global_host_okok 13ns_diff_global_vm_connect_to_local_host_failsok 14ns_diff_local_host_connect_to_local_vm_failsok 15ns_diff_local_vm_connect_to_local_host_failsok 16ns_diff_global_to_local_loopback_local_failsok 17ns_diff_local_to_global_loopback_failsok 18ns_diff_local_to_local_loopback_failsok 19ns_diff_global_to_global_loopback_okok 20ns_same_local_loopback_ok
ok 21ns_same_local_host_connect_to_local_vm_okok 22ns_same_local_vm_connect_to_local_host_okok 23ns_delete_vm_ok
ok 24ns_delete_host_ok
ok 25ns_delete_both_ok
SUMMARY: PASS=25 SKIP=0 FAIL=0
Quoted 1 line
Thanks again for everyone's help and reviews!
Thank you for your hard work and patience!
I think we've come up with an excellent solution that's also not too invasive.
All the patches have my R-b, I've double-checked and tested this v16. Everything seems to be working fine (famous last words xD).
So this series is good to go IMO!
Next step should be to update the vsock(7) namespace.
Thanks,
Stefano
Quoted 54 lines · Stefano Garzarella
Quoted 53 lines · Bobby Eshleman
This series adds namespace support to vhost-vsock and loopback. It does not add namespaces to any of the other guest transports (virtio-vsock, hyperv, or vmci).
The current revision supports two modes: local and global. Local mode is complete isolation of namespaces, while global mode is complete sharing between namespaces of CIDs (the original behavior).
The mode is set using the parent namespace's
/proc/sys/net/vsock/child_ns_modeand inherited when a new namespace is created. The mode of the current namespace can be queried by reading/proc/sys/net/vsock/ns_mode. The mode can not change after the namespace has been created.Modes are per-netns. This allows a system to configure namespaces independently (some may share CIDs, others are completely isolated). This also supports future possible mixed use cases, where there may be namespaces in global mode spinning up VMs while there are mixed mode namespaces that provide services to the VMs, but are not allowed to allocate from the global CID pool (this mode is not implemented in this series).
Additionally, added tests for the new namespace features:
tools/testing/selftests/vsock/vmtest.sh1..25
ok 1vm_server_host_client
ok 2vm_client_host_server
ok 3vm_loopback
ok 4ns_host_vsock_ns_mode_ok
ok 5ns_host_vsock_child_ns_mode_ok
ok 6ns_global_same_cid_fails
ok 7ns_local_same_cid_ok
ok 8ns_global_local_same_cid_ok
ok 9ns_local_global_same_cid_ok
ok 10ns_diff_global_host_connect_to_global_vm_okok 11ns_diff_global_host_connect_to_local_vm_failsok 12ns_diff_global_vm_connect_to_global_host_okok 13ns_diff_global_vm_connect_to_local_host_failsok 14ns_diff_local_host_connect_to_local_vm_failsok 15ns_diff_local_vm_connect_to_local_host_failsok 16ns_diff_global_to_local_loopback_local_failsok 17ns_diff_local_to_global_loopback_failsok 18ns_diff_local_to_local_loopback_failsok 19ns_diff_global_to_global_loopback_okok 20ns_same_local_loopback_ok
ok 21ns_same_local_host_connect_to_local_vm_okok 22ns_same_local_vm_connect_to_local_host_okok 23ns_delete_vm_ok
ok 24ns_delete_host_ok
ok 25ns_delete_both_ok
SUMMARY: PASS=25 SKIP=0 FAIL=0
Quoted 6 lines
Quoted 1 line
Thanks again for everyone's help and reviews!
Thank you for your hard work and patience!
I think we've come up with an excellent solution that's also not too invasive.
Thanks, and I appreciate all of the work you and other maintainers put into this as well! I think we honed in on a great solution too.
Quoted 7 lines
All the patches have my R-b, I've double-checked and tested this v16. Everything seems to be working fine (famous last words xD).
So this series is good to go IMO!
Next step should be to update the vsock(7) namespace.
Sounds good, I'll follow up with that and CC you + other reviewers that participated here.
Thanks again,
Bobby
Quoted 53 lines · Bobby Eshleman
This series adds namespace support to vhost-vsock and loopback. It does not add namespaces to any of the other guest transports (virtio-vsock, hyperv, or vmci).
The current revision supports two modes: local and global. Local mode is complete isolation of namespaces, while global mode is complete sharing between namespaces of CIDs (the original behavior).
The mode is set using the parent namespace's
/proc/sys/net/vsock/child_ns_modeand inherited when a new namespace is created. The mode of the current namespace can be queried by reading/proc/sys/net/vsock/ns_mode. The mode can not change after the namespace has been created.Modes are per-netns. This allows a system to configure namespaces independently (some may share CIDs, others are completely isolated). This also supports future possible mixed use cases, where there may be namespaces in global mode spinning up VMs while there are mixed mode namespaces that provide services to the VMs, but are not allowed to allocate from the global CID pool (this mode is not implemented in this series).
Additionally, added tests for the new namespace features:
tools/testing/selftests/vsock/vmtest.sh1..25
ok 1vm_server_host_client
ok 2vm_client_host_server
ok 3vm_loopback
ok 4ns_host_vsock_ns_mode_ok
ok 5ns_host_vsock_child_ns_mode_ok
ok 6ns_global_same_cid_fails
ok 7ns_local_same_cid_ok
ok 8ns_global_local_same_cid_ok
ok 9ns_local_global_same_cid_ok
ok 10ns_diff_global_host_connect_to_global_vm_okok 11ns_diff_global_host_connect_to_local_vm_failsok 12ns_diff_global_vm_connect_to_global_host_okok 13ns_diff_global_vm_connect_to_local_host_failsok 14ns_diff_local_host_connect_to_local_vm_failsok 15ns_diff_local_vm_connect_to_local_host_failsok 16ns_diff_global_to_local_loopback_local_failsok 17ns_diff_local_to_global_loopback_failsok 18ns_diff_local_to_local_loopback_failsok 19ns_diff_global_to_global_loopback_okok 20ns_same_local_loopback_ok
ok 21ns_same_local_host_connect_to_local_vm_okok 22ns_same_local_vm_connect_to_local_host_okok 23ns_delete_vm_ok
ok 24ns_delete_host_ok
ok 25ns_delete_both_ok
SUMMARY: PASS=25 SKIP=0 FAIL=0
Quoted 4 lines
Thanks again for everyone's help and reviews!
Suggested-by Sargun Dhillon <[email protected]>Signed-off-by Bobby Eshleman <[email protected]>
Quoted 189 lines
Changes in v16:
- updated comments/docs/commit msg (vsock_find_* funcs, init net
mode, why change random port alloc)
- removed init ns mode cmdline
- fixed the missing ${ns} arg forvm_sshinvmtest.sh
- Link to v15: https://lore.kernel.org/r/[email protected]Changes in v15:
- see per-patch change notes in 'vsock: add netns to vsock core'
- Link to v14: https://lore.kernel.org/r/[email protected]Changes in v14:
- squashed 'vsock: add per-net vsock NS mode state' into 'vsock: add
netns to vsock core' (MST)
- remove RFC tag
- fixed base-commit (still had b4 configured to depend on oldvmtest.shseries)
- Link to v13: https://lore.kernel.org/all/[email protected]/Changes in v13:
- add support for immutable sysfsns_modeand inheritance from sysfschild_ns_mode
- remove passing around ofnet_mode, can be accessed now viavsock_net_mode(net)since it is immutable
- update tests for new uAPI
- add one patch to extend the kselftest timeout (it was starting to
fail with the new tests added)
- Link to v12: https://lore.kernel.org/r/[email protected]Changes in v12:
- add ns mode checking to_allow()callbacks to reject local mode for incompatible transports (Stefano)
- flip vhost/loopback to return true forstream_allow()andseqpacket_allow()in "vsock: add netns support to virtio transports" (Stefano)
- addVMADDR_CID_ANY+ local mode documentation inaf_vsock.c(Stefano)
- change "selftests/vsock: add tests for host <-> vm connectivity with namespaces" to skip test 29 invsock_testfor namespace local
vsock_testcalls in a host local-mode namespace. There is a false-positive edge case for that test encountered with the ->stream_allow() approach. More details in that patch.
- updated cover letter with new test output
- Link to v11: https://lore.kernel.org/r/[email protected]Changes in v11:
- vmtest: add a patch to use ss inwait_for_listenerfunctions and
support vsock, tcp, and unix. Change all patches to use the new functions.
- vmtest: add a patch to re-use vm dmesg / warn counting functions
- Link to v10: https://lore.kernel.org/r/[email protected]Changes in v10:
- Combine virtio common patches into one (Stefano)
- Resolvevsock_loopbackvirtio_transport_reset_no_sock()issue
withinfo->vsksetting. This eliminates the need forskb->cb,
so removeskb->cbpatches.
- many line width 80 fixes
- Link to v9: https://lore.kernel.org/all/[email protected]Changes in v9:
- reorder loopback patch after patch for virtio transport common code
- remove module ordering tests patch because loopback no longer depends
on pernet ops
- major simplifications invsock_loopback
- added a new patch for blocking local mode for guests, added test case
to check
- add net ref tracking tovsock_loopbackpatch
- Link to v8: https://lore.kernel.org/r/[email protected]Changes in v8:
- Break generic cleanup/refactoring patches into standalone series,
remove those from this series
- Link to dependency: https://lore.kernel.org/all/20251022-vsock-selftests-fixes-and-improvements-v1-0-edeb179d6463@meta.com/
- Link to v7: https://lore.kernel.org/r/[email protected]Changes in v7:
- fixhv_sockbuild
- break out vmtest patches into distinct, more well-scoped patches
- changeorig_net_modetonet_mode
- many fixes and style changes in per-patch change sets (see individual patches for specific changes)
- optimizevirtio_vsock_skb_cblayout
- update commit messages with more useful descriptions
-vsock_loopback: useorig_net_modeinstead of current net mode
- add tests for edge cases (ns deletion, mode changing, loopback module load ordering)
- Link to v6: https://lore.kernel.org/r/[email protected]Changes in v6:
- define behavior when mode changes to local while socket/VM is alive
-af_vsock: clarify description of CID behavior
-af_vsock: use stronger langauge around CID rules (dont use "may")
-af_vsock: improve naming of buf/buffer
-af_vsock: improve string length checking on proc writes
-vsock_loopback: add space in struct to clarify lock protection
-vsock_loopback: do proper cleanup/unregister onvsock_loopback_exit()
-vsock_loopback: usevirtio_vsock_skb_net()instead ofsock_net()
-vsock_loopback: set loopback to NULL afterkfree()
-vsock_loopback: usepernet_operationsand remove callback mechanism
-vsock_loopback: add macros for "global" and "local"
-vsock_loopback: fix length checking
-vmtest.sh: check for namespace support invmtest.sh
- Link to v5: https://lore.kernel.org/r/[email protected]Changes in v5:
-/proc/net/vsock_ns_mode->/proc/sys/net/vsock/ns_mode
-vsock_global_net->vsock_global_dummy_net
- fix netns lookup invhost_vsockto respect pid namespaces
- add callbacks forvsock_loopbackto avoid circular dependency
-vmtest.shloadsvsock_loopbackmodule
- removevsock_net_mode_can_set()
- changevsock_net_write_mode()to return true/false based on success
- makevsock_net_modeenum instead of u8
- Link to v4: https://lore.kernel.org/r/[email protected]Changes in v4:
- removed RFC tag
- implemented loopback support
- renamed new tests to better reflect behavior
- completed suite of tests with permutations of ns modes andvsock_testas guest/host
- simplified socat bridging with unix socket instead of tcp + veth
- only usevsock_testfor success case, socat for failure case (context in commit message)
- lots of cleanupChanges in v3:
- add notion of "modes"
- add procfs/proc/net/vsock_ns_mode
- local and global modes only
- no/dev/vhost-vsock-netns
-vmtest.shalready merged, so new patch just adds new tests for NS
- Link to v2:
https://lore.kernel.org/kvm/[email protected]Changes in v2:
- only support vhost-vsock namespaces
- all g2h namespaces retain old behavior, only common API changes impacted by vhost-vsock changes
- add/dev/vhost-vsock-netnsfor "opt-in"
- leave/dev/vhost-vsockto old behavior
- removed netns module param
- Link to v1:
https://lore.kernel.org/r/[email protected]Changes in v1:
- added 'netns' module param to vsock.ko to enable the network namespace support (disabled by default)
- added 'vsock_net_eq()' to check the "net" assigned to a socket only when 'netns' support is enabled
- Link to RFC: https://patchwork.ozlabs.org/cover/1202235/---
Bobby Eshleman (12):
vsock: add netns to vsock core
virtio: set skb owner ofvirtio_transport_reset_no_sock()reply
vsock: add netns support to virtio transports selftests/vsock: increase timeout to 1200
selftests/vsock: add namespace helpers tovmtest.shselftests/vsock: prepare vm management helpers for namespaces selftests/vsock: add vm_dmesg_{warn,oops}_count() helpers selftests/vsock: use ss to wait for listeners instead of/proc/netselftests/vsock: add tests for proc sys vsockns_modeselftests/vsock: add namespace tests for CID collisions selftests/vsock: add tests for host <-> vm connectivity with namespaces selftests/vsock: add tests for namespace deletionMAINTAINERS | 1 + drivers/vhost/vsock.c | 44 +- include/linux/virtio_vsock.h | 9 +- include/net/af_vsock.h | 61 +- include/net/net_namespace.h | 4 + include/net/netns/vsock.h | 21 + net/vmw_vsock/af_vsock.c | 335 +++++++++- net/vmw_vsock/hyperv_transport.c | 7 +- net/vmw_vsock/virtio_transport.c | 22 +- net/vmw_vsock/virtio_transport_common.c | 62 +- net/vmw_vsock/vmci_transport.c | 26 +- net/vmw_vsock/vsock_loopback.c | 22 +- tools/testing/selftests/vsock/settings | 2 +- tools/testing/selftests/vsock/vmtest.sh | 1055 +++++++++++++++++++++++++++++-- 14 files changed, 1531 insertions(+), 140 deletions(-)---
base-commit:d8f87aa5fa0a4276491fa8ef436cd22605a3f9ba
change-id: 20250325-vsock-vmtest-b3a21d2102c2Best regards,
Signature · 1 line
Bobby Eshleman <[email protected]>
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:
Quoted 9 lines · On Wed, 21 Jan 2026 14:11:40 -0800 you wrote:
This series adds namespace support to vhost-vsock and loopback. It does not add namespaces to any of the other guest transports (virtio-vsock, hyperv, or vmci).
The current revision supports two modes: local and global. Local mode is complete isolation of namespaces, while global mode is complete sharing between namespaces of CIDs (the original behavior).
[...]
Here is the summary with links:
- [net-next,v16,01/12] vsock: add netns to vsock core
https://git.kernel.org/netdev/net-next/c/eafb64f40ca4 - [net-next,v16,02/12] virtio: set skb owner of virtio_transport_reset_no_sock() reply https://git.kernel.org/netdev/net-next/c/a6ae12a599e0 - [net-next,v16,03/12] vsock: add netns support to virtio transports https://git.kernel.org/netdev/net-next/c/a69686327e42 - [net-next,v16,04/12] selftests/vsock: increase timeout to 1200 https://git.kernel.org/netdev/net-next/c/873e7de9f9a3 - [net-next,v16,05/12] selftests/vsock: add namespace helpers to vmtest.sh https://git.kernel.org/netdev/net-next/c/423ec6383edb - [net-next,v16,06/12] selftests/vsock: prepare vm management helpers for namespaces https://git.kernel.org/netdev/net-next/c/fd1b41725d58 - [net-next,v16,07/12] selftests/vsock: add vm_dmesg_{warn,oops}_count() helpers https://git.kernel.org/netdev/net-next/c/4e870ac81df7 - [net-next,v16,08/12] selftests/vsock: use ss to wait for listeners instead of /proc/net https://git.kernel.org/netdev/net-next/c/7418f3bb3aa2 - [net-next,v16,09/12] selftests/vsock: add tests for proc sys vsock ns_mode https://git.kernel.org/netdev/net-next/c/06cf7895abf9 - [net-next,v16,10/12] selftests/vsock: add namespace tests for CID collisions https://git.kernel.org/netdev/net-next/c/605caec5adc2 - [net-next,v16,11/12] selftests/vsock: add tests for host <-> vm connectivity with namespaces https://git.kernel.org/netdev/net-next/c/0424ee7c3a17 - [net-next,v16,12/12] selftests/vsock: add tests for namespace deletion https://git.kernel.org/netdev/net-next/c/b3b7b33264c6
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html