vsock: add datagram support to virtio and vhost
Subsystems (12)
v2 cover vsock: add datagram support to virtio and vhost
This RFC adds SOCK_DGRAM support to virtio-vsock and vhost-vsock through the existing AF_VSOCK API. It is intended for guest and host services that exchange independent requests, responses, or events and need message boundaries and sender addresses without a stream connection or application framing.
VIRTIO_VSOCK_F_DGRAM gates virtio traffic in both directions. Each virtio send carries zero to 64 KiB of payload in one VIRTIO_VSOCK_TYPE_DGRAM packet. On the RX virtqueue, the device may split a datagram across page-sized guest buffers.
START and EOM delimit the message; the guest reassembles at most 64 fragments and 64 KiB before delivering it to a socket. Invalid or incomplete messages are discarded. Datagram delivery is best effort:
there are no peer credits, acknowledgements, or retransmissions. Send admission uses the socket send-memory limit, and SIOCOUTQ reports charged send memory.
Datagram sockets have a separate port space from stream and seqpacket sockets. The latter two continue to share their port space. Datagram routes are chosen per destination among guest-to-host, host-to-guest, and loopback transports, with VMCI as a fallback. VMCI still owns its socket handle when it is present; loopback works without a directional transport. Hyper-V continues to reject datagram sockets.
Bobby Eshleman and Amery Hung explored this feature in their v6 series and v7 work-in-progress branch. This series is a fresh implementation against the current vsock code. It follows the separate datagram port space, per-destination routing, and VMCI fallback requirements discussed there. For guest receive buffers, it uses bounded fragmentation and reassembly rather than full-message buffers or the earlier drop/error queue approach:
https://lore.kernel.org/r/[email protected]
https://github.com/ameryhung/linux/tree/vsock-dgram-v7
A corresponding virtio specification update has been drafted separately. The wire format and feature negotiation still need specification review before this series can be considered for merging.
The first patches expose the core port allocator and add datagram queue reporting, then implement the common data path, guest and host feature negotiation, routing, and RX fragmentation. The last patch extends vsock_test with local and peer tests for message boundaries, routing, connected peers, port sharing, queue accounting, and delayed reads.
Testing used QEMU with the F_DGRAM property patch below, an x86-64 KASAN/lockdep kernel, paired guest tests, and a nested Linux vhost-vsock backend. The tested paths include 64 KiB messages, delayed-read bursts, feature-disabled ingress, stream and seqpacket controls, and header-only guest RX buffers. A temporary userspace backend still fails stream test 21 on both the series base and tip; that case passes with Linux vhost. Real VMCI and Hyper-V hardware were not exercised.
https://lists.gnu.org/archive/html/qemu-devel/2026-10/msg08074.html
Code was written and reviewed both manually and assisted by Codex (GPT-6.1-Sol) and Claude (Opus 4.6).
---
Changes in v2:
- Keep guest RX buffers page-sized; add START/EOM framing and bounded
datagram reassembly (thanks to Alexei Starovoitov and Bobby Eshleman).
- Preserve stream and seqpacket data when an RX buffer has no payload
room.
- Separate datagram and connection-oriented port spaces, and share
receive metadata and queueing (thanks to Alexei Starovoitov).
- Check negotiated support and destination CID on ingress, and require
an exact connected peer CID and port (thanks to Sashiko AI bot).
- Select a capable route for each destination, retain VMCI as fallback,
and support loopback-only sockets (thanks to Alexei Starovoitov).
- Preserve the connected peer address across temporary parent fallback
(thanks to Sashiko AI bot).
- Use socket write-memory accounting for datagram SIOCOUTQ instead of
separate transport counters (thanks to Alexei Starovoitov).
- Extend selftests for fragmentation, delayed reads, port sharing,
connected peers, and capability-based skips; fix the test build and
README entries (thanks to Alexei Starovoitov, Bobby Eshleman,
and Sashiko AI bot).
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Sergio Lopez (8):
vsock: expose the core port allocator to transports
vsock: report datagram receive and send queue state vsock/virtio: implement the datagram data path vsock/virtio: negotiate datagram support on the guest vhost/vsock: negotiate datagram support with guests
vsock: allow datagrams to use directional transports
vsock: fragment datagrams across receive buffers
vsock: add datagram tests to vsock_test
Documentation/admin-guide/sysctl/net.rst | 8 +- drivers/vhost/vsock.c | 51 +- include/linux/virtio_vsock.h | 9 +- include/net/af_vsock.h | 30 +- include/uapi/linux/virtio_vsock.h | 10 + net/vmw_vsock/af_vsock.c | 459 +++++++++++--- net/vmw_vsock/hyperv_transport.c | 11 +- net/vmw_vsock/virtio_transport.c | 167 ++++- net/vmw_vsock/virtio_transport_common.c | 164 ++++- net/vmw_vsock/vmci_transport.c | 115 ++-- net/vmw_vsock/vsock_bpf.c | 3 +- net/vmw_vsock/vsock_loopback.c | 2 +- tools/testing/vsock/Makefile | 3 +- tools/testing/vsock/README | 31 +- tools/testing/vsock/util.c | 62 +- tools/testing/vsock/util.h | 10 + tools/testing/vsock/vsock_test.c | 175 +++++- tools/testing/vsock/vsock_test_dgram.c | 1000 ++++++++++++++++++++++++++++++ tools/testing/vsock/vsock_test_dgram.h | 29 + 19 files changed, 2066 insertions(+), 273 deletions(-)
---
base-commit: 65ba8f34ec886ec72b2c07ddc8cf3d815c32869d
change-id: 20261027-vsock-dgram-support-683a4e0aebe7
Best regards,
Signature · 1 line
Sergio Lopez <[email protected]>
RFC PATCH v2 1/8 vsock: expose the core port allocator to transports
The core port allocator is currently private to connection-oriented sockets. Virtio datagrams need the core bound table and port allocation, while VMCI datagrams must keep their own endpoint creation and port namespace.
Export vsock_bind_common() with the bound-table locking inside the helper, and use it for stream and SEQPACKET binds. Keep datagram binds delegated to the transport so a transport can opt into the core allocator without changing other transports' binding behavior.
Keep datagram and connection-oriented sockets in separate logical port spaces within the core bound table. Filter bind conflicts and bound-socket lookups by class, including wildcard CIDs and automatic port allocation, so datagram and stream/SEQPACKET sockets can share a CID and port. Stream and SEQPACKET sockets continue to conflict with each other.
--- include/net/af_vsock.h | 3 ++ net/vmw_vsock/af_vsock.c | 98 +++++++++++++++++++++++++++++++----------------- 2 files changed, 66 insertions(+), 35 deletions(-)
include/net/af_vsock.h +3 -0
@@ -220,6 +220,7 @@ void vsock_remove_pending(struct sock *listener, struct sock *pending);void vsock_enqueue_accept (struct sock *listener,struct sock *connected);void vsock_pending_to_accept (struct sock *listener,struct sock *pending);void vsock_insert_connected (struct vsock_sock *vsk); +int vsock_bind_common (struct vsock_sock *vsk,struct sockaddr_vm *addr);void vsock_remove_bound (struct vsock_sock *vsk);void vsock_remove_connected (struct vsock_sock *vsk);struct sock *vsock_find_bound_socket (struct sockaddr_vm *addr);
@@ -227,6 +228,8 @@ struct sock *vsock_find_connected_socket(struct sockaddr_vm *src, struct sockaddr_vm *dst);struct sock *vsock_find_bound_socket_net (struct sockaddr_vm *addr,struct net *net); +struct sock *vsock_find_bound_dgram_socket_net (struct sockaddr_vm *addr, +struct net *net);struct sock *vsock_find_connected_socket_net (struct sockaddr_vm *src,struct sockaddr_vm *dst,struct net *net);
net/vmw_vsock/af_vsock.c +63 -35
175 lines not on this page · raw message
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC v2 1/8] vsock: expose the core port allocator to transports
Sashiko has reviewed this patch and found no issues.
Quoted 10 lines
The core port allocator is currently private to connection-oriented sockets. Virtio datagrams need the core bound table and port allocation, while VMCI datagrams must keep their own endpoint creation and port namespace.
Export
vsock_bind_common()with the bound-table locking inside the helper, and use it for stream and SEQPACKET binds. Keep datagram binds delegated to the transport so a transport can opt into the core allocator without changing other transports' binding behavior.
Quoted 7 lines
Keep datagram and connection-oriented sockets in separate logical port spaces within the core bound table. Filter bind conflicts and bound-socket lookups by class, including wildcard CIDs and automatic port allocation, so datagram and stream/SEQPACKET sockets can share a CID and port. Stream and SEQPACKET sockets continue to conflict with each other.
Signed-off-by Sergio Lopez <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
RFC PATCH v2 2/8 vsock: report datagram receive and send queue state
In reply to Sergio LopezDatagram receive queues hold whole messages, and transports may charge queued datagrams to the socket send buffer. Report the size of the next datagram payload for SIOCINQ under the receive-queue lock, so concurrent recvmsg() cannot free the skb during the query. Let transports override the length calculation to exclude headers such as VMCI's.
Report datagram write readiness only while the send buffer has space. Set the asynchronous no-space flag when it is full so freeing pending send buffers can wake poll waiters.
--- include/net/af_vsock.h | 2 ++ net/vmw_vsock/af_vsock.c | 32 ++++++++++++++++++++++++-------- net/vmw_vsock/vmci_transport.c | 9 +++++++++ 3 files changed, 35 insertions(+), 8 deletions(-)
include/net/af_vsock.h +2 -0
@@ -124,6 +124,8 @@ struct vsock_transport {int (*dgram_enqueue)(struct vsock_sock *,struct sockaddr_vm *,struct msghdr *,size_t len);bool (*dgram_allow)(struct vsock_sock *vsk,u32 cid,u32 port); +/* Optional payload length override, called with the receive queue locked. */ +size_t (*dgram_payload_len)(const struct sk_buff *skb);/* STREAM. */ /* TODO: stream_bind() */
net/vmw_vsock/af_vsock.c +24 -8
@@ -1359,16 +1359,20 @@ static __poll_t vsock_poll(struct file *file, struct socket *sock, mask |=vsock_poll_shutdown (sk, peer_shutdown);/* For datagram sockets we can read if there is something in -* the queue and write as long as the socket isn't shutdown for -* sending. +* the queue and write if the send buffer has space and the +* socket isn't shutdown for sending. */ if (!skb_queue_empty_lockless (&sk->sk_receive_queue) || (sk->sk_shutdown &RCV_SHUTDOWN )) { mask |=EPOLLIN |EPOLLRDNORM ; } -if (!(sk->sk_shutdown &SEND_SHUTDOWN )) - mask |=EPOLLOUT |EPOLLWRNORM |EPOLLWRBAND ; +if (!(sk->sk_shutdown &SEND_SHUTDOWN )) { +if (sock_writeable (sk)) + mask |=EPOLLOUT |EPOLLWRNORM |EPOLLWRBAND ; +else +sk_set_bit (SOCKWQ_ASYNC_NOSPACE , sk); + } }else if (sock_type_connectible (sk->sk_type)) {const struct vsock_transport *transport;
@@ -1632,10 +1636,22 @@ static int vsock_do_ioctl(struct socket *sock, unsigned int cmd,break ; } - n_bytes =vsock_stream_has_data (vsk); -if (n_bytes <0 ) { - ret = n_bytes; -break ; +if (sk->sk_type ==SOCK_DGRAM ) { +struct sk_buff *skb; + +spin_lock_bh (&sk->sk_receive_queue.lock); + skb =skb_peek (&sk->sk_receive_queue); +if (skb && vsk->transport->dgram_payload_len) + n_bytes = vsk->transport->dgram_payload_len (skb); +else + n_bytes = skb ? skb->len :0 ; +spin_unlock_bh (&sk->sk_receive_queue.lock); + }else { + n_bytes =vsock_stream_has_data (vsk); +if (n_bytes <0 ) { + ret = n_bytes; +break ; + } } ret =put_user (n_bytes, arg);break ;
net/vmw_vsock/vmci_transport.c +9 -0
@@ -1761,6 +1761,14 @@ static int vmci_transport_dgram_enqueue(return err -sizeof (*dg); } +static size_t vmci_transport_dgram_payload_len (const struct sk_buff *skb) +{ +if (skb->len <sizeof (struct vmci_datagram )) +return 0 ; + +return skb->len -sizeof (struct vmci_datagram ); +} +static int vmci_transport_dgram_dequeue (struct vsock_sock *vsk,struct msghdr *msg,size_t len,int flags)
@@ -2075,6 +2083,7 @@ static struct vsock_transport vmci_transport = { .connect = vmci_transport_connect, .dgram_bind = vmci_transport_dgram_bind, .dgram_dequeue = vmci_transport_dgram_dequeue, + .dgram_payload_len = vmci_transport_dgram_payload_len, .dgram_enqueue = vmci_transport_dgram_enqueue, .dgram_allow = vmci_transport_dgram_allow, .stream_dequeue = vmci_transport_stream_dequeue,
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC v2 2/8] vsock: report datagram receive and send queue state
Sashiko has reviewed this patch and found no issues.
Quoted 7 lines
Datagram receive queues hold whole messages, and transports may charge queued datagrams to the socket send buffer. Report the size of the next datagram payload for SIOCINQ under the receive-queue lock, so concurrent
recvmsg()cannot free the skb during the query. Let transports override the length calculation to exclude headers such as VMCI's.
Quoted 5 lines
Report datagram write readiness only while the send buffer has space. Set the asynchronous no-space flag when it is full so freeing pending send buffers can wake poll waiters.
Signed-off-by Sergio Lopez <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
RFC PATCH v2 3/8 vsock/virtio: implement the datagram data path
In reply to Sergio LopezAdd the datagram feature bit and packet type, and implement the common virtio transport operations needed to bind, send, and receive datagrams.
Send each message as an OP_RW packet with a maximum 64 KiB payload, including support for empty messages. Bound pending sends by the socket send buffer instead of connection-oriented peer credits. Use the socket's skb memory charge for datagram SIOCOUTQ, including empty messages, and release it through the normal skb ownership lifetime on completion, rejection, or queued drops.
Deliver incoming datagrams through the datagram bound-socket lookup without entering the connection state machine. Connection-oriented receive lookup excludes datagram sockets, so each class can use the same destination CID and port. Validate the transport and connected peer under the socket lock, including after shutdown, and silently discard undeliverable messages. Use the socket datagram queue for recvmsg(), including source addresses, peeking, and truncation reporting based on the payload length after socket filtering. Reject MSG_OOB and MSG_ERRQUEUE before accessing the receive queue. Reject datagram sockmap admission until its receive callbacks support it.
--- include/uapi/linux/virtio_vsock.h | 2 + net/vmw_vsock/af_vsock.c | 31 +++++--- net/vmw_vsock/virtio_transport_common.c | 132 ++++++++++++++++++++++++++++---- net/vmw_vsock/vsock_bpf.c | 3 +- 4 files changed, 140 insertions(+), 28 deletions(-)
include/uapi/linux/virtio_vsock.h +2 -0
@@ -40,6 +40,7 @@/* The feature bitmap for virtio vsock */ #define VIRTIO_VSOCK_F_SEQPACKET 1/* SOCK_SEQPACKET supported */ +#define VIRTIO_VSOCK_F_DGRAM 3/* SOCK_DGRAM supported */ struct virtio_vsock_config {__le64 guest_cid;
@@ -69,6 +70,7 @@ struct virtio_vsock_hdr {enum virtio_vsock_type {VIRTIO_VSOCK_TYPE_STREAM =1 ,VIRTIO_VSOCK_TYPE_SEQPACKET =2 , +VIRTIO_VSOCK_TYPE_DGRAM =3 , };enum virtio_vsock_op {
net/vmw_vsock/af_vsock.c +19 -12
@@ -1659,20 +1659,27 @@ static int vsock_do_ioctl(struct socket *sock, unsigned int cmd,case SIOCOUTQ : {ssize_t n_bytes; -if (!vsk->transport || !vsk->transport->unsent_bytes) { - ret = -EOPNOTSUPP ; -break ; - } +if (sk->sk_type ==SOCK_DGRAM ) { +/* Match UDP: report charged send memory, including skb +* overhead and empty datagrams. +*/ + n_bytes =sk_wmem_alloc_get (sk); + }else { +if (!vsk->transport || !vsk->transport->unsent_bytes) { + ret = -EOPNOTSUPP ; +break ; + } -if (sock_type_connectible (sk->sk_type) && sk->sk_state ==TCP_LISTEN ) { - ret = -EINVAL ; -break ; - } +if (sk->sk_state ==TCP_LISTEN ) { + ret = -EINVAL ; +break ; + } - n_bytes = vsk->transport->unsent_bytes (vsk); -if (n_bytes <0 ) { - ret = n_bytes; -break ; + n_bytes = vsk->transport->unsent_bytes (vsk); +if (n_bytes <0 ) { + ret = n_bytes; +break ; + } } ret =put_user (n_bytes, arg);
net/vmw_vsock/virtio_transport_common.c +117 -15
211 lines not on this page · raw message
net/vmw_vsock/vsock_bpf.c +2 -1
@@ -161,7 +161,8 @@ int vsock_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restoreif (!vsk->transport)return -ENODEV ; -if (!vsk->transport->read_skb) +/* Datagram receive is not implemented by the sockmap callbacks. */ +if (sk->sk_type ==SOCK_DGRAM || !vsk->transport->read_skb)return -EOPNOTSUPP ;vsock_bpf_check_needs_rebuild (psock->sk_proto);
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC v2 3/8] vsock/virtio: implement the datagram data path
Sashiko has reviewed this patch and found no issues.
Quoted 22 lines
Add the datagram feature bit and packet type, and implement the common virtio transport operations needed to bind, send, and receive datagrams.
Send each message as an
OP_RWpacket with a maximum 64 KiB payload, including support for empty messages. Bound pending sends by the socket send buffer instead of connection-oriented peer credits. Use the socket's skb memory charge for datagram SIOCOUTQ, including empty messages, and release it through the normal skb ownership lifetime on completion, rejection, or queued drops.Deliver incoming datagrams through the datagram bound-socket lookup without entering the connection state machine. Connection-oriented receive lookup excludes datagram sockets, so each class can use the same destination CID and port. Validate the transport and connected peer under the socket lock, including after shutdown, and silently discard undeliverable messages. Use the socket datagram queue for
recvmsg(), including source addresses, peeking, and truncation reporting based on the payload length after socket filtering. RejectMSG_OOBandMSG_ERRQUEUEbefore accessing the receive queue. Reject datagram sockmap admission until its receive callbacks support it.
Quoted 1 line
Signed-off-by Sergio Lopez <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
RFC PATCH v2 4/8 vsock/virtio: negotiate datagram support on the guest
In reply to Sergio LopezAdvertise VIRTIO_VSOCK_F_DGRAM and record whether the device negotiated it. Gate datagram socket initialization and destination checks on the negotiated feature and the transport's supported network namespace mode.
Perform device capability checks at socket creation without requiring a remote address, which is not yet known for an unconnected datagram socket. The common virtio implementation provides the datagram data path.
Discard received datagrams when the device has not negotiated support, including when a socket was initialized through another transport. Reject packets addressed to a different CID using the full 64-bit wire value before bound-socket lookup narrows it or matches a wildcard.
--- net/vmw_vsock/virtio_transport.c | 50 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport.c +47 -3
@@ -57,6 +57,7 @@ struct virtio_vsock {u32 guest_cid;bool seqpacket_allow; +bool dgram_allow;/* These fields are used only in tx path in function * 'virtio_transport_send_pkt_work()', so to save
@@ -547,6 +548,18 @@ bool virtio_transport_stream_allow(struct vsock_sock *vsk, u32 cid, u32 port)static bool virtio_transport_seqpacket_allow (struct vsock_sock *vsk,u32 remote_cid); +static bool virtio_transport_dgram_allow_check (struct vsock_sock *vsk, +u32 cid,u32 port); + +static int virtio_transport_socket_init (struct vsock_sock *vsk, +struct vsock_sock *psk) +{ +if (sk_vsock (vsk)->sk_type ==SOCK_DGRAM && + !virtio_transport_dgram_allow_check (vsk,0 ,0 )) +return -ESOCKTNOSUPPORT ; + +return virtio_transport_do_socket_init (vsk, psk); +}static bool virtio_transport_has_remote_cid (struct vsock_sock *vsk,u32 cid) {
@@ -561,7 +574,7 @@ static struct virtio_transport virtio_transport = { .get_local_cid = virtio_transport_get_local_cid, .has_remote_cid = virtio_transport_has_remote_cid, - .init = virtio_transport_do_socket_init, + .init = virtio_transport_socket_init, .destruct = virtio_transport_destruct, .release = virtio_transport_release, .connect = virtio_transport_connect,
@@ -571,7 +584,7 @@ static struct virtio_transport virtio_transport = { .dgram_bind = virtio_transport_dgram_bind, .dgram_dequeue = virtio_transport_dgram_dequeue, .dgram_enqueue = virtio_transport_dgram_enqueue, - .dgram_allow = virtio_transport_dgram_allow, + .dgram_allow = virtio_transport_dgram_allow_check, .stream_dequeue = virtio_transport_stream_dequeue, .stream_enqueue = virtio_transport_stream_enqueue,
@@ -629,6 +642,25 @@ virtio_transport_seqpacket_allow(struct vsock_sock *vsk, u32 remote_cid)return seqpacket_allow; } +static bool +virtio_transport_dgram_allow_check (struct vsock_sock *vsk,u32 cid,u32 port) +{ +struct virtio_vsock *vsock; +bool dgram_allow; + +if (!vsock_net_mode_global (vsk)) +return false; + + dgram_allow = false; +rcu_read_lock (); + vsock =rcu_dereference (the_virtio_vsock); +if (vsock) + dgram_allow = vsock->dgram_allow; +rcu_read_unlock (); + +return dgram_allow; +} +static void virtio_transport_rx_work (struct work_struct *work) { struct virtio_vsock *vsock =
@@ -677,6 +709,14 @@ static void virtio_transport_rx_work(struct work_struct *work)continue ; } +/* Check the full wire CID before socket lookup truncates it. */ +if (le16_to_cpu (hdr->type) ==VIRTIO_VSOCK_TYPE_DGRAM && + (!vsock->dgram_allow || +le64_to_cpu (hdr->dst_cid) != vsock->guest_cid)) { +kfree_skb (skb); +continue ; + } +if (payload_len)virtio_vsock_skb_put (skb, payload_len);
@@ -835,6 +875,9 @@ static int virtio_vsock_probe(struct virtio_device *vdev)if (virtio_has_feature (vdev,VIRTIO_VSOCK_F_SEQPACKET )) vsock->seqpacket_allow = true; +if (virtio_has_feature (vdev,VIRTIO_VSOCK_F_DGRAM )) + vsock->dgram_allow = true; + vdev->priv = vsock; ret =virtio_vsock_vqs_init (vsock);
@@ -932,7 +975,8 @@ static struct virtio_device_id id_table[] = { };static unsigned int features[] = { -VIRTIO_VSOCK_F_SEQPACKET +VIRTIO_VSOCK_F_SEQPACKET , +VIRTIO_VSOCK_F_DGRAM , };static struct virtio_driver virtio_vsock_driver = {
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC v2 4/8] vsock/virtio: negotiate datagram support on the guest
Sashiko has reviewed this patch and found no issues.
Quoted 10 lines
Advertise
VIRTIO_VSOCK_F_DGRAMand record whether the device negotiated it. Gate datagram socket initialization and destination checks on the negotiated feature and the transport's supported network namespace mode.Perform device capability checks at socket creation without requiring a remote address, which is not yet known for an unconnected datagram socket. The common virtio implementation provides the datagram data path.
Quoted 6 lines
Discard received datagrams when the device has not negotiated support, including when a socket was initialized through another transport. Reject packets addressed to a different CID using the full 64-bit wire value before bound-socket lookup narrows it or matches a wildcard.
Signed-off-by Sergio Lopez <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
RFC PATCH v2 5/8 vhost/vsock: negotiate datagram support with guests
In reply to Sergio LopezExpose VIRTIO_VSOCK_F_DGRAM in the vhost feature bitmap and retain the negotiated value for each guest. Keep datagrams disabled until the feature is negotiated.
Use a vhost-specific datagram destination check to look up the guest in the socket's network namespace and require its negotiated datagram feature. The common virtio implementation supplies the bind and message operations; destination validation remains in connect() and sendmsg(), where the remote CID is available.
Reject guest datagrams on ingress unless the receiving virtqueue acknowledged the feature. Complete rejected descriptors normally so the queue can continue processing subsequent traffic.
--- drivers/vhost/vsock.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-)
drivers/vhost/vsock.c +30 -4
@@ -32,7 +32,8 @@static const int vhost_vsock_bits[] = {VHOST_FEATURES ,VIRTIO_F_ACCESS_PLATFORM , -VIRTIO_VSOCK_F_SEQPACKET +VIRTIO_VSOCK_F_SEQPACKET , +VIRTIO_VSOCK_F_DGRAM , };#define VHOST_VSOCK_FEATURES VHOST_FEATURES_U64(vhost_vsock_bits, 0)
@@ -62,6 +63,7 @@ struct vhost_vsock {u32 guest_cid;bool seqpacket_allow;bool ever_started;/* set on first SET_RUNNING(1); never cleared */ +bool dgram_allow; };static u32 vhost_transport_get_local_cid (void )
@@ -436,6 +438,8 @@ static bool vhost_transport_msgzerocopy_allow(void)static bool vhost_transport_seqpacket_allow (struct vsock_sock *vsk,u32 remote_cid); +static bool vhost_transport_dgram_allow (struct vsock_sock *vsk, +u32 cid,u32 port);static bool vhost_transport_stream_allow (struct vsock_sock *vsk,u32 cid,u32 port)
@@ -460,7 +464,7 @@ static struct virtio_transport vhost_transport = { .dgram_enqueue = virtio_transport_dgram_enqueue, .dgram_dequeue = virtio_transport_dgram_dequeue, .dgram_bind = virtio_transport_dgram_bind, - .dgram_allow = virtio_transport_dgram_allow, + .dgram_allow = vhost_transport_dgram_allow, .stream_enqueue = virtio_transport_stream_enqueue, .stream_dequeue = virtio_transport_stream_dequeue,
@@ -516,6 +520,24 @@ static bool vhost_transport_seqpacket_allow(struct vsock_sock *vsk,return seqpacket_allow; } +static bool vhost_transport_dgram_allow (struct vsock_sock *vsk, +u32 cid,u32 port) +{ +struct net *net =sock_net (sk_vsock (vsk)); +struct vhost_vsock *vsock; +bool dgram_allow = false; + +rcu_read_lock (); + vsock =vhost_vsock_get (cid, net); + +if (vsock) + dgram_allow = vsock->dgram_allow; + +rcu_read_unlock (); + +return dgram_allow; +} +static void vhost_vsock_handle_tx_kick (struct vhost_work *work) { struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
@@ -573,10 +595,12 @@ static void vhost_vsock_handle_tx_kick(struct vhost_work *work) hdr =virtio_vsock_hdr (skb); -/* Only accept correctly addressed packets */ +/* Check addressing and datagram feature negotiation. */ if (le64_to_cpu (hdr->src_cid) == vsock->guest_cid &&le64_to_cpu (hdr->dst_cid) == -vhost_transport_get_local_cid ()) +vhost_transport_get_local_cid () && + (le16_to_cpu (hdr->type) !=VIRTIO_VSOCK_TYPE_DGRAM || +vhost_has_feature (vq,VIRTIO_VSOCK_F_DGRAM )))virtio_transport_recv_pkt (&vhost_transport, skb, vsock->net);else
@@ -737,6 +761,7 @@ static int vhost_vsock_dev_open(struct inode *inode, struct file *file) vsock->guest_cid =0 ;/* no CID assigned yet */ vsock->seqpacket_allow = false; vsock->ever_started = false; + vsock->dgram_allow = false;atomic_set (&vsock->queued_replies,0 );
@@ -890,6 +915,7 @@ static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features) } vsock->seqpacket_allow = features & (1ULL <<VIRTIO_VSOCK_F_SEQPACKET ); + vsock->dgram_allow = features & (1ULL <<VIRTIO_VSOCK_F_DGRAM );for (i =0 ; i <ARRAY_SIZE (vsock->vqs); i++) { vq = &vsock->vqs[i];
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC v2 5/8] vhost/vsock: negotiate datagram support with guests
Sashiko has reviewed this patch and found no issues.
Quoted 10 lines
Expose
VIRTIO_VSOCK_F_DGRAMin the vhost feature bitmap and retain the negotiated value for each guest. Keep datagrams disabled until the feature is negotiated.Use a vhost-specific datagram destination check to look up the guest in the socket's network namespace and require its negotiated datagram feature. The common virtio implementation supplies the bind and message operations; destination validation remains in
connect()andsendmsg(), where the remote CID is available.
Quoted 5 lines
Reject guest datagrams on ingress unless the receiving virtqueue acknowledged the feature. Complete rejected descriptors normally so the queue can continue processing subsequent traffic.
Signed-off-by Sergio Lopez <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
RFC PATCH v2 6/8 vsock: allow datagrams to use directional transports
In reply to Sergio LopezUse the datagram fallback transport to own sockets that need transport- specific bind state. VMCI needs this ownership to create and destroy its receive handle. Without a fallback, initialize socket state through a directional transport or loopback; use loopback if a directional transport does not support datagrams. Hyper-V still rejects datagrams.
Share local, guest-to-host, host-to-guest, and parent-fallback route selection with stream and seqpacket sockets. Choose a capable directional transport for each datagram destination and use VMCI when none can serve the peer. Keep VMCI as the socket owner across connect and send calls so changing destinations cannot destroy its receive handle. Preserve parent routing in returned addresses and when connecting through the guest-to-host fallback. A host CID remains a loopback alias when the guest transport has no active device.
Reserve VMCI-bound ports in the core table as well as in VMCI so virtio and vhost can deliver to the same socket. Undo the core bind if VMCI handle creation fails, and retry ephemeral ports that collide with VMCI resources. Queue only datagram payloads with common source metadata in both receive paths. Read the shared queue directly in core recvmsg() and report payload size through SIOCINQ. Preserve the virtio TO_HOST source flag, the VMCI source CID and port, peeking, truncation, and socket filtering.
Allow virtio datagram sends from a VMCI-owned socket without interpreting VMCI private state as virtio state. The skb's socket ownership tracks send memory regardless of which transport owns the socket. Per-send capability checks still reject host traffic when virtio datagrams were not negotiated.
Check connected sources against the peer's selected route and exact CID and port, including on loopback. Wildcard loopback senders use the addressed local CID; explicit bindings retain their own CID. Reject loopback source bindings whose reply address selects another transport. Use a copy of the peer address during connected sends so a temporary parent fallback does not pin later sends to the host. Remove sockets from the core tables when transport initialization fails.
Paired stream, seqpacket, and datagram tests pass with virtio/vhost. Datagram tests also pass with a temporary VMCI-like fallback owner. The fallback tests cover mixed send and receive, VMCI-style bind rollback, and an unclaimed destination. A child-disappearance reproducer confirms parent fallback and direct-route recovery. VMCI and Hyper-V objects build; real VMCI hardware has not been exercised.
--- Documentation/admin-guide/sysctl/net.rst | 8 +- drivers/vhost/vsock.c | 2 +- include/linux/virtio_vsock.h | 9 +- include/net/af_vsock.h | 29 ++- net/vmw_vsock/af_vsock.c | 306 ++++++++++++++++++++++++++----- net/vmw_vsock/hyperv_transport.c | 11 +- net/vmw_vsock/virtio_transport.c | 2 +- net/vmw_vsock/virtio_transport_common.c | 113 ++++++------ net/vmw_vsock/vmci_transport.c | 124 ++++--------- net/vmw_vsock/vsock_loopback.c | 2 +- 10 files changed, 390 insertions(+), 216 deletions(-)
Documentation/admin-guide/sysctl/net.rst +5 -3
@@ -586,9 +586,11 @@ reachable through the hypervisor (e.g. Nitro Enclaves) to address both using a single CID space, without requiring applications to set ``VMADDR_FLAG_TO_HOST``. -When the fallback is taken, ``VMADDR_FLAG_TO_HOST`` is automatically set on -the remote address so that userspace can determine the path via -``getpeername()``. +When a connect falls back, ``VMADDR_FLAG_TO_HOST`` is automatically set on +the connected peer address so that userspace can determine the selected path +via ``getpeername()``. A datagram send on an existing connection can also fall +back; in that case the flag is set only on the per-send destination. Later +sends can use H2G again if it takes ownership of the destination CID. Note: With this sysctl enabled, user space that attempts to talk to a guest CID which is not implemented by the H2G transport will create host vsock
drivers/vhost/vsock.c +1 -1
@@ -462,9 +462,9 @@ static struct virtio_transport vhost_transport = { .cancel_pkt = vhost_transport_cancel_pkt, .dgram_enqueue = virtio_transport_dgram_enqueue, - .dgram_dequeue = virtio_transport_dgram_dequeue, .dgram_bind = virtio_transport_dgram_bind, .dgram_allow = vhost_transport_dgram_allow, + .dgram_transport_match = virtio_transport_dgram_transport_match, .stream_enqueue = virtio_transport_stream_enqueue, .stream_dequeue = virtio_transport_stream_dequeue,
include/linux/virtio_vsock.h +4 -5
@@ -12,6 +12,7 @@struct virtio_vsock_skb_cb {bool reply;bool tap_delivered; +bool to_host;u32 offset; };
@@ -171,6 +172,7 @@ struct virtio_vsock_sock {struct virtio_vsock_pkt_info {u32 remote_cid, remote_port; +const struct virtio_transport *t_ops;struct vsock_sock *vsk;struct msghdr *msg;struct net *net;
@@ -204,11 +206,6 @@ virtio_transport_stream_dequeue(struct vsock_sock *vsk,size_t len, int type);int -virtio_transport_dgram_dequeue (struct vsock_sock *vsk, -struct msghdr *msg, -size_t len,int flags); - -int virtio_transport_seqpacket_enqueue (struct vsock_sock *vsk,struct msghdr *msg,size_t len);
@@ -261,6 +258,7 @@ 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 (struct vsock_sock *vsk,u32 cid,u32 port); +bool virtio_transport_dgram_transport_match (const struct vsock_transport *t);int virtio_transport_connect (struct vsock_sock *vsk);
@@ -274,6 +272,7 @@ virtio_transport_stream_enqueue(struct vsock_sock *vsk, size_t len);int virtio_transport_dgram_enqueue (struct vsock_sock *vsk, +const struct vsock_transport *transport,struct sockaddr_vm *remote_addr,struct msghdr *msg,size_t len);
include/net/af_vsock.h +21 -8
@@ -26,6 +26,13 @@ extern spinlock_t vsock_table_lock;#define vsock_sk (__sk) ((struct vsock_sock *)__sk)#define sk_vsock (__vsk) (&(__vsk)->sk) +/* Datagrams from different transports share the socket receive queue. */ +struct vsock_dgram_skb_cb { +struct sockaddr_vm src; +}; + +#define VSOCK_DGRAM_SKB_CB (skb) ((struct vsock_dgram_skb_cb *)((skb)->cb)) +struct vsock_sock {/* sk must be the first member. */ struct sock sk;
@@ -98,8 +105,8 @@ struct vsock_transport_send_notify_data {#define VSOCK_TRANSPORT_F_H2G 0x00000001/* Transport provides guest->host communication */ #define VSOCK_TRANSPORT_F_G2H 0x00000002 -/* Transport provides DGRAM communication */ -#define VSOCK_TRANSPORT_F_DGRAM 0x00000004 +/* Transport provides fallback DGRAM communication and owns its endpoint */ +#define VSOCK_TRANSPORT_F_DGRAM_FALLBACK 0x00000004/* Transport provides local (loopback) communication */ #define VSOCK_TRANSPORT_F_LOCAL 0x00000008
@@ -119,13 +126,15 @@ struct vsock_transport {/* DGRAM. */ int (*dgram_bind)(struct vsock_sock *,struct sockaddr_vm *); -int (*dgram_dequeue)(struct vsock_sock *vsk,struct msghdr *msg, -size_t len,int flags); -int (*dgram_enqueue)(struct vsock_sock *,struct sockaddr_vm *, -struct msghdr *,size_t len); +int (*dgram_enqueue)(struct vsock_sock *vsk, +const struct vsock_transport *transport, +struct sockaddr_vm *remote_addr, +struct msghdr *msg,size_t len);bool (*dgram_allow)(struct vsock_sock *vsk,u32 cid,u32 port); -/* Optional payload length override, called with the receive queue locked. */ -size_t (*dgram_payload_len)(const struct sk_buff *skb); +/* A fallback transport can share its datagram sockets with routed ones. */ +bool dgram_fallback; +/* Optional: another transport can use this socket's datagram state. */ +bool (*dgram_transport_match)(const struct vsock_transport *transport);/* STREAM. */ /* TODO: stream_bind() */
@@ -223,6 +232,7 @@ void vsock_enqueue_accept(struct sock *listener, struct sock *connected);void vsock_pending_to_accept (struct sock *listener,struct sock *pending);void vsock_insert_connected (struct vsock_sock *vsk);int vsock_bind_common (struct vsock_sock *vsk,struct sockaddr_vm *addr); +void vsock_unbind_common (struct vsock_sock *vsk);void vsock_remove_bound (struct vsock_sock *vsk);void vsock_remove_connected (struct vsock_sock *vsk);struct sock *vsock_find_bound_socket (struct sockaddr_vm *addr);
@@ -242,6 +252,9 @@ void vsock_remove_sock(struct vsock_sock *vsk);void vsock_for_each_connected_socket (struct vsock_transport *transport,void (*fn)(struct sock *sk));int vsock_assign_transport (struct vsock_sock *vsk,struct vsock_sock *psk); +const struct vsock_transport * +vsock_dgram_get_transport (struct vsock_sock *vsk, +struct sockaddr_vm *remote_addr);bool vsock_find_cid (unsigned int cid);void vsock_linger (struct sock *sk);
net/vmw_vsock/af_vsock.c +257 -49
466 lines not on this page · raw message
net/vmw_vsock/hyperv_transport.c +4 -7
@@ -443,6 +443,9 @@ static int hvs_sock_init(struct vsock_sock *vsk, struct vsock_sock *psk)struct hvsock *hvs;struct sock *sk =sk_vsock (vsk); +if (sk->sk_type ==SOCK_DGRAM ) +return -ESOCKTNOSUPPORT ; + hvs =kzalloc_obj (*hvs);if (!hvs)return -ENOMEM ;
@@ -556,13 +559,8 @@ static int hvs_dgram_bind(struct vsock_sock *vsk, struct sockaddr_vm *addr)return -EOPNOTSUPP ; } -static int hvs_dgram_dequeue (struct vsock_sock *vsk,struct msghdr *msg, -size_t len,int flags) -{ -return -EOPNOTSUPP ; -} -static int hvs_dgram_enqueue (struct vsock_sock *vsk, +const struct vsock_transport *transport,struct sockaddr_vm *remote,struct msghdr *msg,size_t dgram_len) {
@@ -871,7 +869,6 @@ static struct vsock_transport hvs_transport = { .shutdown = hvs_shutdown, .dgram_bind = hvs_dgram_bind, - .dgram_dequeue = hvs_dgram_dequeue, .dgram_enqueue = hvs_dgram_enqueue, .dgram_allow = hvs_dgram_allow,
net/vmw_vsock/virtio_transport.c +1 -1
@@ -582,9 +582,9 @@ static struct virtio_transport virtio_transport = { .cancel_pkt = virtio_transport_cancel_pkt, .dgram_bind = virtio_transport_dgram_bind, - .dgram_dequeue = virtio_transport_dgram_dequeue, .dgram_enqueue = virtio_transport_dgram_enqueue, .dgram_allow = virtio_transport_dgram_allow_check, + .dgram_transport_match = virtio_transport_dgram_transport_match, .stream_dequeue = virtio_transport_stream_dequeue, .stream_enqueue = virtio_transport_stream_enqueue,
net/vmw_vsock/virtio_transport_common.c +58 -55
172 lines not on this page · raw message
net/vmw_vsock/vmci_transport.c +38 -86
179 lines not on this page · raw message
net/vmw_vsock/vsock_loopback.c +1 -1
@@ -74,9 +74,9 @@ static struct virtio_transport loopback_transport = { .cancel_pkt = vsock_loopback_cancel_pkt, .dgram_bind = virtio_transport_dgram_bind, - .dgram_dequeue = virtio_transport_dgram_dequeue, .dgram_enqueue = virtio_transport_dgram_enqueue, .dgram_allow = virtio_transport_dgram_allow, + .dgram_transport_match = virtio_transport_dgram_transport_match, .stream_dequeue = virtio_transport_stream_dequeue, .stream_enqueue = virtio_transport_stream_enqueue,
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC v2 6/8] vsock: allow datagrams to use directional transports
Thank you for your contribution! Sashiko AI review found 8 potential issues to consider: - [High] vsock: Hyper-V stream connections broken in vsock_use_local_transport() - [High] vsock…
Thank you for your contribution! Sashiko AI review found 8 potential issues to consider:
- [High] vsock: Hyper-V stream connections broken in vsock_use_local_transport()
- [High] vsock: vsock_select_transport() routes VMADDR_CID_HOST datagrams to VMCI
- [High] vsock: explicit CID bind failure in vmci_transport_dgram_bind()
- [High] vsock/vmci: local netns isolation bypass in vmci_transport_dgram_bind()
- [Medium] vsock: broken VMADDR_CID_ANY handling in vsock_dgram_connect() and sendmsg()
- [Medium] vsock: bind rollback TOCTOU race in virtio_transport_recv_pkt()
- [Medium] vsock/virtio: virtio_transport_recv_pkt() sets TO_HOST on host datagrams
- [Low] vsock/virtio: unused to_host field in struct virtio_vsock_skb_cb --
commit 99283d4e6bf0b4f58509834e3f59987834c1907c
Author: Sergio Lopez <[email protected]>
vsock: allow datagrams to use directional transports
This commit routes AF_VSOCK datagrams across directional transports, loopback, and the VMCI fallback transport while unifying datagram receive queuing in the core socket layer.
include/linux/virtio_vsock.h line 17
Quoted 11 lines
include/linux/virtio_vsock.h +1
-0@@ -12,6 +12,7 @@struct virtio_vsock_skb_cb {bool reply;bool tap_delivered; +bool to_host;u32 offset; };
[Severity: Low]
This isn't a bug, but struct virtio_vsock_skb_cb::to_host is never read or written anywhere in the patch (receive-side VMADDR_FLAG_TO_HOST is stored in VSOCK_DGRAM_SKB_CB(skb)->src.svm_flags instead).
Quoted 4 lines
net/vmw_vsock/af_vsock.c +0
-0
[ ... ]
Quoted 15 lines
net/vmw_vsock/af_vsock.c +2
-2@@ -568,6 +569,8 @@ EXPORT_SYMBOL_GPL(vsock_enqueue_accept);static bool vsock_use_local_transport (unsigned int remote_cid) { +u32 local_cid; +lockdep_assert_held (&vsock_register_mutex);if (!transport_local)@@ -576,11 +579,50 @@ static bool vsock_use_local_transport(unsigned int remote_cid)if (remote_cid ==VMADDR_CID_LOCAL )return true; -if (transport_g2h) { -return remote_cid == transport_g2h->get_local_cid ();
net/vmw_vsock/af_vsock.c static bool vsock_use_local_transport(unsigned int remote_cid) · line 588
Quoted 10 lines
net/vmw_vsock/af_vsock.c +7
-3- }else { -return remote_cid ==VMADDR_CID_HOST ; - } + local_cid = transport_g2h ? transport_g2h->get_local_cid () : +VMADDR_CID_ANY ; +if (local_cid ==VMADDR_CID_ANY ) + local_cid =VMADDR_CID_HOST ; + +return remote_cid == local_cid; +}
[Severity: High]
Does replacing VMADDR_CID_ANY with VMADDR_CID_HOST here break Hyper-V SOCK_STREAM connections when vsock_loopback is loaded?
hvs_transport registers as transport_g2h and its get_local_cid() callback (hvs_get_local_cid()) always returns VMADDR_CID_ANY because Hyper-V guests do not use a numeric guest CID. With this change, vsock_use_local_transport(VMADDR_CID_HOST) returns true whenever transport_local is registered, so vsock_select_transport() routes outgoing guest-to-host connect() calls to loopback_transport instead of hvs_transport, and incoming host connections in hvs_open_connection() (which sets vnew->remote_addr.svm_cid = VMADDR_CID_HOST) fail the !hvs_check_transport(vnew) check and are dropped.
Quoted 15 lines
net/vmw_vsock/af_vsock.c +15
-0+ +/* Select a route without changing socket ownership. The caller holds +* vsock_register_mutex and pins the returned transport before releasing it. +*/ +static const struct vsock_transport * +vsock_select_transport (struct vsock_sock *vsk, +const struct sockaddr_vm *remote_addr, +const struct vsock_transport *dgram_owner, +bool *to_host) +{ +struct sock *sk =sk_vsock (vsk); +u32 cid = remote_addr->svm_cid; + +lockdep_assert_held (&vsock_register_mutex); + *to_host = false;
net/vmw_vsock/af_vsock.c static bool vsock_use_local_transport(unsigned int remote_cid) · line 613
Quoted 10 lines
net/vmw_vsock/af_vsock.c +10
-0+ +if (vsock_use_local_transport (cid)) +return transport_local; +/* VMCI may use a different local CID from the virtio transports. */ +if (dgram_owner && dgram_owner->dgram_fallback && + cid == dgram_owner->get_local_cid ()) +return dgram_owner; +if (cid <=VMADDR_CID_HOST || + (remote_addr->svm_flags &VMADDR_FLAG_TO_HOST )) +return transport_g2h;
[Severity: High]
On a virtio guest with vmw_vsock_vmci_transport loaded, doesn't checking cid == dgram_owner->get_local_cid() before cid <= VMADDR_CID_HOST misroute VMADDR_CID_HOST datagrams to VMCI instead of virtio_transport?
drivers/misc/vmw_vmci/vmci_driver.c:vmci_get_context_id() {
if (vmci_guest_code_active())
return vmci_get_vm_context_id();
else if (vmci_host_code_active())
return VMCI_HOST_CONTEXT_ID;
return VMCI_INVALID_ID;
}Because vmci_drv_init() initializes the host device unconditionally on module load, vmci_host_code_active() is true on any guest without a VMware PCI device, so vmci_transport_get_local_cid() returns VMCI_HOST_CONTEXT_ID (2, which equals VMADDR_CID_HOST). When a guest SOCK_DGRAM socket sends or connects to VMADDR_CID_HOST, cid == dgram_owner->get_local_cid() matches vmci_transport before reaching cid <= VMADDR_CID_HOST, causing sends to fail in vmci_datagram_send() with -EHOSTUNREACH and connected sockets to reject incoming virtio datagrams from the host in virtio_transport_dgram_source_allow().
[ ... ]
Quoted 13 lines
net/vmw_vsock/af_vsock.c +2
-3@@ -629,33 +673,28 @@ int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk) vsk->remote_addr.svm_cid > VMADDR_CID_HOST) vsk->remote_addr.svm_flags |= VMADDR_FLAG_TO_HOST; - remote_flags = vsk->remote_addr.svm_flags; -mutex_lock (&vsock_register_mutex);switch (sk->sk_type) {case SOCK_DGRAM : - new_transport = transport_dgram; +/* VMCI must own bind() to create its receive handle, even when +* directional transports serve individual destinations.
net/vmw_vsock/af_vsock.c int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk) · line 691
Quoted 10 lines
net/vmw_vsock/af_vsock.c +9
-0+*/ +if (transport_dgram_fallback) + new_transport = transport_dgram_fallback; +else if (transport_g2h) + new_transport = transport_g2h; +else if (transport_h2g) + new_transport = transport_h2g; +else + new_transport = transport_local;break ;
[Severity: High]
Should vsock_assign_transport() (or vmci_transport_dgram_bind()) avoid allocating a global VMCI datagram handle when the socket's netns is in VSOCK_NET_MODE_LOCAL?
Assigning transport_dgram_fallback to SOCK_DGRAM sockets in a VSOCK_NET_MODE_LOCAL namespace causes vmci_transport_dgram_bind() to call vmci_transport_datagram_create_hnd(), which reserves the port in the system-wide vmci_resource_table and registers vmci_transport_recv_dgram_cb(). Because vmci_resource_table is global and vmci_transport_recv_dgram_cb() does not check vsock_net_mode_global(vsk), sockets in local-mode namespaces collide on ports with init_net and can receive global VMCI datagrams across the namespace isolation boundary.
[ ... ]
Quoted 66 lines
net/vmw_vsock/af_vsock.c +16
-10@@ -1498,7 +1671,8 @@ static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg,*/ if (remote_addr->svm_cid ==VMADDR_CID_ANY ) - remote_addr->svm_cid = transport->get_local_cid (); + remote_addr->svm_cid = transport->dgram_transport_match ? +VMADDR_CID_LOCAL : transport->get_local_cid ();if (!vsock_addr_bound (remote_addr)) { err = -EINVAL ;@@ -1508,27 +1682,32 @@ static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg, remote_addr = &vsk->remote_addr;if (remote_addr->svm_cid ==VMADDR_CID_ANY ) - remote_addr->svm_cid = transport->get_local_cid (); + remote_addr->svm_cid = transport->dgram_transport_match ? +VMADDR_CID_LOCAL : transport->get_local_cid ();/* XXX Should connect() or this function ensure remote_addr is * bound? */ -if (!vsock_addr_bound (&vsk->remote_addr)) { +if (!vsock_addr_bound (remote_addr)) { err = -EINVAL ; goto out; } +/* A fallback must not pin subsequent sends to the host. */ + connected_addr = *remote_addr; + remote_addr = &connected_addr; }else { err = -EINVAL ; goto out; } -if (!transport->dgram_allow (vsk, remote_addr->svm_cid, - remote_addr->svm_port)) { - err = -EINVAL ; + transport =vsock_dgram_get_transport (vsk, remote_addr); +if (IS_ERR (transport)) { + err =PTR_ERR (transport); goto out; } - err = transport->dgram_enqueue (vsk, remote_addr, msg, len); + err = transport->dgram_enqueue (vsk, transport, remote_addr, msg, len); +module_put (transport->module); out:release_sock (sk);@@ -1542,6 +1721,7 @@ static int vsock_dgram_connect(struct socket *sock,struct sock *sk;struct vsock_sock *vsk;struct sockaddr_vm *remote_addr; +const struct vsock_transport *transport; sk = sock->sk; vsk =vsock_sk (sk);@@ -1563,11 +1743,16 @@ static int vsock_dgram_connect(struct socket *sock,if (err) goto out; -if (!vsk->transport->dgram_allow (vsk, remote_addr->svm_cid, - remote_addr->svm_port)) { - err = -EINVAL ; +if (remote_addr->svm_cid ==VMADDR_CID_ANY && + vsk->transport->dgram_transport_match)
net/vmw_vsock/af_vsock.c static int vsock_dgram_connect(struct socket *sock, · line 1757
Quoted 10 lines
net/vmw_vsock/af_vsock.c +6
-0+ remote_addr->svm_cid =VMADDR_CID_LOCAL ; + + transport =vsock_dgram_get_transport (vsk, remote_addr); +if (IS_ERR (transport)) { + err =PTR_ERR (transport); goto out; } +module_put (transport->module);memcpy (&vsk->remote_addr, remote_addr,sizeof (vsk->remote_addr));
[Severity: Medium]
What happens in vsock_dgram_connect() and vsock_dgram_sendmsg() when remote_addr->svm_cid is VMADDR_CID_ANY and vmci_transport owns the socket (so dgram_transport_match is NULL)?
In vsock_dgram_connect(), remote_addr->svm_cid is left as VMADDR_CID_ANY (0xffffffffU) and passed to vsock_dgram_get_transport(). Because 0xffffffffU > VMADDR_CID_HOST, vsock_select_transport() takes the g2h_fallback path when enabled and sets VMADDR_FLAG_TO_HOST on vsk->remote_addr, while leaving vsk->remote_addr.svm_cid as VMADDR_CID_ANY so virtio_transport_dgram_source_allow() drops all incoming packets until sendmsg() is called.
In vsock_dgram_sendmsg(), the SS_CONNECTED path then mutates vsk->remote_addr.svm_cid in place before copying it into connected_addr. Moreover, if VMCI has no active personality, transport->get_local_cid() returns VMCI_INVALID_ID (VMADDR_CID_ANY), causing vsock_addr_bound() in vsock_dgram_sendmsg() to fail with -EINVAL and vsock_select_transport() in vsock_dgram_connect() to match cid == dgram_owner->get_local_cid() instead of loopback.
Quoted 4 lines
net/vmw_vsock/virtio_transport_common.c +0
-0
[ ... ]
net/vmw_vsock/virtio_transport_common.c void virtio_transport_recv_pkt(struct virtio_transport *t, · line 1894
Quoted 12 lines
net/vmw_vsock/virtio_transport_common.c +1
-3@@ -1891,14 +1887,21 @@ void virtio_transport_recv_pkt(struct virtio_transport *t, vsk =vsock_sk (sk);lock_sock (sk);if (sock_flag (sk,SOCK_DEAD ) || - vsk->transport != &t->transport || - (sk->sk_socket->state !=SS_UNCONNECTED && - !vsock_check_source (vsk, &t->transport, &src))) { + !virtio_transport_dgram_source_allow (vsk, &t->transport, &src)) {release_sock (sk);sock_put (sk); goto free_pkt; }
[Severity: Medium]
Can virtio_transport_recv_pkt() deliver a datagram to an unbound socket or the wrong port if vmci_transport_dgram_bind() rolls back the core bind?
CPU0 CPU1__vsock_bind() virtio_transport_recv_pkt()
lock_sock(sk)
vmci_transport_dgram_bind()
vsock_bind_common(vsk, addr)
sk = vsock_find_bound_dgram_socket_net(&dst, ...)
lock_sock(sk) (blocks)
vmci_transport_datagram_create_hnd()
(fails)
vsock_unbind_common(vsk) release_sock(sk)
(acquires lock_sock) virtio_transport_dgram_source_allow() sock_queue_rcv_skb(sk, skb) <-- queued on unbound socket
vsock_bind_common() publishes vsk in vsock_bind_table and drops vsock_table_lock before vmci_transport_datagram_create_hnd() runs. If handle creation fails, vsock_unbind_common() removes vsk or retries on another port, but virtio_transport_recv_pkt() does not re-check that vsk->local_addr still matches dst after acquiring lock_sock(sk).
net/vmw_vsock/virtio_transport_common.c void virtio_transport_recv_pkt(struct virtio_transport *t, · line 1904
Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +10
-0+ +/* Loopback can hand the TX skb to its receiver. Release TX +* ownership before replacing its private control block. +*/ +skb_orphan (skb); +vsock_addr_init (&VSOCK_DGRAM_SKB_CB (skb)->src, + src.svm_cid, src.svm_port); +if (t->transport.get_local_cid () >VMADDR_CID_HOST ) +VSOCK_DGRAM_SKB_CB (skb)->src.svm_flags = +VMADDR_FLAG_TO_HOST ;
[Severity: Medium]
Should this also check src.svm_cid > VMADDR_CID_HOST before setting VMADDR_FLAG_TO_HOST, matching vsock_assign_transport()?
On a guest using virtio_transport, t->transport.get_local_cid() is the guest CID (> VMADDR_CID_HOST), so checking only get_local_cid() sets VMADDR_FLAG_TO_HOST on every received datagram even when the sender is the host itself (src.svm_cid == VMADDR_CID_HOST). recvmsg() then reports VMADDR_FLAG_TO_HOST in msg_name for direct host-to-guest traffic, whereas getpeername() on a socket connected to VMADDR_CID_HOST reports svm_flags == 0.
Quoted 4 lines
net/vmw_vsock/vmci_transport.c +0
-0
[ ... ]
Quoted 39 lines
net/vmw_vsock/vmci_transport.c +8
-17@@ -1690,39 +1693,49 @@ static void vmci_transport_release(struct vsock_sock *vsk) } } +#define VMCI_DGRAM_BIND_RETRIES 24 +static int vmci_transport_dgram_bind (struct vsock_sock *vsk,struct sockaddr_vm *addr) { +const bool auto_port = addr->svm_port ==VMADDR_PORT_ANY ;u32 port;u32 flags; +unsigned int i;int err; -/* VMCI will select a resource ID for us if we provide -* VMCI_INVALID_ID. -*/ - port = addr->svm_port ==VMADDR_PORT_ANY ? -VMCI_INVALID_ID : addr->svm_port; - -if (port <=LAST_RESERVED_PORT && !capable (CAP_NET_BIND_SERVICE )) -return -EACCES ; - flags = addr->svm_cid ==VMADDR_CID_ANY ?VMCI_FLAG_ANYCID_DG_HND :0 ; - err =vmci_transport_datagram_create_hnd (port, flags, - vmci_transport_recv_dgram_cb, - &vsk->sk, - &vmci_trans (vsk)->dg_handle); -if (err <VMCI_SUCCESS ) -return vmci_transport_error_to_vsock_error (err); -vsock_addr_init (&vsk->local_addr, addr->svm_cid, -vmci_trans (vsk)->dg_handle.resource); +/* Reserve the same port for VMCI and directional receive. A VMCI +* resource may already occupy a core-selected ephemeral port. +*/ +for (i =0 ; i <VMCI_DGRAM_BIND_RETRIES ; i++) {
net/vmw_vsock/vmci_transport.c static void vmci_transport_release(struct vsock_sock *vsk) · line 1722
Quoted 10 lines
net/vmw_vsock/vmci_transport.c +8
-1+ err =vsock_bind_common (vsk, addr); +if (err) +return err; -return 0 ; + port = vsk->local_addr.svm_port; + err =vmci_transport_datagram_create_hnd (port, flags, + vmci_transport_recv_dgram_cb, + &vsk->sk, + &vmci_trans (vsk)->dg_handle);
[Severity: High]
What happens here when a user binds a SOCK_DGRAM socket to an explicit local CID (such as VMADDR_CID_LOCAL or a virtio guest CID) while vmw_vsock_vmci_transport is loaded?
drivers/misc/vmw_vmci/vmci_datagram.c:dg_create_handle() {
...
if ((flags & VMCI_FLAG_ANYCID_DG_HND) != 0) {
context_id = VMCI_INVALID_ID;
} else {
context_id = vmci_get_context_id();
if (context_id == VMCI_INVALID_ID)
return VMCI_ERROR_NO_RESOURCES;
}
...
}Because addr->svm_cid != VMADDR_CID_ANY, flags is 0. If VMCI has no active guest or host personality (vmci_get_context_id() == VMCI_INVALID_ID), dg_create_handle() returns VMCI_ERROR_NO_RESOURCES and every explicit-CID bind() fails with -ENOBUFS. If VMCI is active, dg_create_handle() binds the VMCI handle to vmci_get_context_id() rather than addr->svm_cid, so a socket bound to VMADDR_CID_LOCAL (or a virtio guest CID) still registers a VMCI receiver on VMCI's context ID and can receive external VMCI datagrams.
Conversely, vsock_find_cid() only checks transport_g2h, transport_h2g, and transport_local, so if VMCI's local CID differs from transport_g2h, __vsock_bind() rejects binding to VMCI's own local CID with -EADDRNOTAVAIL.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
RFC PATCH v2 7/8 vsock: fragment datagrams across receive buffers
In reply to Sergio LopezThe guest already uses page-sized RX allocations. A datagram can exceed one RX buffer. Growing every posted buffer to hold a full datagram would increase virtqueue memory use and make small queued datagrams retain large backing allocations. With delayed reads, a bounded receive queue could then fill after only a few messages.
Define START and EOM flags for datagram packets. Mark outgoing messages with both flags, and let vhost split a message across guest RX buffers, keeping START on the first fragment and EOM on the last. When a data packet meets a descriptor with no payload space, vhost returns that descriptor with zero used bytes. It drops datagrams but requeues stream and seqpacket data for a later buffer.
Reassemble consecutive fragments in the guest before tap or socket delivery. Bound a partial message to 64 fragments and 64 KiB. Discard incomplete messages on malformed packets, a new START, a peer mismatch, an intervening non-datagram packet, or virtqueue teardown. Require complete framing for host and loopback ingress.
A QEMU reproducer now delivers a 9 KiB datagram as one message and all 16 messages of a delayed-read burst. Paired host/guest selftests cover 64 KiB messages and the burst through both a userspace backend and the patched Linux vhost backend, with stream and seqpacket controls. A nested vhost-vsock guest posting header-only RX buffers also passes stream and seqpacket receive tests.
--- drivers/vhost/vsock.c | 15 +++++ include/uapi/linux/virtio_vsock.h | 8 +++ net/vmw_vsock/virtio_transport.c | 115 ++++++++++++++++++++++++++++++-- net/vmw_vsock/virtio_transport_common.c | 11 ++- 4 files changed, 141 insertions(+), 8 deletions(-)
drivers/vhost/vsock.c +15 -0
@@ -185,6 +185,19 @@ vhost_transport_do_send_pkt(struct vhost_vsock *vsock,* buffer, we split it using multiple buffers. */ if (payload_len > iov_len -sizeof (*hdr)) { +if (iov_len ==sizeof (*hdr)) { +/* Connected sockets must retry with the next buffer. */ +if (le16_to_cpu (hdr->type) == +VIRTIO_VSOCK_TYPE_DGRAM ) +kfree_skb (skb); +else +virtio_vsock_skb_queue_head ( + &vsock->send_pkt_queue, skb); +vhost_add_used (vq, head,0 ); + added = true; +continue ; + } + payload_len = iov_len -sizeof (*hdr);/* As we are copying pieces of large packet's buffer to
@@ -244,6 +257,8 @@ vhost_transport_do_send_pkt(struct vhost_vsock *vsock,*/ if (VIRTIO_VSOCK_SKB_CB (skb)->offset < skb->len) { hdr->flags |=cpu_to_le32 (flags_to_restore); +if (le16_to_cpu (hdr->type) ==VIRTIO_VSOCK_TYPE_DGRAM ) + hdr->flags &= ~cpu_to_le32 (VIRTIO_VSOCK_DGRAM_START );/* We are queueing the same skb to handle * the remaining bytes, and we want to deliver it
include/uapi/linux/virtio_vsock.h +8 -0
@@ -101,6 +101,14 @@ enum virtio_vsock_shutdown {enum virtio_vsock_rw {VIRTIO_VSOCK_SEQ_EOM =1 ,VIRTIO_VSOCK_SEQ_EOR =2 , +/* +* With VIRTIO_VSOCK_F_DGRAM, START marks the first RW packet of a +* datagram and EOM marks the last. Both are set for a datagram that +* fits in one packet. Fragments must be consecutive on the receive +* virtqueue. Each packet's len is its fragment length. +*/ +VIRTIO_VSOCK_DGRAM_EOM =VIRTIO_VSOCK_SEQ_EOM , +VIRTIO_VSOCK_DGRAM_START =4 , };#endif /* _UAPI_LINUX_VIRTIO_VSOCK_H */
net/vmw_vsock/virtio_transport.c +108 -7
180 lines not on this page · raw message
net/vmw_vsock/virtio_transport_common.c +10 -1
@@ -275,6 +275,13 @@ static struct sk_buff *virtio_transport_alloc_skb(struct virtio_vsock_pkt_info * hdr->flags |=cpu_to_le32 (VIRTIO_VSOCK_SEQ_EOR ); } } +if (info->type ==VIRTIO_VSOCK_TYPE_DGRAM ) { +struct virtio_vsock_hdr *hdr =virtio_vsock_hdr (skb); + +/* Vhost may split this message across guest RX buffers. */ + hdr->flags |=cpu_to_le32 (VIRTIO_VSOCK_DGRAM_START | +VIRTIO_VSOCK_DGRAM_EOM ); + }if (info->reply)virtio_vsock_skb_set_reply (skb);
@@ -1876,7 +1883,9 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,* datagrams are silently dropped (no RST). */ if (le16_to_cpu (hdr->type) ==VIRTIO_VSOCK_TYPE_DGRAM ) { -if (le16_to_cpu (hdr->op) !=VIRTIO_VSOCK_OP_RW ) +if (le16_to_cpu (hdr->op) !=VIRTIO_VSOCK_OP_RW || +le32_to_cpu (hdr->flags) != + (VIRTIO_VSOCK_DGRAM_START |VIRTIO_VSOCK_DGRAM_EOM )) goto free_pkt; sk =vsock_find_bound_dgram_socket_net (&dst, net);
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC v2 7/8] vsock: fragment datagrams across receive buffers
Sashiko has reviewed this patch and found no issues.
Quoted 19 lines
The guest already uses page-sized RX allocations. A datagram can exceed one RX buffer. Growing every posted buffer to hold a full datagram would increase virtqueue memory use and make small queued datagrams retain large backing allocations. With delayed reads, a bounded receive queue could then fill after only a few messages.
Define START and EOM flags for datagram packets. Mark outgoing messages with both flags, and let vhost split a message across guest RX buffers, keeping START on the first fragment and EOM on the last. When a data packet meets a descriptor with no payload space, vhost returns that descriptor with zero used bytes. It drops datagrams but requeues stream and seqpacket data for a later buffer.
Reassemble consecutive fragments in the guest before tap or socket delivery. Bound a partial message to 64 fragments and 64 KiB. Discard incomplete messages on malformed packets, a new START, a peer mismatch, an intervening non-datagram packet, or virtqueue teardown. Require complete framing for host and loopback ingress.
Quoted 8 lines
A QEMU reproducer now delivers a 9 KiB datagram as one message and all 16 messages of a delayed-read burst. Paired host/guest selftests cover 64 KiB messages and the burst through both a userspace backend and the patched Linux vhost backend, with stream and seqpacket controls. A nested vhost-vsock guest posting header-only RX buffers also passes stream and seqpacket receive tests.
Signed-off-by Sergio Lopez <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
RFC PATCH v2 8/8 vsock: add datagram tests to vsock_test
In reply to Sergio LopezCover local semantics, host/guest transfers, routing and queue accounting. Synchronize capability-based skips and document manual checks.
Probe VMCI and virtio/vhost independently for capability skips. VMCI can own a datagram socket while a directional transport serves its peer. Local cases require loopback, but do not require a directional transport. The VMCI queue-length case skips when loopback would take self-CID traffic, so a passing result exercises VMCI receive accounting. Document this coexistence in the README.
Test that connected loopback datagrams reject a different local CID at the same port, including after write shutdown, and that wildcard senders use the addressed CID and receive replies.
Add a delayed-read burst in both directions. Send 16 distinct 512-byte datagrams before the receiver reads, with a bounded SO_RCVBUF. Check message content and source address and verify no extra packet remains.
Test that datagrams and stream/SEQPACKET sockets can bind the same CID and port in both orders, including wildcard CIDs, and that each receives only its own traffic. Stream and SEQPACKET binds must still conflict.
--- tools/testing/vsock/Makefile | 3 +- tools/testing/vsock/README | 31 +- tools/testing/vsock/util.c | 62 +- tools/testing/vsock/util.h | 10 + tools/testing/vsock/vsock_test.c | 175 +++++- tools/testing/vsock/vsock_test_dgram.c | 1000 ++++++++++++++++++++++++++++++++ tools/testing/vsock/vsock_test_dgram.h | 29 + 7 files changed, 1278 insertions(+), 32 deletions(-)
tools/testing/vsock/Makefile +2 -1
@@ -1,7 +1,8 @@ # SPDX-License-Identifier: GPL-2.0-only all: test vsock_perf test: vsock_test vsock_diag_test vsock_uring_test -vsock_test: vsock_test.o vsock_test_zerocopy.o timeout.o control.o util.o msg_zerocopy_common.o +vsock_test: vsock_test.o vsock_test_zerocopy.o vsock_test_dgram.o \ + timeout.o control.o util.o msg_zerocopy_common.o vsock_diag_test: vsock_diag_test.o timeout.o control.o util.o vsock_perf: vsock_perf.o msg_zerocopy_common.o
tools/testing/vsock/README +29 -2
@@ -5,7 +5,7 @@ Hyper-V. The following tests are available: - * vsock_test - core AF_VSOCK socket functionality + * vsock_test - stream, seqpacket and datagram functionality * vsock_diag_test - vsock_diag.ko module for listing open sockets The following prerequisite steps are not automated and must be performed prior
@@ -32,7 +32,7 @@ Invoke test binaries in both directions as follows: --control-port=1234 \ --peer-cid=2 (host)# $TEST_BINARY --mode=client \ - --control-port=$GUEST_IP \ + --control-host=$GUEST_IP \ --control-port=1234 \ --peer-cid=3
@@ -51,6 +51,33 @@ suite run, e.g. For more information see Documentation/dev-tools/kmemleak.rst. +Datagram tests +-------------- +vsock_test includes datagram cases in the client/server runs above. Unsupported +cases print a skip reason. + +Virtio/vhost datagrams require kernel support on both sides and a VMM that +negotiates VIRTIO_VSOCK_F_DGRAM. Load vsock_loopback on both host and guest for +the local cases, and make /dev/vsock available. VMCI's dedicated datagram +transport can coexist with virtio/vhost: it owns the datagram endpoint while +negotiated directional routes can serve individual destinations. + +Run only the local datagram cases without a peer or TCP control connection: + + ./vsock_test --local + +Use --list and --pick <test_id> to select individual cases. The BPF admission +test (51, requiring BPF privileges) and nested routing test (52, requiring both +virtio and vhost transports) need explicit selection. The VMCI queue-length +test (56) requires VMCI without vsock_loopback, so self-directed traffic uses +VMCI rather than loopback. +The connected loopback peer test (57) checks same-port senders on different +local CIDs and wildcard source addresses. +The delayed-read burst test (58) queues 16 datagrams under a bounded receive +buffer in each direction before the receiver reads them. +The port-space test (59) binds a datagram and a stream or seqpacket listener +to the same CID and port in both orders, then checks delivery to both sockets. + vsock_perf utility ------------------- 'vsock_perf' is a simple tool to measure vsock performance. It works in
tools/testing/vsock/util.c +35 -27
82 lines not on this page · raw message
tools/testing/vsock/util.h +10 -0
@@ -51,6 +51,10 @@ struct test_opts {enum test_mode mode;unsigned int peer_cid;unsigned int peer_port; +unsigned int bind_cid; +bool local; +bool to_host; +bool expect_to_host; };/* A test case definition. Test functions must print failures to stderr and
@@ -66,6 +70,12 @@ struct test_case {void (*run_server)(const struct test_opts *opts);bool skip; +bool local;/* Can run without a control connection */ + +/* Return a skip reason or NULL after the selection barrier. Checks +* that coordinate over the control channel must agree on both peers. +*/ +const char *(*check)(const struct test_opts *opts); };void init_signals (void );
tools/testing/vsock/vsock_test.c +173 -2
232 lines not on this page · raw message
tools/testing/vsock/vsock_test_dgram.c new file +1000 -0
1,000 lines not on this page · raw message
tools/testing/vsock/vsock_test_dgram.h new file +29 -0
@@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef VSOCK_TEST_DGRAM_H +#define VSOCK_TEST_DGRAM_H + +#include "util.h" + +const char *vsock_dgram_check_local (const struct test_opts *opts); +const char *vsock_dgram_check_peer (const struct test_opts *opts); +const char *vsock_dgram_check_routes (const struct test_opts *opts); +const char *vsock_dgram_check_vmci (const struct test_opts *opts); + +void test_dgram_bind (const struct test_opts *opts); +void test_dgram_receive (const struct test_opts *opts); +void test_dgram_receive_flags (const struct test_opts *opts); +void test_dgram_filter (const struct test_opts *opts); +void test_dgram_connect (const struct test_opts *opts); +void test_dgram_connected_cid (const struct test_opts *opts); +void test_dgram_source_cid (const struct test_opts *opts); +void test_dgram_bound_lookup (const struct test_opts *opts); +void test_dgram_sockmap (const struct test_opts *opts); +void test_dgram_nested_source (const struct test_opts *opts); +void test_dgram_sizes (const struct test_opts *opts); +void test_dgram_burst (const struct test_opts *opts); +void test_dgram_port_space (const struct test_opts *opts); +void test_dgram_routes (const struct test_opts *opts); +void test_dgram_peer_filter (const struct test_opts *opts); +void test_dgram_vmci_inq (const struct test_opts *opts); + +#endif /* VSOCK_TEST_DGRAM_H */
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC v2 8/8] vsock: add datagram tests to vsock_test
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] vsock_test: local tests calling local_cid() fail without virtio or vhost - [Medium] vsock_test…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] vsock_test: local tests calling local_cid() fail without virtio or vhost
- [Medium] vsock_test: false-positive DGRAM_VIRTIO and DGRAM_PEER in dgram_capabilities()
- [Medium] vsock_test: VMCI coexistence fails in test_dgram_bind() and local CID bind
--
commit 004d4f33594e6af5b36eeee37f6e14f8e43323f5
Author: Sergio Lopez <[email protected]>
vsock: add datagram tests to vsock_test
This commit adds AF_VSOCK datagram tests to vsock_test covering local loopback behavior, host/guest transfers, routing, and queue accounting, along with a --local execution mode and capability-based test skipping.
Quoted 5 lines
tools/testing/vsock/vsock_test_dgram.c new file +0
-0
[ ... ]
tools/testing/vsock/vsock_test_dgram.c line 116
Quoted 13 lines
tools/testing/vsock/vsock_test_dgram.c new file +13
-0+static unsigned int local_cid (void ) +{ +unsigned int cid; +int fd; + + fd =open ("/dev/vsock" ,O_RDONLY |O_CLOEXEC ); +check (fd,"open /dev/vsock" ); +check (ioctl (fd,IOCTL_VM_SOCKETS_GET_LOCAL_CID , &cid),"get local CID" ); +close (fd); +if (cid ==VMADDR_CID_ANY || cid <=VMADDR_CID_LOCAL ) +fail ("a virtio or vhost transport is required (local CID %u)" , cid); +return cid; +}
[Severity: Medium]
Will test_dgram_source_cid(), test_dgram_bound_lookup(), and test_dgram_connected_cid() abort the test run instead of skipping when vsock_loopback is loaded without a virtio or vhost transport?
Those three tests are guarded only by vsock_dgram_check_local(), which succeeds whenever vsock_loopback is registered, but they unconditionally call local_cid(). Without a guest-to-host or host-to-guest transport, vsock_dev_do_ioctl() falls back to transport_local and returns a local cid of 1, causing local_cid() to call fail().
[ ... ]
Quoted 26 lines
tools/testing/vsock/vsock_test_dgram.c new file +26
-0+static unsigned int dgram_capabilities (const struct test_opts *opts, +unsigned int needs) +{ +unsigned int caps =DGRAM_SOCKET ; +struct sockaddr_vm peer; +bool vmci; +int fd, local_fd; + + fd =socket (AF_VSOCK ,SOCK_DGRAM |SOCK_NONBLOCK |SOCK_CLOEXEC ,0 ); +if (fd <0 ) { +if (dgram_unavailable (errno)) +return 0 ; +check (fd,"probe datagram socket" ); + } +/* VMCI may own the socket while a directional transport serves peers. +* Check the drivers independently; connect() below probes the peer route. +*/ + vmci = !access ("/sys/module/vmw_vsock_vmci_transport" ,F_OK ); +if (vmci) + caps |=DGRAM_VMCI ; +if (!vmci || + !access ("/sys/bus/virtio/drivers/vmw_vsock_virtio_transport" ,F_OK ) || + !access ("/sys/class/misc/vhost-vsock" ,F_OK )) + caps |=DGRAM_VIRTIO ; + +if ((needs &DGRAM_PEER ) && (caps &DGRAM_VIRTIO )) {
tools/testing/vsock/vsock_test_dgram.c line 184
Quoted 10 lines
tools/testing/vsock/vsock_test_dgram.c new file +10
-0+bind_fd (fd, opts->bind_cid,VMADDR_PORT_ANY ); + peer =address (opts->peer_cid, opts->peer_port); +if (opts->to_host) + peer.svm_flags =VMADDR_FLAG_TO_HOST ; +if (!connect (fd, (struct sockaddr *)&peer,sizeof (peer))) + caps |=DGRAM_PEER ; +else if (!dgram_unavailable (errno) && errno !=EINVAL ) +check (-1 ,"probe peer datagram route" ); + } +close (fd);
[Severity: Medium]
Can dgram_capabilities() report false-positive virtio and peer datagram support when either vmw_vsock_vmci_transport is absent or the directional transport did not negotiate datagrams?
When vmw_vsock_vmci_transport is not loaded, !vmci short-circuits the sysfs checks and sets the virtio capability even if only vsock_loopback is loaded; if opts->peer_cid is 2 without a guest-to-host transport, vsock_use_local_transport() routes connect() to loopback and also sets the peer capability. Conversely, when vmw_vsock_vmci_transport is loaded alongside virtio or vhost without datagram support negotiated, vsock_dgram_get_transport() falls back to vmci_transport during connect(), so connect() succeeds and peer tests run and fail instead of skipping.
[ ... ]
Quoted 17 lines
tools/testing/vsock/vsock_test_dgram.c new file +17
-0+void test_dgram_bind (const struct test_opts *opts) +{ +int rx =bound_socket (VMADDR_CID_ANY ); +struct sockaddr_vm dest =local_address (rx); +int tx =dgram_socket (); +int other =dgram_socket (); +struct sockaddr_vm src; +char byte; + + dest.svm_cid =VMADDR_CID_ANY ; +expect_error (bind (other, (struct sockaddr *)&dest,sizeof (dest)), +EADDRINUSE ,"duplicate bind" ); +send_packet (tx, &dest,"A" ,1 ); + src =recv_packet (rx, &byte,1 ,0 ,1 , false); +if (byte !='A' || src.svm_port !=sock_name (tx, false).svm_port || + src.svm_cid !=VMADDR_CID_LOCAL || src.svm_port <1024 ) +fail ("autobind or wildcard destination returned an invalid source" );
[ ... ]
tools/testing/vsock/vsock_test_dgram.c line 577
Quoted 12 lines
tools/testing/vsock/vsock_test_dgram.c new file +12
-0+static void check_connected_local_cid (unsigned int peer_cid, +unsigned int stray_cid,bool shutdown_write) +{ +int rx =bound_socket (VMADDR_CID_ANY ); +int allowed =bound_socket (peer_cid); +int stray =dgram_socket (); +struct sockaddr_vm peer =sock_name (allowed, false); +struct sockaddr_vm dest =local_address (rx); +struct sockaddr_vm src; +char byte; + +bind_fd (stray, stray_cid, peer.svm_port);
[Severity: Medium]
Do test_dgram_bind() and check_connected_local_cid() fail when vmw_vsock_vmci_transport is loaded alongside vsock_loopback?
When vmci_transport owns the datagram socket, vsock_dgram_sendmsg() resolves a wildcard destination cid via transport->get_local_cid() (vmci_get_context_id()) instead of 1, so test_dgram_bind() failseither in send_packet() or on the src.svm_cid check. In check_connected_local_cid(), vmci_transport_dgram_bind() passes flags = 0 to dg_create_handle() for any non-wildcard cid, ignoring addr->svm_cid and registering both allowed and stray under (vmci_get_context_id(), peer.svm_port), so binding stray fails with -EADDRINUSE.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Michael S. Tsirkin
maintainerQuoted 6 lines · Sergio Lopez
This RFC adds
SOCK_DGRAMsupport to virtio-vsock and vhost-vsock through the existingAF_VSOCKAPI. It is intended for guest and host services that exchange independent requests, responses, or events and need message boundaries and sender addresses without a stream connection or application framing.
Quoted 6 lines
VIRTIO_VSOCK_F_DGRAMgates virtio traffic in both directions. Each virtio send carries zero to 64 KiB of payload in oneVIRTIO_VSOCK_TYPE_DGRAMpacket. On the RX virtqueue, the device may split a datagram across page-sized guest buffers.
START and EOM delimit the message; the guest reassembles at most 64 fragments and 64 KiB before delivering it to a socket.
where do these limits come from? how does peer know not to send more?
Thanks!
Quoted 108 lines
Invalid or
incomplete messages are discarded. Datagram delivery is best effort:
there are no peer credits, acknowledgements, or retransmissions. Send admission uses the socket send-memory limit, and SIOCOUTQ reports charged send memory.Datagram sockets have a separate port space from stream and seqpacket sockets. The latter two continue to share their port space. Datagram routes are chosen per destination among guest-to-host, host-to-guest, and loopback transports, with VMCI as a fallback. VMCI still owns its socket handle when it is present; loopback works without a directional transport. Hyper-V continues to reject datagram sockets.
Bobby Eshleman and Amery Hung explored this feature in their v6 series and v7 work-in-progress branch. This series is a fresh implementation against the current vsock code. It follows the separate datagram port space, per-destination routing, and VMCI fallback requirements discussed there. For guest receive buffers, it uses bounded fragmentation and reassembly rather than full-message buffers or the earlier drop/error queue approach:
https://lore.kernel.org/r/[email protected]
https://github.com/ameryhung/linux/tree/vsock-dgram-v7A corresponding virtio specification update has been drafted separately. The wire format and feature negotiation still need specification review before this series can be considered for merging.
The first patches expose the core port allocator and add datagram queue reporting, then implement the common data path, guest and host feature negotiation, routing, and RX fragmentation. The last patch extends
vsock_testwith local and peer tests for message boundaries, routing, connected peers, port sharing, queue accounting, and delayed reads.Testing used QEMU with the
F_DGRAMproperty patch below, an x86-64 KASAN/lockdep kernel, paired guest tests, and a nested Linux vhost-vsock backend. The tested paths include 64 KiB messages, delayed-read bursts, feature-disabled ingress, stream and seqpacket controls, and header-only guest RX buffers. A temporary userspace backend still fails stream test 21 on both the series base and tip; that case passes with Linux vhost. Real VMCI and Hyper-V hardware were not exercised.https://lists.gnu.org/archive/html/qemu-devel/2026-10/msg08074.html
Code was written and reviewed both manually and assisted by Codex (GPT-6.1-Sol) and Claude (Opus 4.6).
Signed-off-by Sergio Lopez <[email protected]>---
Changes in v2:
- Keep guest RX buffers page-sized; add START/EOM framing and bounded
datagram reassembly (thanks to Alexei Starovoitov and Bobby Eshleman).
- Preserve stream and seqpacket data when an RX buffer has no payload
room.
- Separate datagram and connection-oriented port spaces, and share
receive metadata and queueing (thanks to Alexei Starovoitov).
- Check negotiated support and destination CID on ingress, and require
an exact connected peer CID and port (thanks to Sashiko AI bot).
- Select a capable route for each destination, retain VMCI as fallback,
and support loopback-only sockets (thanks to Alexei Starovoitov).
- Preserve the connected peer address across temporary parent fallback
(thanks to Sashiko AI bot).
- Use socket write-memory accounting for datagram SIOCOUTQ instead of
separate transport counters (thanks to Alexei Starovoitov).
- Extend selftests for fragmentation, delayed reads, port sharing,
connected peers, and capability-based skips; fix the test build and
README entries (thanks to Alexei Starovoitov, Bobby Eshleman,
and Sashiko AI bot).
- Link to v1: https://lore.kernel.org/r/[email protected]---
Sergio Lopez (8):
vsock: expose the core port allocator to transports
vsock: report datagram receive and send queue state vsock/virtio: implement the datagram data path vsock/virtio: negotiate datagram support on the guest vhost/vsock: negotiate datagram support with guests
vsock: allow datagrams to use directional transports
vsock: fragment datagrams across receive buffers
vsock: add datagram tests tovsock_testDocumentation/admin-guide/sysctl/net.rst | 8 +- drivers/vhost/vsock.c | 51 +- include/linux/virtio_vsock.h | 9 +- include/net/af_vsock.h | 30 +- include/uapi/linux/virtio_vsock.h | 10 + net/vmw_vsock/af_vsock.c | 459 +++++++++++--- net/vmw_vsock/hyperv_transport.c | 11 +- net/vmw_vsock/virtio_transport.c | 167 ++++- net/vmw_vsock/virtio_transport_common.c | 164 ++++- net/vmw_vsock/vmci_transport.c | 115 ++-- net/vmw_vsock/vsock_bpf.c | 3 +- net/vmw_vsock/vsock_loopback.c | 2 +- tools/testing/vsock/Makefile | 3 +- tools/testing/vsock/README | 31 +- tools/testing/vsock/util.c | 62 +- tools/testing/vsock/util.h | 10 + tools/testing/vsock/vsock_test.c | 175 +++++- tools/testing/vsock/vsock_test_dgram.c | 1000 ++++++++++++++++++++++++++++++ tools/testing/vsock/vsock_test_dgram.h | 29 + 19 files changed, 2066 insertions(+), 273 deletions(-)---
base-commit:65ba8f34ec886ec72b2c07ddc8cf3d815c32869d
change-id: 20261027-vsock-dgram-support-683a4e0aebe7Best regards,
Signature · 1 line
Sergio Lopez <[email protected]>
Quoted 7 lines · Michael S. Tsirkin
Quoted 6 lines · Sergio Lopez
This RFC adds
SOCK_DGRAMsupport to virtio-vsock and vhost-vsock through the existingAF_VSOCKAPI. It is intended for guest and host services that exchange independent requests, responses, or events and need message boundaries and sender addresses without a stream connection or application framing.
Quoted 9 lines
Quoted 6 lines
VIRTIO_VSOCK_F_DGRAMgates virtio traffic in both directions. Each virtio send carries zero to 64 KiB of payload in oneVIRTIO_VSOCK_TYPE_DGRAMpacket. On the RX virtqueue, the device may split a datagram across page-sized guest buffers.
START and EOM delimit the message; the guest reassembles at most 64 fragments and 64 KiB before delivering it to a socket.where do these limits come from? how does peer know not to send more?
The 64 KiB limit comes from VIRTIO_VSOCK_MAX_PKT_BUF_SIZE. The number of fragments is a reasonable number to prevent the driver from putting too much strain on the device.
Both limits should be part of the spec. This is the relevant part from the draft I have put together so far:
+When VIRTIO_VSOCK_F_DGRAM is negotiated, each rx virtqueue buffer supplied +by the driver MUST have room for the header and at least 1024 payload bytes. +The driver MUST discard a received datagram packet whose \field{dst_cid} +differs from the current \field{guest_cid} configuration value. +The driver MUST reassemble consecutive fragments with matching address +fields and MUST NOT deliver a partial datagram. It MUST discard an orphaned +fragment, a fragment with an unsupported flag or operation, and any +incomplete datagram interrupted by a new START or an unrelated packet. It +MUST discard a datagram if its total length exceeds 65536 bytes or it has +more than 64 fragments.
Thanks!
Sergio.