]> git.ipfire.org Git - thirdparty/linux.git/commit
vsock: add G2H fallback for CIDs not owned by H2G transport
authorAlexander Graf <graf@amazon.com>
Wed, 4 Mar 2026 23:00:27 +0000 (23:00 +0000)
committerPaolo Abeni <pabeni@redhat.com>
Thu, 12 Mar 2026 09:59:36 +0000 (10:59 +0100)
commit0de607dc4fd80ede3b2a35e8a72f99c7a0bbc321
tree72878c52d5623288bd4e0ff528e6d6f9c73d146f
parent17edc4e820bf8b4c7737c1de86c267e6974d543a
vsock: add G2H fallback for CIDs not owned by H2G transport

When no H2G transport is loaded, vsock currently routes all CIDs to the
G2H transport (commit 65b422d9b61b ("vsock: forward all packets to the
host when no H2G is registered"). Extend that existing behavior: when
an H2G transport is loaded but does not claim a given CID, the
connection falls back to G2H in the same way.

This matters in environments like Nitro Enclaves, where an instance may
run nested VMs via vhost-vsock (H2G) while also needing to reach sibling
enclaves at higher CIDs through virtio-vsock-pci (G2H). With the old
code, any CID > 2 was unconditionally routed to H2G when vhost was
loaded, making those enclaves unreachable without setting
VMADDR_FLAG_TO_HOST explicitly on every connect.

Requiring every application to set VMADDR_FLAG_TO_HOST creates friction:
tools like socat, iperf, and others would all need to learn about it.
The flag was introduced 6 years ago and I am still not aware of any tool
that supports it. Even if there was support, it would be cumbersome to
use. The most natural experience is a single CID address space where H2G
only wins for CIDs it actually owns, and everything else falls through to
G2H, extending the behavior that already exists when H2G is absent.

To give user space at least a hint that the kernel applied this logic,
automatically set the VMADDR_FLAG_TO_HOST on the remote address so it
can determine the path taken via getpeername().

Add a per-network namespace sysctl net.vsock.g2h_fallback (default 1).
At 0 it forces strict routing: H2G always wins for CID > VMADDR_CID_HOST,
or ENODEV if H2G is not loaded.

Signed-off-by: Alexander Graf <graf@amazon.com>
Tested-by: syzbot@syzkaller.appspotmail.com
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
Link: https://patch.msgid.link/20260304230027.59857-1-graf@amazon.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
Documentation/admin-guide/sysctl/net.rst
drivers/vhost/vsock.c
include/net/af_vsock.h
include/net/netns/vsock.h
net/vmw_vsock/af_vsock.c
net/vmw_vsock/virtio_transport.c