From: Bobby Eshleman <hidden> Date: 2025-08-05 21:49:13
This series adds namespace support to vhost-vsock. It does not add
namespaces to any of the guest transports (virtio-vsock, hyperv, or
vmci).
The current revision only supports two modes: local or global. Local
mode is complete isolation of namespaces, while global mode is complete
sharing between namespaces of CIDs (the original behavior).
Future may include supporting a mixed mode, which I expect to be more
complicated because socket lookups will have to include new logic and
API changes to behave differently based on if the lookup is part of a
mixed mode CID allocation, a global CID allocation, a mixed-to-global
connection (allowed), or a global-to-mixed connection (not allowed).
Modes are per-netns and write-once. This allows a system to configure
namespaces independently (some may share CIDs, others are completely
isolated). This also supports future 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.
Thanks again for everyone's help and reviews!
Signed-off-by: Bobby Eshleman <redacted>
To: Stefano Garzarella <sgarzare@redhat.com>
To: Shuah Khan <shuah@kernel.org>
To: David S. Miller <davem@davemloft.net>
To: Eric Dumazet <edumazet@google.com>
To: Jakub Kicinski <kuba@kernel.org>
To: Paolo Abeni <pabeni@redhat.com>
To: Simon Horman <horms@kernel.org>
To: Stefan Hajnoczi <stefanha@redhat.com>
To: Michael S. Tsirkin <mst@redhat.com>
To: Jason Wang <redacted>
To: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
To: Eugenio Pérez <eperezma@redhat.com>
To: K. Y. Srinivasan <kys@microsoft.com>
To: Haiyang Zhang <haiyangz@microsoft.com>
To: Wei Liu <wei.liu@kernel.org>
To: Dexuan Cui <decui@microsoft.com>
To: Bryan Tan <bryan-bt.tan@broadcom.com>
To: Vishnu Dasa <vishnu.dasa@broadcom.com>
To: Broadcom internal kernel review list <bcm-kernel-feedback-list@broadcom.com>
Cc: virtualization@lists.linux.dev
Cc: netdev@vger.kernel.org
Cc: linux-kselftest@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: kvm@vger.kernel.org
Cc: linux-hyperv@vger.kernel.org
Cc: berrange@redhat.com
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/20250312-vsock-netns-v2-0-84bffa1aa97a@gmail.com
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/20200116172428.311437-1-sgarzare@redhat.com
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: a per-net vsock NS mode state
vsock: add net to vsock skb cb
vsock: add netns to af_vsock core
vsock/virtio: add netns to virtio transport common
vhost/vsock: add netns support
vsock/virtio: use the global netns
hv_sock: add netns hooks
vsock/vmci: add netns hooks
vsock/loopback: add netns support
selftests/vsock: improve logging in vmtest.sh
selftests/vsock: invoke vsock_test through helpers
selftests/vsock: add namespace tests
MAINTAINERS | 1 +
drivers/vhost/vsock.c | 48 +-
include/linux/virtio_vsock.h | 12 +
include/net/af_vsock.h | 59 +-
include/net/net_namespace.h | 4 +
include/net/netns/vsock.h | 21 +
net/vmw_vsock/af_vsock.c | 204 +++++-
net/vmw_vsock/hyperv_transport.c | 2 +-
net/vmw_vsock/virtio_transport.c | 5 +-
net/vmw_vsock/virtio_transport_common.c | 14 +-
net/vmw_vsock/vmci_transport.c | 4 +-
net/vmw_vsock/vsock_loopback.c | 59 +-
tools/testing/selftests/vsock/vmtest.sh | 1088 ++++++++++++++++++++++++++-----
13 files changed, 1330 insertions(+), 191 deletions(-)
---
base-commit: dd500e4aecf25e48e874ca7628697969df679493
change-id: 20250325-vsock-vmtest-b3a21d2102c2
Best regards,
--
Bobby Eshleman [off-list ref]
From: Bobby Eshleman <hidden> Date: 2025-08-05 21:49:14
From: Bobby Eshleman <redacted>
Add the per-net vsock NS mode state. This only adds the structure for
holding the mode necessary and some of the definitions, but does not
integrate the functionality yet.
Signed-off-by: Bobby Eshleman <redacted>
---
MAINTAINERS | 1 +
include/net/af_vsock.h | 42 ++++++++++++++++++++++++++++++++++++++++++
include/net/net_namespace.h | 4 ++++
include/net/netns/vsock.h | 18 ++++++++++++++++++
4 files changed, 65 insertions(+)
@@ -196,6 +197,9 @@ struct net {/* Move to a better place when the config guard is removed. */structmutexrtnl_mutex;#endif+#if IS_ENABLED(CONFIG_VSOCKETS)+structnetns_vsockvsock;+#endif}__randomize_layout;#include<linux/seq_file_net.h>
From: Bobby Eshleman <hidden> Date: 2025-08-05 21:49:16
From: Bobby Eshleman <redacted>
Add a net pointer to the vsock skb and helpers for getting/setting it.
This is in preparation for adding vsock NS support.
Signed-off-by: Bobby Eshleman <redacted>
---
include/linux/virtio_vsock.h | 11 +++++++++++
1 file changed, 11 insertions(+)
From: Bobby Eshleman <hidden> Date: 2025-08-05 21:49:18
From: Bobby Eshleman <redacted>
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.
Signed-off-by: Bobby Eshleman <redacted>
---
include/net/af_vsock.h | 13 +++-
net/vmw_vsock/af_vsock.c | 198 +++++++++++++++++++++++++++++++++++++++++++----
2 files changed, 194 insertions(+), 17 deletions(-)
@@ -149,6 +169,9 @@ static const struct vsock_transport *transport_dgram;staticconststructvsock_transport*transport_local;staticDEFINE_MUTEX(vsock_register_mutex);+structnet__vsock_global_net;+EXPORT_SYMBOL_GPL(__vsock_global_net);+/**** UTILS ****//* Each bound VSocket is stored in the bind hash table and each connected
From: Bobby Eshleman <hidden> Date: 2025-08-05 21:49:19
From: Bobby Eshleman <redacted>
Add support to the virtio-vsock common code for passing around net
namespace pointers (tx and rx). The series still requires vhost/virtio
transport support to be added by future patches.
Signed-off-by: Bobby Eshleman <redacted>
---
include/linux/virtio_vsock.h | 1 +
net/vmw_vsock/virtio_transport_common.c | 14 ++++++++++++--
2 files changed, 13 insertions(+), 2 deletions(-)
@@ -524,6 +526,7 @@ static int virtio_transport_send_credit_update(struct vsock_sock *vsk)structvirtio_vsock_pkt_infoinfo={.op=VIRTIO_VSOCK_OP_CREDIT_UPDATE,.vsk=vsk,+.net=sock_net(sk_vsock(vsk)),};returnvirtio_transport_send_pkt_info(vsk,&info);
@@ -1064,6 +1067,7 @@ int virtio_transport_connect(struct vsock_sock *vsk)structvirtio_vsock_pkt_infoinfo={.op=VIRTIO_VSOCK_OP_REQUEST,.vsk=vsk,+.net=sock_net(sk_vsock(vsk)),};returnvirtio_transport_send_pkt_info(vsk,&info);
@@ -1079,6 +1083,7 @@ int virtio_transport_shutdown(struct vsock_sock *vsk, int mode)(mode&SEND_SHUTDOWN?VIRTIO_VSOCK_SHUTDOWN_SEND:0),.vsk=vsk,+.net=sock_net(sk_vsock(vsk)),};returnvirtio_transport_send_pkt_info(vsk,&info);
@@ -1142,6 +1148,7 @@ static int virtio_transport_reset(struct vsock_sock *vsk,.op=VIRTIO_VSOCK_OP_RST,.reply=!!skb,.vsk=vsk,+.net=sock_net(sk_vsock(vsk)),};/* Send RST only if the original pkt is not a RST pkt */
@@ -1162,6 +1169,7 @@ 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,+.net=virtio_vsock_skb_net(skb),};structsk_buff*reply;
@@ -1603,9 +1613,9 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,/* The socket must be in connected or bound table*otherwisesendresetback*/-sk=vsock_find_connected_socket(&src,&dst);+sk=vsock_find_connected_socket(&src,&dst,net);if(!sk){-sk=vsock_find_bound_socket(&dst);+sk=vsock_find_bound_socket(&dst,net);if(!sk){(void)virtio_transport_reset_no_sock(t,skb);gotofree_pkt;
From: Bobby Eshleman <hidden> Date: 2025-08-05 21:49:21
From: Bobby Eshleman <redacted>
Add the ability to isolate vsock flows using namespaces.
The namespace for a VM is inherited from the PID that opened the
vhost-vsock device.
Signed-off-by: Bobby Eshleman <redacted>
---
drivers/vhost/vsock.c | 48 ++++++++++++++++++++++++++++++++++++++----------
1 file changed, 38 insertions(+), 10 deletions(-)
@@ -46,6 +46,8 @@ static DEFINE_READ_MOSTLY_HASHTABLE(vhost_vsock_hash, 8);structvhost_vsock{structvhost_devdev;structvhost_virtqueuevqs[2];+structnet*net;+netns_trackerns_tracker;/* Link to global vhost_vsock_hash, writes use vhost_vsock_mutex */structhlist_nodehash;
@@ -67,7 +85,7 @@ static u32 vhost_transport_get_local_cid(void)/* Callers that dereference the return value must hold vhost_vsock_mutex or the*RCUreadlock.*/-staticstructvhost_vsock*vhost_vsock_get(u32guest_cid)+staticstructvhost_vsock*vhost_vsock_get(u32guest_cid,structnet*net){structvhost_vsock*vsock;
@@ -272,13 +289,14 @@ static intvhost_transport_send_pkt(structsk_buff*skb){structvirtio_vsock_hdr*hdr=virtio_vsock_hdr(skb);+structnet*net=virtio_vsock_skb_net(skb);structvhost_vsock*vsock;intlen=skb->len;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);
@@ -305,7 +323,7 @@ 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)gotoout;
@@ -525,6 +544,7 @@ static void vhost_vsock_handle_tx_kick(struct vhost_work *work)continue;}+virtio_vsock_skb_set_net(skb,vsock->net);total_len+=sizeof(*hdr)+skb->len;/* Deliver to monitoring devices all received packets */
@@ -651,10 +671,16 @@ static void vhost_vsock_free(struct vhost_vsock *vsock)staticintvhost_vsock_dev_open(structinode*inode,structfile*file){+structvhost_virtqueue**vqs;structvhost_vsock*vsock;+structnet*net;intret;+net=get_net_ns_by_pid(current->pid);+if(IS_ERR(net))+returnPTR_ERR(net);+/* This struct is large and allocation could fail, fall back to vmalloc*ifthereisnootherway.*/
@@ -668,6 +694,7 @@ static int vhost_vsock_dev_open(struct inode *inode, struct file *file)gotoout;}+vhost_vsock_net_set(vsock,net);vsock->guest_cid=0;/* no CID assigned yet */vsock->seqpacket_allow=false;
@@ -707,7 +734,7 @@ static void vhost_vsock_reset_orphans(struct sock *sk)*//* 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)))return;/* If the close timeout is pending, let it expire. This avoids races
@@ -778,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;
From: Bobby Eshleman <hidden> Date: 2025-08-05 21:49:22
From: Bobby Eshleman <redacted>
This changes virtio-vsock to always use the global netns dummy so that
all guest vsock continues to operate in global mode. The guest vsock
behavior is unchanged.
Signed-off-by: Bobby Eshleman <redacted>
---
net/vmw_vsock/virtio_transport.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
From: Bobby Eshleman <hidden> Date: 2025-08-05 21:49:24
From: Bobby Eshleman <redacted>
Make NS changes not break hyperv. Guest vsocks still remain in the
global namespace always, so the behavior is unchanged.
Signed-off-by: Bobby Eshleman <redacted>
---
net/vmw_vsock/hyperv_transport.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
From: Bobby Eshleman <hidden> Date: 2025-08-05 21:49:26
From: Bobby Eshleman <redacted>
Add hooks for new internal NS calls to avoid breaking vmci. Guest vsocks
remain in global mode namespaces, so behavior is unchanged.
Signed-off-by: Bobby Eshleman <redacted>
---
net/vmw_vsock/vmci_transport.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
@@ -703,9 +703,9 @@ static int vmci_transport_recv_stream_cb(void *data, struct vmci_datagram *dg)vsock_addr_init(&src,pkt->dg.src.context,pkt->src_port);vsock_addr_init(&dst,pkt->dg.dst.context,pkt->dst_port);-sk=vsock_find_connected_socket(&src,&dst);+sk=vsock_find_connected_socket(&src,&dst,vsock_global_net());if(!sk){-sk=vsock_find_bound_socket(&dst);+sk=vsock_find_bound_socket(&dst,vsock_global_net());if(!sk){/* We could not find a socket for this specified*address.IfthispacketisaRST,wejustdropit.
From: Bobby Eshleman <hidden> Date: 2025-08-05 21:49:27
From: Bobby Eshleman <redacted>
Add NS support to vsock loopback. Sockets in a global mode netns
communicate with each other, regardless of namespace. Sockets in a local
mode netns may only communicate with other sockets within the same
namespace.
Signed-off-by: Bobby Eshleman <redacted>
---
include/net/af_vsock.h | 4 +++
include/net/netns/vsock.h | 3 +++
net/vmw_vsock/af_vsock.c | 8 +++++-
net/vmw_vsock/vsock_loopback.c | 59 +++++++++++++++++++++++++++++++++++-------
4 files changed, 63 insertions(+), 11 deletions(-)
@@ -2778,9 +2778,12 @@ static __net_init int vsock_sysctl_init_net(struct net *net){vsock_net_init(net);-if(vsock_sysctl_register(net))+if(vsock_loopback_init_net(net))return-ENOMEM;+if(vsock_sysctl_register(net))+gotoerr_loopback;+#ifdef CONFIG_PROC_FSif(!proc_create_net_single_write("vsock_ns_mode",0644,net->proc_net,vsock_proc_ns_mode_show,
@@ -2793,12 +2796,15 @@ static __net_init int vsock_sysctl_init_net(struct net *net)err_sysctl:vsock_sysctl_unregister(net);+err_loopback:+vsock_loopback_exit_net(net);return-ENOMEM;}static__net_exitvoidvsock_sysctl_exit_net(structnet*net){vsock_sysctl_unregister(net);+vsock_loopback_exit_net(net);}staticstructpernet_operationsvsock_sysctl_ops__net_initdata={
From: Bobby Eshleman <hidden> Date: 2025-08-05 21:49:29
From: Bobby Eshleman <redacted>
Improve logging by adding configurable log levels. Additionally, improve
usability of logging functions. Remove the test name prefix from logging
functions so that logging calls can be made deeper into the call stack
without passing down the test name or setting some global. Teach log
function to accept a LOG_PREFIX variable to avoid unnecessary argument
shifting.
Signed-off-by: Bobby Eshleman <redacted>
---
tools/testing/selftests/vsock/vmtest.sh | 75 ++++++++++++++++-----------------
1 file changed, 37 insertions(+), 38 deletions(-)
@@ -51,7 +51,12 @@ readonly TEST_DESCS=("Run vsock_test using the loopback transport in the VM.")-VERBOSE=0+readonlyLOG_LEVEL_DEBUG=0+readonlyLOG_LEVEL_INFO=1+readonlyLOG_LEVEL_WARN=2+readonlyLOG_LEVEL_ERROR=3++VERBOSE="${LOG_LEVEL_WARN}" usage(){localname
From: Bobby Eshleman <hidden> Date: 2025-08-05 21:49:30
From: Bobby Eshleman <redacted>
Add helper calls vm_vsock_test() and host_vsock_test() to invoke the
vsock_test binary. This encapsulates several items of repeat logic, such
as waiting for the server to reach listening state and
enabling/disabling the bash option pipefail to avoid pipe-style logging
from hiding failures.
Signed-off-by: Bobby Eshleman <redacted>
---
tools/testing/selftests/vsock/vmtest.sh | 120 ++++++++++++++++++++++++++++----
1 file changed, 108 insertions(+), 12 deletions(-)
@@ -256,6 +257,13 @@ wait_for_listener()# for tcp protocol additionally check the socket state["${protocol}"="tcp"]&&pattern="${pattern}0A"++# 'grep -q' exits on match, sending SIGPIPE to 'awk', which exits with+# an error, causing the if-condition to fail when pipefail is set.+# Instead, temporarily disable pipefail and restore it later.+old_pipefail=$(set-o|awk'/^pipefail[[:space:]]+(on|off)$/{print $2}')+set+opipefail+foriin$(seq"${max_intervals}");doifawk'{print $2" "$4}'/proc/net/"${protocol}"*|\grep-q"${pattern}";then
@@ -314,28 +326,112 @@ log_guest() {LOG_PREFIX=guestlog$@}+vm_vsock_test(){+localns=$1+localmode=$2+localrc++set-opipefail+if[["${mode}"==client]];then+localhost=$3+localcid=$4+localport=$5++# log output and use pipefail to respect vsock_test errors+vm_ssh"${ns}"--"${VSOCK_TEST}"\+--mode=client\+--control-host="${host}"\+--peer-cid="${cid}"\+--control-port="${port}"\+2>&1|log_guest+rc=$?+else+localcid=$3+localport=$4++# log output and use pipefail to respect vsock_test errors+vm_ssh"${ns}"--"${VSOCK_TEST}"\+--mode=server\+--peer-cid="${cid}"\+--control-port="${port}"\+2>&1|log_guest&+rc=$?++if[[$rc-ne0]];then+set+opipefail+return$rc+fi++vm_wait_for_listener"${ns}""${port}"+rc=$?+fi+set+opipefail++return$rc}+host_vsock_test(){+localns=$1+localmode=$2+localcmd++if[["${ns}"==none]];then+cmd="${VSOCK_TEST}"+else+cmd="ip netns exec ${ns}${VSOCK_TEST}"+fi++# log output and use pipefail to respect vsock_test errors+set-opipefail+if[["${mode}"==client]];then+localhost=$3+localcid=$4+localport=$5++${cmd}\+--mode="${mode}"\+--peer-cid="${cid}"\+--control-host="${host}"\+--control-port="${port}"2>&1|log_host+rc=$?+else+localcid=$3+localport=$4++${cmd}\+--mode="${mode}"\+--peer-cid="${cid}"\+--control-port="${port}"2>&1|log_host&+rc=$?++if[[$rc-ne0]];then+return$rc+fi++host_wait_for_listener"${ns}""${port}""${WAIT_PERIOD}""${WAIT_PERIOD_MAX}"+rc=$?+fi+set+opipefail+return$rc} test_vm_server_host_client(){+vm_vsock_test"none""server"2"${TEST_GUEST_PORT}"+host_vsock_test"none""client""127.0.0.1""${VSOCK_CID}""${TEST_HOST_PORT}"+}-vm_ssh--"${VSOCK_TEST}"\---mode=server\---control-port="${TEST_GUEST_PORT}"\---peer-cid=2\-2>&1|log_guest&+test_vm_client_host_server(){+host_vsock_test"none""server""${VSOCK_CID}""${TEST_HOST_PORT_LISTENER}"+vm_vsock_test"none""client""10.0.2.2"2"${TEST_HOST_PORT_LISTENER}"+}-vm_wait_for_listener"${TEST_GUEST_PORT}"+test_vm_loopback(){+vm_vsock_test"none""server"1"${TEST_HOST_PORT_LISTENER}"+vm_vsock_test"none""client""127.0.0.1"1"${TEST_HOST_PORT_LISTENER}"+}-${VSOCK_TEST}\---mode=client\---control-host=127.0.0.1\---peer-cid="${VSOCK_CID}"\---control-port="${TEST_HOST_PORT}"2>&1|log_host-return$?} test_vm_client_host_server(){
From: Bobby Eshleman <hidden> Date: 2025-08-05 21:49:32
From: Bobby Eshleman <redacted>
Add tests for namespace support in vsock. Use socat for basic connection
failure tests and vsock_test for full functionality tests when
communication is expected to succeed. vsock_test is not used for failure
cases because in theory vsock_test could allow connection and some
traffic flow but fail on some other case (e.g., fail on MSG_ZEROCOPY).
Tests cover all cases of clients and servers being in all variants of
local ns, global ns, host process, and VM process.
Legacy tests are retained and executed in the init ns.
Signed-off-by: Bobby Eshleman <redacted>
---
tools/testing/selftests/vsock/vmtest.sh | 909 ++++++++++++++++++++++++++++----
1 file changed, 804 insertions(+), 105 deletions(-)
@@ -7,6 +7,7 @@# * virtme-ng# * busybox-static (used by virtme-ng)# * qemu (used by virtme-ng)+# * socatreadonlySCRIPT_DIR="$(cd-P--"$(dirname--"${BASH_SOURCE[0]}")"&&pwd-P)"readonlyKERNEL_CHECKOUT=$(realpath"${SCRIPT_DIR}"/../../../../)
@@ -23,7 +24,7 @@ readonly VSOCK_CID=1234readonlyWAIT_PERIOD=3readonlyWAIT_PERIOD_MAX=60readonlyWAIT_TOTAL=$((WAIT_PERIOD*WAIT_PERIOD_MAX))-readonlyQEMU_PIDFILE=$(mktemp/tmp/qemu_vsock_vmtest_XXXX.pid)+readonlyWAIT_QEMU=5# virtme-ng offers a netdev for ssh when using "--ssh", but we also need a# control port forwarded for vsock_test. Because virtme-ng doesn't support
@@ -33,23 +34,125 @@ readonly QEMU_PIDFILE=$(mktemp /tmp/qemu_vsock_vmtest_XXXX.pid)# add the kernel cmdline options that virtme-init uses to setup the interface.readonlyQEMU_TEST_PORT_FWD="hostfwd=tcp::${TEST_HOST_PORT}-:${TEST_GUEST_PORT}"readonlyQEMU_SSH_PORT_FWD="hostfwd=tcp::${SSH_HOST_PORT}-:${SSH_GUEST_PORT}"-readonlyQEMU_OPTS="\--netdevuser,id=n0,${QEMU_TEST_PORT_FWD},${QEMU_SSH_PORT_FWD}\--devicevirtio-net-pci,netdev=n0\--devicevhost-vsock-pci,guest-cid=${VSOCK_CID}\---pidfile${QEMU_PIDFILE}\-"readonlyKERNEL_CMDLINE="\virtme.dhcpnet.ifnames=0biosdevname=0\virtme.sshvirtme_ssh_channel=tcpvirtme_ssh_user=$USER\"readonlyLOG=$(mktemp/tmp/vsock_vmtest_XXXX.log)-readonlyTEST_NAMES=(vm_server_host_clientvm_client_host_servervm_loopback)+readonlyTEST_NAMES=(+vm_server_host_client+vm_client_host_server+vm_loopback+host_vsock_ns_mode_ok+host_vsock_ns_mode_write_once_ok+global_same_cid_fails+local_same_cid_ok+global_local_same_cid_ok+local_global_same_cid_ok+diff_ns_global_host_connect_to_global_vm_ok+diff_ns_global_host_connect_to_local_vm_fails+diff_ns_global_vm_connect_to_global_host_ok+diff_ns_global_vm_connect_to_local_host_fails+diff_ns_local_host_connect_to_local_vm_fails+diff_ns_local_vm_connect_to_local_host_fails+diff_ns_global_to_local_loopback_local_fails+diff_ns_local_to_global_loopback_fails+diff_ns_local_to_local_loopback_fails+diff_ns_global_to_global_loopback_ok+same_ns_local_loopback_ok+same_ns_local_host_connect_to_local_vm_ok+same_ns_local_vm_connect_to_local_host_ok+)+readonlyTEST_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."++# host_vsock_ns_mode_ok+"Check /proc/net/vsock_ns_mode strings on the host."++# host_vsock_ns_mode_write_once_ok+"Check /proc/net/vsock_ns_mode is write-once on the host."++# global_same_cid_fails+"Check QEMU fails to start two VMs with same CID in two different global namespaces."++# local_same_cid_ok+"Check QEMU successfully starts two VMs with same CID in two different local namespaces."++# 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."++# 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."++# diff_ns_global_host_connect_to_global_vm_ok+"Run vsock_test client in global ns with server in VM in another global ns."++# diff_ns_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."++# diff_ns_global_vm_connect_to_global_host_ok+"Run vsock_test client in VM in a global ns with server in another global ns."++# diff_ns_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."++# diff_ns_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."++# diff_ns_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."++# diff_ns_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."++# diff_ns_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."++# diff_ns_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."++# diff_ns_global_to_global_loopback_ok+"Run socat to test a loopback vsock in a global ns successfuly connects to a vsock in another global ns."++# same_ns_local_loopback_ok+"Run socat to test a loopback vsock in a local ns successfuly connects to a vsock in the same ns."++# same_ns_local_host_connect_to_local_vm_ok+"Run vsock_test client in a local ns with server in VM in same ns."++# same_ns_local_vm_connect_to_local_host_ok+"Run vsock_test client in VM in a local ns with server in same ns."+)++readonlyUSE_SHARED_VM=(vm_server_host_clientvm_client_host_servervm_loopback)+readonlyUSE_INIT_NETNS=(+global_same_cid_fails+local_same_cid_ok+global_local_same_cid_ok+local_global_same_cid_ok+diff_ns_global_host_connect_to_global_vm_ok+diff_ns_global_host_connect_to_local_vm_fails+diff_ns_global_vm_connect_to_global_host_ok+diff_ns_global_vm_connect_to_local_host_fails+diff_ns_local_host_connect_to_local_vm_fails+diff_ns_local_vm_connect_to_local_host_fails+diff_ns_global_to_local_loopback_local_fails+diff_ns_local_to_global_loopback_fails+diff_ns_local_to_local_loopback_fails+diff_ns_global_to_global_loopback_ok+same_ns_local_loopback_ok+same_ns_local_host_connect_to_local_vm_ok+same_ns_local_vm_connect_to_local_host_ok)+readonlyMODES=("local""global")readonlyLOG_LEVEL_DEBUG=0readonlyLOG_LEVEL_INFO=1
@@ -58,6 +161,12 @@ readonly LOG_LEVEL_ERROR=3VERBOSE="${LOG_LEVEL_WARN}"+# Test pass/fail counters+cnt_pass=0+cnt_fail=0+cnt_skip=0+cnt_total=0+ usage(){localnamelocaldesc
@@ -89,21 +198,87 @@ die() {exit"${KSFT_FAIL}"}+add_namespaces(){+# add namespaces local0, local1, global0, and global1+formodein"${MODES[@]}";do+ipnetnsadd"${mode}0"2>/dev/null+ipnetnsadd"${mode}1"2>/dev/null+done+}++init_namespaces(){+formodein"${MODES[@]}";do+ns_set_mode"${mode}0""${mode}"+ns_set_mode"${mode}1""${mode}"++log_host"set ns ${mode}0 to mode ${mode}"+log_host"set ns ${mode}1 to mode ${mode}"++# we need lo for qemu port forwarding+ipnetnsexec"${mode}0"iplinksetdevloup+ipnetnsexec"${mode}1"iplinksetdevloup+done+}++del_namespaces(){+formodein"${MODES[@]}";do+ipnetnsdel"${mode}0"+ipnetnsdel"${mode}1"+log_host"removed ns ${mode}0"+log_host"removed ns ${mode}1"+done&>/dev/null+}++ns_set_mode(){+localns=$1+localmode=$2++echo"${mode}"|ipnetnsexec"${ns}"\+tee/proc/net/vsock_ns_mode&>/dev/null+}+ vm_ssh(){-ssh-q-oUserKnownHostsFile=/dev/null-p${SSH_HOST_PORT}localhost"$@"+localns_exec++if[["${1}"==none]];then+localns_exec=""+else+localns_exec="ip netns exec ${1}"+fi++shift++${ns_exec}ssh-q-oUserKnownHostsFile=/dev/null-p${SSH_HOST_PORT}localhost$*+return$?} cleanup(){-if[[-s"${QEMU_PIDFILE}"]];then-pkill-SIGTERM-F"${QEMU_PIDFILE}">/dev/null2>&1-fi+del_namespaces+}-# If failure occurred during or before qemu start up, then we need-# to clean this up ourselves.-if[[-e"${QEMU_PIDFILE}"]];then-rm"${QEMU_PIDFILE}"-fi+terminate_pidfiles(){+localpidfile++forpidfilein"$@";do+if[[-s"${pidfile}"]];then+pkill-SIGTERM-F"${pidfile}"2>&1>/dev/null+fi++# If failure occurred during or before qemu start up, then we need+# to clean this up ourselves.+if[[-e"${pidfile}"]];then+rm-f"${pidfile}"+fi+done+}++terminate_pids(){+localpid++forpidin"$@";do+kill-SIGTERM"${pid}"&>/dev/null||:+done} check_args(){
@@ -278,17 +484,29 @@ wait_for_listener()} vm_wait_for_listener(){-localport=$1+localns=$1+localport=$2++log"Waiting for listener on port ${port} on vm"-vm_ssh<<EOF+vm_ssh"${ns}"<<EOF$(declare-fwait_for_listener) wait_for_listener${port}${WAIT_PERIOD}${WAIT_PERIOD_MAX} EOF} host_wait_for_listener(){-wait_for_listener"${TEST_HOST_PORT_LISTENER}""${WAIT_PERIOD}""${WAIT_PERIOD_MAX}"+localns=$1+localport=$2+if[["${ns}"==none]];then+wait_for_listener"${port}""${WAIT_PERIOD}""${WAIT_PERIOD_MAX}"+else+ipnetnsexec"${ns}"bash<<-EOF+$(declare-fwait_for_listener)+wait_for_listener${port}${WAIT_PERIOD}${WAIT_PERIOD_MAX}+EOF+fi} log(){
@@ -431,47 +649,499 @@ test_vm_loopback() {vm_vsock_test"none""client""127.0.0.1"1"${TEST_HOST_PORT_LISTENER}"}+test_host_vsock_ns_mode_ok(){+add_namespaces++formodein"${MODES[@]}";do+if!ns_set_mode"${mode}0""${mode}";then+del_namespaces+return"${KSFT_FAIL}"+fi+done+del_namespaces}-test_vm_client_host_server(){+test_host_vsock_ns_mode_write_once_ok(){+add_namespaces-${VSOCK_TEST}\---mode"server"\---control-port"${TEST_HOST_PORT_LISTENER}"\---peer-cid"${VSOCK_CID}"2>&1|log_host&+formodein"${MODES[@]}";do+localns="${mode}0"+if!ns_set_mode"${ns}""${mode}";then+del_namespaces+return"${KSFT_FAIL}"+fi-host_wait_for_listener+# try writing again and expect failure+ifns_set_mode"${ns}""${mode}";then+del_namespaces+return"${KSFT_FAIL}"+fi+done-vm_ssh--"${VSOCK_TEST}"\---mode=client\---control-host=10.0.2.2\---peer-cid=2\---control-port="${TEST_HOST_PORT_LISTENER}"2>&1|log_guest+del_namespaces-return$?+return"${KSFT_PASS}"}-test_vm_loopback(){-localport=60000# non-forwarded local port+namespaces_can_boot_same_cid(){+localns0=$1+localns1=$2+localpidfile1pidfile2+localcid=20+readonlycid+localrc-vm_ssh--"${VSOCK_TEST}"\---mode=server\---control-port="${port}"\---peer-cid=12>&1|log_guest&+pidfile1=$(mktemp/tmp/qemu_vsock_vmtest_XXXX.pid)+vm_start"${cid}""${ns0}""${pidfile1}"-vm_wait_for_listener"${port}"+pidfile2=$(mktemp/tmp/qemu_vsock_vmtest_XXXX.pid)+vm_start"${cid}""${ns1}""${pidfile2}"-vm_ssh--"${VSOCK_TEST}"\---mode=client\---control-host="127.0.0.1"\---control-port="${port}"\---peer-cid=12>&1|log_guest+rc=$?+terminate_pidfiles"${pidfile1}""${pidfile2}"-return$?+return$rc+}++test_global_same_cid_fails(){+ifnamespaces_can_boot_same_cid"global0""global1";then+return"${KSFT_FAIL}"+fi++return"${KSFT_PASS}"+}++test_local_global_same_cid_ok(){+ifnamespaces_can_boot_same_cid"local0""global0";then+return"${KSFT_PASS}"+fi++return"${KSFT_FAIL}"+}++test_global_local_same_cid_ok(){+ifnamespaces_can_boot_same_cid"global0""local0";then+return"${KSFT_PASS}"+fi++return"${KSFT_FAIL}"+}++test_local_same_cid_ok(){+ifnamespaces_can_boot_same_cid"local0""local0";then+return"${KSFT_FAIL}"+fi++return"${KSFT_PASS}"+}++test_diff_ns_global_host_connect_to_global_vm_ok(){+localpidspidpidfile+localns0ns1port+declare-apids+localunixfile+ns0="global0"+ns1="global1"+port=1234+localrc++pidfile=$(mktemp/tmp/qemu_vsock_vmtest_XXXX.pid)++if!vm_start"${VSOCK_CID}""${ns0}""${pidfile}";then+return"${KSFT_FAIL}"+fi++unixfile=$(mktemp-u/tmp/XXXX.sock)+ipnetnsexec"${ns1}"\+socatTCP-LISTEN:"${TEST_HOST_PORT}",fork\+UNIX-CONNECT:"${unixfile}"&+pids+=($!)+host_wait_for_listener"${ns1}""${TEST_HOST_PORT}"++ipnetnsexec"${ns0}"socatUNIX-LISTEN:"${unixfile}",fork\+TCP-CONNECT:localhost:"${TEST_HOST_PORT}"&+pids+=($!)++vm_vsock_test"${ns0}""server"2"${TEST_GUEST_PORT}"+vm_wait_for_listener"${ns0}""${TEST_GUEST_PORT}"+host_vsock_test"${ns1}""client""127.0.0.1""${VSOCK_CID}""${TEST_HOST_PORT}"+rc=$?++forpidin"${pids[@]}";do+if[["$(jobs-p)"=*"${pid}"*]];then+kill-SIGTERM"${pid}"&>/dev/null+fi+done++terminate_pidfiles"${pidfile}"++if[[$rc-ne0]];then+return"${KSFT_FAIL}"+fi++return"${KSFT_PASS}"}-run_test(){+test_diff_ns_global_host_connect_to_local_vm_fails(){+localns0="global0"+localns1="local0"+localport=12345+localpidfile+localresult+localpid++outfile=$(mktemp)++pidfile=$(mktemp/tmp/qemu_vsock_vmtest_XXXX.pid)+if!vm_start"${VSOCK_CID}""${ns1}""${pidfile}";then+log_host"failed to start vm (cid=${VSOCK_CID}, ns=${ns0})"+return$KSFT_FAIL+fi++vm_wait_for_ssh"${ns1}"+vm_ssh"${ns1}"--socatVSOCK-LISTEN:"${port}"STDOUT>"${outfile}"&+echoTEST|ipnetnsexec"${ns0}"\+socatSTDINVSOCK-CONNECT:"${VSOCK_CID}":"${port}"2>/dev/null++terminate_pidfiles"${pidfile}"++result=$(cat"${outfile}")+rm-f"${outfile}"++if[["${result}"!=TEST]];then+return$KSFT_PASS+fi++return$KSFT_FAIL+}++test_diff_ns_global_vm_connect_to_global_host_ok(){+localns0="global0"+localns1="global1"+localport=12345+localunixfile+localpidfile+localpids++declare-apids++log_host"Setup socat bridge from ns ${ns0} to ns ${ns1} over port ${port}"++unixfile=$(mktemp-u/tmp/XXXX.sock)++ipnetnsexec"${ns0}"\+socatTCP-LISTEN:"${port}"UNIX-CONNECT:"${unixfile}"&+pids+=($!)++ipnetnsexec"${ns1}"\+socatUNIX-LISTEN:"${unixfile}"TCP-CONNECT:127.0.0.1:"${port}"&+pids+=($!)++log_host"Launching ${VSOCK_TEST} in ns ${ns1}"+host_vsock_test"${ns1}""server""${VSOCK_CID}""${port}"++pidfile=$(mktemp/tmp/qemu_vsock_vmtest_XXXX.pid)+if!vm_start"${VSOCK_CID}""${ns0}""${pidfile}";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}"+vm_vsock_test"${ns0}""client""10.0.2.2"2"${port}"+rc=$?++terminate_pidfiles"${pidfile}"+terminate_pids"${pids[@]}"+rm-f"${unixfile}"++if[[!$rc-eq0]];then+return"${KSFT_FAIL}"+fi++return"${KSFT_PASS}"++}++test_diff_ns_global_vm_connect_to_local_host_fails(){+localns0="global0"+localns1="local0"+localport=12345+localpidfile+localresult+localpid++log_host"Launching socat in ns ${ns1}"+outfile=$(mktemp)+ipnetnsexec"${ns1}"socatVSOCK-LISTEN:${port}STDOUT&>"${outfile}"&+pid=$!++pidfile=$(mktemp/tmp/qemu_vsock_vmtest_XXXX.pid)+if!vm_start"${VSOCK_CID}""${ns0}""${pidfile}";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}"++vm_ssh"${ns0}"--\+bash-c"echo TEST | socat STDIN VSOCK-CONNECT:2:${port}"2>&1|log_guest++terminate_pidfiles"${pidfile}"+terminate_pids"${pid}"++result=$(cat"${outfile}")+rm-f"${outfile}"++if[["${result}"!=TEST]];then+return"${KSFT_PASS}"+fi++return"${KSFT_FAIL}"+}++test_diff_ns_local_host_connect_to_local_vm_fails(){+localns0="local0"+localns1="local1"+localport=12345+localpidfile+localresult+localpid++outfile=$(mktemp)++pidfile=$(mktemp/tmp/qemu_vsock_vmtest_XXXX.pid)+if!vm_start"${VSOCK_CID}""${ns1}""${pidfile}";then+log_host"failed to start vm (cid=${cid}, ns=${ns0})"+return$KSFT_FAIL+fi++vm_wait_for_ssh"${ns1}"+vm_ssh"${ns1}"--socatVSOCK-LISTEN:"${port}"STDOUT>"${outfile}"&+echoTEST|ipnetnsexec"${ns0}"\+socatSTDINVSOCK-CONNECT:"${VSOCK_CID}":"${port}"2>/dev/null++terminate_pidfiles"${pidfile}"++result=$(cat"${outfile}")+rm-f"${outfile}"++if[["${result}"!=TEST]];then+return$KSFT_PASS+fi++return$KSFT_FAIL+}++test_diff_ns_local_vm_connect_to_local_host_fails(){+localns0="local0"+localns1="local1"+localport=12345+localpidfile+localresult+localpid++log_host"Launching socat in ns ${ns1}"+outfile=$(mktemp)+ipnetnsexec"${ns1}"socatVSOCK-LISTEN:"${port}"STDOUT&>"${outfile}"&+pid=$!++pidfile=$(mktemp/tmp/qemu_vsock_vmtest_XXXX.pid)+if!vm_start"${VSOCK_CID}""${ns0}""${pidfile}";then+log_host"failed to start vm (cid=${cid}, ns=${ns0})"+rm-f"${outfile}"+return"${KSFT_FAIL}"+fi++vm_wait_for_ssh"${ns0}"++vm_ssh"${ns0}"--\+bash-c"echo TEST | socat STDIN VSOCK-CONNECT:2:${port}"2>&1|log_guest++terminate_pidfiles"${pidfile}"+terminate_pids"${pid}"++result=$(cat"${outfile}")+rm-f"${outfile}"++if[["${result}"!=TEST]];then+return"${KSFT_PASS}"+fi++return"${KSFT_FAIL}"+}++__test_loopback_two_netns(){+localns0=$1+localns1=$2+localport=12345+localresult+localpid++log_host"Launching socat in ns ${ns1}"+outfile=$(mktemp)+ipnetnsexec"${ns1}"socatVSOCK-LISTEN:"${port}"STDOUT>"${outfile}"2>/dev/null&+pid=$!++log_host"Launching socat in ns ${ns0}"+echoTEST|ipnetnsexec"${ns0}"socatSTDINVSOCK-CONNECT:1:"${port}"2>/dev/null+terminate_pids"${pid}"++result=$(cat"${outfile}")+rm-f"${outfile}"++if[["${result}"==TEST]];then+return0+fi++return1+}++test_diff_ns_global_to_local_loopback_local_fails(){+if!__test_loopback_two_netns"global0""local0";then+return"${KSFT_PASS}"+fi++return"${KSFT_FAIL}"+}++test_diff_ns_local_to_global_loopback_fails(){+if!__test_loopback_two_netns"local0""global0";then+return"${KSFT_PASS}"+fi++return"${KSFT_FAIL}"+}++test_diff_ns_local_to_local_loopback_fails(){+if!__test_loopback_two_netns"local0""local1";then+return"${KSFT_PASS}"+fi++return"${KSFT_FAIL}"+}++test_diff_ns_global_to_global_loopback_ok(){+if__test_loopback_two_netns"global0""global1";then+return"${KSFT_PASS}"+fi++return"${KSFT_FAIL}"+}++test_same_ns_local_loopback_ok(){+if__test_loopback_two_netns"local0""local0";then+return"${KSFT_PASS}"+fi++return"${KSFT_FAIL}"+}++test_same_ns_local_host_connect_to_local_vm_ok(){+localns="local0"+localport=1234+localpidfile+localrc++pidfile=$(mktemp/tmp/qemu_vsock_vmtest_XXXX.pid)++if!vm_start"${VSOCK_CID}""${ns}""${pidfile}";then+return"${KSFT_FAIL}"+fi++vm_vsock_test"${ns}""server"2"${TEST_GUEST_PORT}"+host_vsock_test"${ns}""client""127.0.0.1""${VSOCK_CID}""${TEST_HOST_PORT}"+rc=$?++terminate_pidfiles"${pidfile}"++if[[$rc-ne0]];then+return"${KSFT_FAIL}"+fi++return"${KSFT_PASS}"+}++test_same_ns_local_vm_connect_to_local_host_ok(){+localns="local0"+localport=1234+localpidfile+localrc++pidfile=$(mktemp/tmp/qemu_vsock_vmtest_XXXX.pid)++if!vm_start"${VSOCK_CID}""${ns}""${pidfile}";then+return"${KSFT_FAIL}"+fi++vm_vsock_test"${ns}""server"2"${TEST_GUEST_PORT}"+host_vsock_test"${ns}""client""127.0.0.1""${VSOCK_CID}""${TEST_HOST_PORT}"+rc=$?++terminate_pidfiles"${pidfile}"++if[[$rc-ne0]];then+return"${KSFT_FAIL}"+fi++return"${KSFT_PASS}"+}++shared_vm_test(){+localtname++tname="${1}"++fortestnamein"${USE_SHARED_VM[@]}";do+if[["${tname}"=="${testname}"]];then+return0+fi+done++return1+}+++init_netns_test(){+localtname++tname="${1}"++fortestnamein"${USE_INIT_NETNS[@]}";do+if[["${tname}"=="${testname}"]];then+return0+fi+done++return1+}++check_result(){+localrcnum++rc=$1+num=$((cnt_total+1))++if[[${rc}-eq$KSFT_PASS]];then+cnt_pass=$((cnt_pass+1))+echo"ok ${num}${arg}"+elif[[${rc}-eq$KSFT_SKIP]];then+cnt_skip=$((cnt_skip+1))+echo"ok ${num}${arg} # SKIP"+elif[[${rc}-eq$KSFT_FAIL]];then+cnt_fail=$((cnt_fail+1))+echo"not ok ${num}${arg} # exit=$rc"+fi++cnt_total=$((cnt_total+1))+}++run_shared_vm_tests(){+localstart_shared_vmpidfilelocalhost_oops_cnt_beforelocalhost_warn_cnt_beforelocalvm_oops_cnt_before
@@ -483,40 +1153,90 @@ run_test() {localnamelocalrc-host_oops_cnt_before=$(dmesg|grep-c-i'Oops')-host_warn_cnt_before=$(dmesg--level=warn|wc-l)-vm_oops_cnt_before=$(vm_ssh--dmesg|grep-c-i'Oops')-vm_warn_cnt_before=$(vm_ssh--dmesg--level=warn|wc-l)+start_shared_vm=0-name=$(echo"${1}"|awk'{ print $1 }')-evaltest_"${name}"-rc=$?+forargin"${ARGS[@]}";do+ifshared_vm_test"${arg}";then+start_shared_vm=1+break+fi+done-host_oops_cnt_after=$(dmesg|grep-i'Oops'|wc-l)-if[[${host_oops_cnt_after}-gt${host_oops_cnt_before}]];then-echo"FAIL: kernel oops detected on host"|log_host"${name}"-rc=$KSFT_FAIL+pidfile=""+if[["${start_shared_vm}"==1]];then+pidfile=$(mktemp$PIDFILE_TEMPLATE)+log_host"Booting up VM"+vm_start"${VSOCK_CID}""none""${pidfile}"+vm_wait_for_ssh"none"+log_host"VM booted up"fi-host_warn_cnt_after=$(dmesg--level=warn|wc-l)-if[[${host_warn_cnt_after}-gt${host_warn_cnt_before}]];then-echo"FAIL: kernel warning detected on host"|log_host"${name}"-rc=$KSFT_FAIL-fi+forargin"${ARGS[@]}";do+if!shared_vm_test"${arg}";then+continue+fi-vm_oops_cnt_after=$(vm_ssh--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"${name}"-rc=$KSFT_FAIL-fi+host_oops_cnt_before=$(dmesg|grep-c-i'Oops')+host_warn_cnt_before=$(dmesg--level=warn|wc-l)+vm_oops_cnt_before=$(vm_sshnone--dmesg|grep-c-i'Oops')+vm_warn_cnt_before=$(vm_sshnone--dmesg--level=warn|wc-l)++name=$(echo"${arg}"|awk'{ print $1 }')+log_host"Executing test_${name}"+evaltest_"${name}"+rc=$?++host_oops_cnt_after=$(dmesg|grep-i'Oops'|wc-l)+if[[${host_oops_cnt_after}-gt${host_oops_cnt_before}]];then+echo"FAIL: kernel oops detected on host"|log_host"${name}"+rc=$KSFT_FAIL+fi++host_warn_cnt_after=$(dmesg--level=warn|wc-l)+if[[${host_warn_cnt_after}-gt${host_warn_cnt_before}]];then+echo"FAIL: kernel warning detected on host"|log_host"${name}"+rc=$KSFT_FAIL+fi-vm_warn_cnt_after=$(vm_ssh--dmesg--level=warn|wc-l)-if[[${vm_warn_cnt_after}-gt${vm_warn_cnt_before}]];then-echo"FAIL: kernel warning detected on vm"|log_host"${name}"-rc=$KSFT_FAIL+vm_oops_cnt_after=$(vm_sshnone--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"${name}"+rc=$KSFT_FAIL+fi++vm_warn_cnt_after=$(vm_sshnone--dmesg--level=warn|wc-l)+if[[${vm_warn_cnt_after}-gt${vm_warn_cnt_before}]];then+echo"FAIL: kernel warning detected on vm"|log_host"${name}"+rc=$KSFT_FAIL+fi++check_result"${rc}"+done++if[[-n"${pidfile}"]];then+log_host"VM terminate"+terminate_pidfiles"${pidfile}"fi+}++run_isolated_vm_tests(){+forargin"${ARGS[@]}";do+ifshared_vm_test"${arg}";then+continue+fi-return"${rc}"+add_namespaces+ifinit_netns_test"${arg}";then+init_namespaces+fi++name=$(echo"${arg}"|awk'{ print $1 }')+log_host"Executing test_${name}"+evaltest_"${name}"+check_result$?++del_namespaces+done}QEMU="qemu-system-$(uname-m)"
@@ -543,34 +1263,13 @@ fi check_args"${ARGS[@]}" check_deps check_vng+check_socat handle_buildecho"1..${#ARGS[@]}"-log_host"Booting up VM"-vm_start-vm_wait_for_ssh-log_host"VM booted up"--cnt_pass=0-cnt_fail=0-cnt_skip=0-cnt_total=0-forargin"${ARGS[@]}";do-run_test"${arg}"-rc=$?-if[[${rc}-eq$KSFT_PASS]];then-cnt_pass=$((cnt_pass+1))-echo"ok ${cnt_total}${arg}"-elif[[${rc}-eq$KSFT_SKIP]];then-cnt_skip=$((cnt_skip+1))-echo"ok ${cnt_total}${arg} # SKIP"-elif[[${rc}-eq$KSFT_FAIL]];then-cnt_fail=$((cnt_fail+1))-echo"not ok ${cnt_total}${arg} # exit=$rc"-fi-cnt_total=$((cnt_total+1))-done+run_shared_vm_tests+run_isolated_vm_testsecho"SUMMARY: PASS=${cnt_pass} SKIP=${cnt_skip} FAIL=${cnt_fail}"echo"Log: ${LOG}"
From: Bobby Eshleman <hidden> Date: 2025-08-05 22:03:40
On Tue, Aug 05, 2025 at 02:49:08PM -0700, Bobby Eshleman wrote:
...
Thanks again for everyone's help and reviews!
Changes in v4:
- removed RFC tag
My bad, I didn't notice I still had the rfc tag before sending out with
b4.
This is ready for review and not really an RFC. All test cases passing,
etc...
-Bobby
From: Simon Horman <horms@kernel.org> Date: 2025-08-06 19:12:47
On Tue, Aug 05, 2025 at 02:49:17PM -0700, Bobby Eshleman wrote:
From: Bobby Eshleman <redacted>
Add NS support to vsock loopback. Sockets in a global mode netns
communicate with each other, regardless of namespace. Sockets in a local
mode netns may only communicate with other sockets within the same
namespace.
Signed-off-by: Bobby Eshleman <redacted>
This change needs to be squashed into
PATCH 3/12 vsock: add netns to af_vsock core
To avoid build breakage.
Likewise with the other change to vsock_loopback_seqpacket_allow below.
And I think also for a number of other changes made by PATCH 3/12.
Please make sure that patches don't introduce transient build failures.
It breaks bisection.
On the topic of vsock_loopback_seqpacket_allow, also:
* Please line wrap this so that the code is 80 columns wide or less,
as is still preferred for Networking code.
Flagged by checkpatch.pl --max-line-length=80
* Can we move the definition of vsock_loopback_seqpacket_allow() here?
The function itself is is trivial. And doing so would avoid a forward
declaration.
I think EXPORT_SYMBOL_GPL is needed for both vsock_loopback_exit_net and
vsock_loopback_init_net for the case where CONFIG_VSOCKETS=m
Also, in Kconfig VSOCKETS_LOOPBACK depends on VSOCKETS. But this code adds
a reverse dependency. As it stands it's possible to configure VSOCKETS
without VSOCKETS_LOOPBACK, which will not compile.
Perhaps stub implementations of vsock_loopback_init_net and
vsock_loopback_exit_net should be implemented in af_vsock.h if
VSOCKETS_LOOPBACK is not enabled?
...
--
pw-bot: changes-requested
From: Simon Horman <horms@kernel.org> Date: 2025-08-06 19:13:55
On Tue, Aug 05, 2025 at 03:03:37PM -0700, Bobby Eshleman wrote:
On Tue, Aug 05, 2025 at 02:49:08PM -0700, Bobby Eshleman wrote:
...
quoted
Thanks again for everyone's help and reviews!
Changes in v4:
- removed RFC tag
My bad, I didn't notice I still had the rfc tag before sending out with
b4.
This is ready for review and not really an RFC. All test cases passing,
etc...
Ack. But net-next is currently closed for the merge-window.
So please don't post non-RFC patches for it until it reopens,
around the 11th August.
From: Bobby Eshleman <hidden> Date: 2025-08-06 21:31:15
On Wed, Aug 06, 2025 at 08:12:39PM +0100, Simon Horman wrote:
On Tue, Aug 05, 2025 at 02:49:17PM -0700, Bobby Eshleman wrote:
quoted
From: Bobby Eshleman <redacted>
...
This change needs to be squashed into
PATCH 3/12 vsock: add netns to af_vsock core
To avoid build breakage.
Likewise with the other change to vsock_loopback_seqpacket_allow below.
And I think also for a number of other changes made by PATCH 3/12.
Please make sure that patches don't introduce transient build failures.
It breaks bisection.
Will do, thanks!
On the topic of vsock_loopback_seqpacket_allow, also:
* Please line wrap this so that the code is 80 columns wide or less,
as is still preferred for Networking code.
Flagged by checkpatch.pl --max-line-length=80
* Can we move the definition of vsock_loopback_seqpacket_allow() here?
The function itself is is trivial. And doing so would avoid a forward
declaration.
I think EXPORT_SYMBOL_GPL is needed for both vsock_loopback_exit_net and
vsock_loopback_init_net for the case where CONFIG_VSOCKETS=m
Also, in Kconfig VSOCKETS_LOOPBACK depends on VSOCKETS. But this code adds
a reverse dependency. As it stands it's possible to configure VSOCKETS
without VSOCKETS_LOOPBACK, which will not compile.
Perhaps stub implementations of vsock_loopback_init_net and
vsock_loopback_exit_net should be implemented in af_vsock.h if
VSOCKETS_LOOPBACK is not enabled?
Roger that, makes sense. Thanks for the review!
Best,
Bobby
Hi Bobby,
On Tue, Aug 05, 2025 at 02:49:08PM -0700, Bobby Eshleman wrote:
This series adds namespace support to vhost-vsock. It does not add
namespaces to any of the guest transports (virtio-vsock, hyperv, or
vmci).
The current revision only supports two modes: local or global. Local
mode is complete isolation of namespaces, while global mode is complete
sharing between namespaces of CIDs (the original behavior).
Future may include supporting a mixed mode, which I expect to be more
complicated because socket lookups will have to include new logic and
API changes to behave differently based on if the lookup is part of a
mixed mode CID allocation, a global CID allocation, a mixed-to-global
connection (allowed), or a global-to-mixed connection (not allowed).
Modes are per-netns and write-once. This allows a system to configure
namespaces independently (some may share CIDs, others are completely
isolated). This also supports future 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.
Thanks again for everyone's help and reviews!
Thanks for your work!
As I mentioned to you, I'll be off for the next 2 weeks, so I'll take a
look when I'm back, but feel free to send new versions if you receive
enough comments on this.
Thanks,
Stefano
Signed-off-by: Bobby Eshleman <redacted>
To: Stefano Garzarella <sgarzare@redhat.com>
To: Shuah Khan <shuah@kernel.org>
To: David S. Miller <davem@davemloft.net>
To: Eric Dumazet <edumazet@google.com>
To: Jakub Kicinski <kuba@kernel.org>
To: Paolo Abeni <pabeni@redhat.com>
To: Simon Horman <horms@kernel.org>
To: Stefan Hajnoczi <stefanha@redhat.com>
To: Michael S. Tsirkin <mst@redhat.com>
To: Jason Wang <redacted>
To: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
To: Eugenio Pérez <eperezma@redhat.com>
To: K. Y. Srinivasan <kys@microsoft.com>
To: Haiyang Zhang <haiyangz@microsoft.com>
To: Wei Liu <wei.liu@kernel.org>
To: Dexuan Cui <decui@microsoft.com>
To: Bryan Tan <bryan-bt.tan@broadcom.com>
To: Vishnu Dasa <vishnu.dasa@broadcom.com>
To: Broadcom internal kernel review list <bcm-kernel-feedback-list@broadcom.com>
Cc: virtualization@lists.linux.dev
Cc: netdev@vger.kernel.org
Cc: linux-kselftest@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: kvm@vger.kernel.org
Cc: linux-hyperv@vger.kernel.org
Cc: berrange@redhat.com
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/20250312-vsock-netns-v2-0-84bffa1aa97a@gmail.com
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/20200116172428.311437-1-sgarzare@redhat.com
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: a per-net vsock NS mode state
vsock: add net to vsock skb cb
vsock: add netns to af_vsock core
vsock/virtio: add netns to virtio transport common
vhost/vsock: add netns support
vsock/virtio: use the global netns
hv_sock: add netns hooks
vsock/vmci: add netns hooks
vsock/loopback: add netns support
selftests/vsock: improve logging in vmtest.sh
selftests/vsock: invoke vsock_test through helpers
selftests/vsock: add namespace tests
MAINTAINERS | 1 +
drivers/vhost/vsock.c | 48 +-
include/linux/virtio_vsock.h | 12 +
include/net/af_vsock.h | 59 +-
include/net/net_namespace.h | 4 +
include/net/netns/vsock.h | 21 +
net/vmw_vsock/af_vsock.c | 204 +++++-
net/vmw_vsock/hyperv_transport.c | 2 +-
net/vmw_vsock/virtio_transport.c | 5 +-
net/vmw_vsock/virtio_transport_common.c | 14 +-
net/vmw_vsock/vmci_transport.c | 4 +-
net/vmw_vsock/vsock_loopback.c | 59 +-
tools/testing/selftests/vsock/vmtest.sh | 1088 ++++++++++++++++++++++++++-----
13 files changed, 1330 insertions(+), 191 deletions(-)
---
base-commit: dd500e4aecf25e48e874ca7628697969df679493
change-id: 20250325-vsock-vmtest-b3a21d2102c2
Best regards,
--
Bobby Eshleman [off-list ref]
From: Bobby Eshleman <hidden> Date: 2025-08-07 20:24:37
On Thu, Aug 07, 2025 at 10:06:35AM +0200, Stefano Garzarella wrote:
Hi Bobby,
...
Thanks for your work!
As I mentioned to you, I'll be off for the next 2 weeks, so I'll take a look
when I'm back, but feel free to send new versions if you receive enough
comments on this.
Thanks,
Stefano