Skip to contentSkip to conversation

vsock: add datagram support to virtio and vhost

RFC PATCH Superseded Sergio Lopez 3 participants 21 messages Nov 13 – Nov 19

Subsystems (12)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 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.

The Sashiko review of v2 also reported that a VMCI datagram bind in a local-mode network namespace creates a receive handle in VMCI's global port space, so a sender outside the namespace can deliver datagrams across the namespace isolation boundary. The issue predates this series and it's getting addressed in a separate patch:

https://lore.kernel.org/all/[email protected]

Within this series, local-mode namespaces bind datagrams only in the core table and never create a VMCI handle.

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. VMCI's host personality was exercised in a QEMU guest, including coexistence with virtio and self-send without loopback. A VMware VMCI guest device 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 v3:
- Preserve Hyper-V stream routing and prefer virtio for guest CID_HOST
datagrams (thanks to Sashiko AI bot).
- Bind explicit non-VMCI CIDs and local-mode sockets without a VMCI
handle, resolve wildcard destinations, and retain VMCI self routes
(thanks to Sashiko AI bot).
- Recheck bound addresses after receive takes the socket lock and set
TO_HOST only for forwarded guest sources (thanks to Sashiko AI bot).
- Probe negotiated virtio datagrams and numeric local CIDs before running dependent tests; cover wildcard and VMCI coexistence (thanks to Sashiko
AI bot).
- Link to v2: https://lore.kernel.org/r/[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 to vsock_test

 Documentation/admin-guide/sysctl/net.rst |    8 +-
 drivers/vhost/vsock.c                    |   51 +-
 include/linux/virtio_vsock.h             |    8 +-
 include/net/af_vsock.h                   |   31 +-
 include/uapi/linux/virtio_vsock.h        |   10 +
 net/vmw_vsock/af_vsock.c                 |  526 +++++++++++----
 net/vmw_vsock/hyperv_transport.c         |   11 +-
 net/vmw_vsock/virtio_transport.c         |  167 ++++-
 net/vmw_vsock/virtio_transport_common.c  |  172 ++++-
 net/vmw_vsock/vmci_transport.c           |  124 ++--
 net/vmw_vsock/vsock_bpf.c                |    3 +-
 net/vmw_vsock/vsock_loopback.c           |    2 +-
 tools/testing/vsock/Makefile             |    3 +-
 tools/testing/vsock/README               |   33 +-
 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   | 1061 ++++++++++++++++++++++++++++++
 tools/testing/vsock/vsock_test_dgram.h   |   30 +
 19 files changed, 2195 insertions(+), 292 deletions(-)

---
base-commit: 65ba8f34ec886ec72b2c07ddc8cf3d815c32869d
change-id: 20261027-vsock-dgram-support-683a4e0aebe7

Best regards,

Signature · 1 line
Sergio Lopez <[email protected]>
#1 lore raw

RFC PATCH v3 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
index e8481f12312e..ac061115ed30 100644
@@ -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
#2 lore raw

Re: [PATCH RFC v3 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.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

RFC PATCH v3 2/8 vsock: report datagram receive and send queue state

In reply to Sergio Lopez

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.

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
index ac061115ed30..e4fbea3b0e46 100644
@@ -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
index d5cf240d4ece..8a271eb66860 100644
@@ -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
index 1f186e8f8364..2b9493e987d1 100644
@@ -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
#4 lore raw

Re: [PATCH RFC v3 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.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#5 lore raw

RFC PATCH v3 3/8 vsock/virtio: implement the datagram data path

In reply to Sergio Lopez

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_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
index 64738838bee5..27b4b2b8bf13 100644
@@ -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
index 8a271eb66860..aef5389eeca2 100644
@@ -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
index 9049d2648646..296e1fa7970b 100644
@@ -161,7 +161,8 @@ int vsock_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore
 	if (!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
#6 lore raw

Re: [PATCH RFC v3 3/8] vsock/virtio: implement the datagram data path

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] vsock/virtio: OOB read on filtered nonlinear vhost dgram skb - [Medium] vsock/virtio: empty…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] vsock/virtio: OOB read on filtered nonlinear vhost dgram skb
- [Medium] vsock/virtio: empty datagrams skipped on repeated MSG_PEEK --

commit 7bb89ea8d31712feb1c6f76854206e10a5fd07b5
Author: Sergio Lopez <[email protected]>

vsock/virtio: implement the datagram data path

This commit adds the VIRTIO_VSOCK_F_DGRAM feature bit and VIRTIO_VSOCK_TYPE_DGRAM packet type, and implements the common virtio transport operations to bind, send, and receive datagrams.

Quoted 4 lines
net/vmw_vsock/virtio_transport_common.c +0 -0
index 1e762a480df49..08baa1181ba6d 100644

[ ... ]

Quoted 7 lines
net/vmw_vsock/virtio_transport_common.c +2 -1
@@ -999,7 +1018,47 @@ virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
 			       struct msghdr *msg,
 			       size_t len, int flags)
 {
-	return -EOPNOTSUPP;
+	struct sock *sk = sk_vsock(vsk);
+	struct virtio_vsock_hdr *hdr;

net/vmw_vsock/virtio_transport_common.c virtio_transport_dgram_dequeue(struct vsock_sock *vsk, · line 1032

Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +10 -0
+	struct sk_buff *skb;
+	size_t payload_len;
+	int err;
+
+	if (flags & (MSG_OOB | MSG_ERRQUEUE))
+		return -EOPNOTSUPP;
+
+	skb = skb_recv_datagram(sk, flags, &err);
+	if (!skb)
+		return err;

[Severity: Medium]
Does calling skb_recv_datagram() skip an unread empty datagram when userspace calls recvmsg() with MSG_PEEK more than once?

Because virtio_vsock_hdr lives in skb headroom, an empty datagram has skb->len == 0. skb_recv_datagram() passes off = 0 to __skb_recv_datagram(), which sets peek_at_off = true in __skb_try_recv_from_queue():

net/core/datagram.c:__skb_try_recv_from_queue() {
	...
		if (flags & MSG_PEEK) {
			if (peek_at_off && _off >= skb->len &&
			    (_off || skb->peeked)) {
				_off -= skb->len;
				continue;
			}
			if (!skb->len) {
				skb = skb_set_peeked(skb);
				if (unlikely(!skb)) {
					*err = -ENOMEM;
					return NULL;
				}
			}
			*peeked = 1;
			refcount_inc(&skb->users);
		} else
	...
}

The first MSG_PEEK sets skb->peeked = 1 on the 0-length skb, and the next MSG_PEEK sees _off >= skb->len && skb->peeked and skips the unread empty datagram, returning the following datagram out of order or failing with -EAGAIN. Calling __skb_recv_datagram() with sk_peek_offset(sk, flags) avoids this, as in commit a0917e0bc6ef ("datagram: When peeking datagrams with offset < 0 don't skip empty skbs").

[ ... ]

Quoted 20 lines
net/vmw_vsock/virtio_transport_common.c +16 -0
@@ -1805,6 +1875,38 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,
 		goto free_pkt;
 	}
 
+	/* Dgram packets are delivered directly to bound sockets without
+	 * going through the connection state machine. Undeliverable
+	 * 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)
+			goto free_pkt;
+
+		sk = vsock_find_bound_dgram_socket_net(&dst, net);
+
+		if (!sk)
+			goto free_pkt;
+
+		vsk = vsock_sk(sk);
+		lock_sock(sk);
+		if (sock_flag(sk, SOCK_DEAD) ||

net/vmw_vsock/virtio_transport_common.c void virtio_transport_recv_pkt(struct virtio_transport *t, · line 1903

Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +10 -0
+		    vsk->transport != &t->transport ||
+		    (sk->sk_socket->state != SS_UNCONNECTED &&
+		     !vsock_check_source(vsk, &t->transport, &src))) {
+			release_sock(sk);
+			sock_put(sk);
+			goto free_pkt;
+		}
+
+		if (sock_queue_rcv_skb(sk, skb))
+			kfree_skb(skb);

[Severity: High]
Can sock_queue_rcv_skb() corrupt a nonlinear vhost-vsock skb when a socket filter trims the packet, leading to an out-of-bounds read in virtio_transport_dgram_dequeue()?

In vhost_vsock_alloc_skb(), the skb is allocated from the guest TX descriptor chain length before hdr->len is inspected, and virtio_vsock_skb_put() then sets skb->len = payload_len without adjusting skb->data_len:

include/linux/virtio_vsock.h:virtio_vsock_skb_put() {
	if (skb_is_nonlinear(skb))
		skb->len = len;
	else
		skb_put(skb, len);
}

When a guest sends a TX descriptor chain larger than 32 KiB with hdr->len smaller than the descriptor payload space, the resulting nonlinear skb has skb->len < skb->data_len and a negative skb_headlen(skb). If the receiving host datagram socket has a filter attached, sock_queue_rcv_skb() -> sk_filter_trim_cap() -> pskb_trim() -> ___pskb_trim() treats the negative skb_headlen(skb) as a large unsigned offset, drops all page frags, zeroes skb->data_len, and sets skb->tail past skb->end. The subsequent skb_copy_datagram_iter() in virtio_transport_dgram_dequeue() then copies skb_shared_info and out-of-bounds kernel heap memory after skb->end to userspace.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#7 lore raw

RFC PATCH v3 4/8 vsock/virtio: negotiate datagram support on the guest

In reply to Sergio Lopez

Advertise 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
index 4f9aa9c4c3aa..fd10f20d7611 100644
@@ -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
#8 lore raw

Re: [PATCH RFC v3 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_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.

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.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#9 lore raw

RFC PATCH v3 5/8 vhost/vsock: negotiate datagram support with guests

In reply to Sergio Lopez

Expose 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
index 0cb87971d749..7971f6f0b6a2 100644
@@ -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
#10 lore raw

Re: [PATCH RFC v3 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_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.

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.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#11 lore raw

RFC PATCH v3 6/8 vsock: allow datagrams to use directional transports

In reply to Sergio Lopez

Use 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. Bind local namespace sockets only in the core table because VMCI handles occupy a global port space.

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             |   8 +-
 include/net/af_vsock.h                   |  30 ++-
 net/vmw_vsock/af_vsock.c                 | 373 +++++++++++++++++++++++++------
 net/vmw_vsock/hyperv_transport.c         |  11 +-
 net/vmw_vsock/virtio_transport.c         |   2 +-
 net/vmw_vsock/virtio_transport_common.c  | 121 +++++-----
 net/vmw_vsock/vmci_transport.c           | 133 +++++------
 net/vmw_vsock/vsock_loopback.c           |   2 +-
 10 files changed, 455 insertions(+), 235 deletions(-)
Documentation/admin-guide/sysctl/net.rst +5 -3
index fe43e8595958..30d81128fd29 100644
@@ -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
index 7971f6f0b6a2..f4fb2ac732f5 100644
@@ -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 +3 -5
index f91704731057..221ce1a1c1dc 100644
@@ -171,6 +171,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 +205,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 +257,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 +271,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 +22 -8
index e4fbea3b0e46..29494297106c 100644
@@ -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;
@@ -43,6 +50,7 @@ struct vsock_sock {
 					 * cached peer?
 					 */
 	u32 cached_peer;  /* Context ID of last dgram destination check. */
+	bool dgram_peer_is_vmci_self;	/* Wildcard connect chose VMCI self-send. */
 	const struct cred *owner;
 	/* Rest are SOCK_STREAM only. */
 	long connect_timeout;
@@ -98,8 +106,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 +127,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 +233,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 +253,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 vmci_self);
 bool vsock_find_cid(unsigned int cid);
 void vsock_linger(struct sock *sk);
 
net/vmw_vsock/af_vsock.c +302 -71

549 lines not on this page · raw message

net/vmw_vsock/hyperv_transport.c +4 -7
index 855b15971c35..015c5371ea98 100644
@@ -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
index fd10f20d7611..87a96e94c3c2 100644
@@ -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 +66 -55

181 lines not on this page · raw message

net/vmw_vsock/vmci_transport.c +50 -83

196 lines not on this page · raw message

net/vmw_vsock/vsock_loopback.c +1 -1
index 8068d1b6e851..645b10843f86 100644
@@ -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
#12 lore raw

Re: [PATCH RFC v3 6/8] vsock: allow datagrams to use directional transports

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] vsock: local datagrams route to transport_g2h when loopback is unloaded - [Medium] vsock: VMCI…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] vsock: local datagrams route to transport_g2h when loopback is unloaded
- [Medium] vsock: VMCI datagram sockets misroute CID_HOST and CID_ANY to loopback
--

commit 9eab4679f5d6926cf379df182996c2e8cd726902
Author: Sergio Lopez <[email protected]>

vsock: allow datagrams to use directional transports

This commit enables datagram sockets to select directional and loopback transports per destination while keeping a fallback transport as the socket owner when needed, and unifies datagram receive queuing in the core.

Quoted 4 lines
net/vmw_vsock/af_vsock.c +0 -0
index aef5389eeca2c..e0d1a3e5d05cc 100644

[ ... ]

Quoted 30 lines
net/vmw_vsock/af_vsock.c +22 -4
@@ -576,11 +579,56 @@ 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();
-	} else {
-		return remote_cid == VMADDR_CID_HOST;
+	local_cid = transport_g2h ? transport_g2h->get_local_cid() :
+		VMADDR_CID_HOST;
+	/* A virtio device without a CID keeps the host loopback alias.
+	 * Hyper-V has no numeric CID even while it is active, so CID_HOST
+	 * must keep using its guest-to-host transport in that case.
+	 */
+	if (local_cid == VMADDR_CID_ANY && transport_g2h->dgram_transport_match)
+		local_cid = VMADDR_CID_HOST;
+
+	return remote_cid == local_cid;
+}
+
+/* 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);

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
+	u32 cid = remote_addr->svm_cid;
+
+	lockdep_assert_held(&vsock_register_mutex);
+	*to_host = false;
+
+	if (vsock_use_local_transport(cid))
+		return transport_local;
+	if (cid <= VMADDR_CID_HOST ||
+	    (remote_addr->svm_flags & VMADDR_FLAG_TO_HOST))
+		return transport_g2h;

[Severity: Medium]
Can vsock_select_transport() route VMADDR_CID_LOCAL datagrams to transport_g2h when transport_local is not registered?

When transport_local is NULL, vsock_use_local_transport(VMADDR_CID_LOCAL) returns false and cid <= VMADDR_CID_HOST (1 <= 2) matches, returning transport_g2h instead of NULL. Because vsock_dgram_resolve_cid() also maps VMADDR_CID_ANY to VMADDR_CID_LOCAL whenever owner->dgram_transport_match is set, datagram connect() and sendmsg() to VMADDR_CID_LOCAL or VMADDR_CID_ANY transmit local packets to the host (or fall back to the VMCI owner) instead of failing with -ENODEV. When vsock.g2h_fallback is enabled without transport_local, connecting a datagram socket to the guest's own CID also succeeds via transport_g2h even though subsequent sends fail in virtio_transport_send_pkt().

[ ... ]

Quoted 14 lines
net/vmw_vsock/af_vsock.c +10 -0
@@ -1461,6 +1645,27 @@ static int vsock_read_skb(struct sock *sk, skb_read_actor_t read_actor)
 	return vsk->transport->read_skb(vsk, read_actor);
 }
 
+static int vsock_dgram_resolve_cid(struct vsock_sock *vsk,
+				   struct sockaddr_vm *remote_addr,
+				   bool *vmci_self)
+{
+	const struct vsock_transport *owner = vsk->transport;
+
+	if (remote_addr->svm_cid != VMADDR_CID_ANY)
+		return 0;
+
+	mutex_lock(&vsock_register_mutex);

net/vmw_vsock/af_vsock.c static int vsock_read_skb(struct sock *sk, skb_read_actor_t read_actor) · line 1667

Quoted 10 lines
net/vmw_vsock/af_vsock.c +10 -0
+	if (transport_local || owner->dgram_transport_match) {
+		remote_addr->svm_cid = VMADDR_CID_LOCAL;
+	} else {
+		remote_addr->svm_cid = owner->get_local_cid();
+		*vmci_self = owner->dgram_fallback;
+	}
+	mutex_unlock(&vsock_register_mutex);
+
+	return remote_addr->svm_cid == VMADDR_CID_ANY ? -EADDRNOTAVAIL : 0;
+}

[Severity: Medium]
Does this misroute VMCI datagrams onto loopback when vsock_loopback is loaded?

On a VMCI guest where virtio_transport is loaded without an active virtio-vsock device (or transport_g2h is NULL), vsock_use_local_transport(VMADDR_CID_HOST) returns true, so vsock_select_transport() routes VMADDR_CID_HOST datagrams to transport_local instead of falling back to the VMCI owner. In addition, if a VMCI socket is bound to VMCI's local CID and sends or connects to VMADDR_CID_ANY while transport_local is registered, vsock_dgram_resolve_cid() resolves the destination to VMADDR_CID_LOCAL and vsock_dgram_get_transport() skips the fallback to owner because transport->get_local_cid() == VMADDR_CID_LOCAL, failing with -EADDRNOTAVAIL when vsk->local_addr.svm_cid does not match loopback.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#13 lore raw

RFC PATCH v3 7/8 vsock: fragment datagrams across receive buffers

In reply to Sergio Lopez

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.

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
index f4fb2ac732f5..1ff4a9aff907 100644
@@ -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
index 27b4b2b8bf13..e1937f756eeb 100644
@@ -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
index c540bc40755a..23f141cd786a 100644
@@ -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);
@@ -1877,7 +1884,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
#14 lore raw

Re: [PATCH RFC v3 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.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#15 lore raw

Re: [PATCH RFC v3 7/8] vsock: fragment datagrams across receive buffers

In reply to Sergio Lopez
Quoted 73 lines · Sergio Lopez

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.

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
index f4fb2ac732f5..1ff4a9aff907 100644
@@ -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 +0 -0
index 27b4b2b8bf13..e1937f756eeb 100644
@@ -101,6 +101,14 @@ enum virtio_vsock_shutdown {
 enum virtio_vsock_rw {

include/uapi/linux/virtio_vsock.h enum virtio_vsock_shutdown { · line 111

Quoted 10 lines
include/uapi/linux/virtio_vsock.h +8 -0
 	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,

Should this be 4 if VIRTIO_VSOCK_DGRAM_EOM = VIRTIO_VSOCK_SEQ_EOM = 1? Seems like 3 is missing?

include/uapi/linux/virtio_vsock.h enum virtio_vsock_shutdown { · line 112

Quoted 1 line
include/uapi/linux/virtio_vsock.h +0 -0
 };

Is VIRTIO_VSOCK_DGRAM_START strictly necessary? If we just received an EOM from a peer, then we know that the next pkt from that peer is also the start of a new dgram. We'd have to be careful to never abandon a datagram partway through, but for example I think we can just requeue the datagram fragment to later retry if we fail to place onto the vq?

Quoted 229 lines
include/uapi/linux/virtio_vsock.h +0 -0
 #endif /* _UAPI_LINUX_VIRTIO_VSOCK_H */
net/vmw_vsock/virtio_transport.c +108 -7
index 87a96e94c3c2..c8c58a7dcec4 100644
@@ -54,6 +54,8 @@ struct virtio_vsock {
 	bool rx_run;
 	int rx_buf_nr;
 	int rx_buf_max_nr;
+	struct sk_buff_head dgram_rx_queue;
+	u32 dgram_rx_len;
 
 	u32 guest_cid;
 	bool seqpacket_allow;
@@ -312,7 +314,7 @@ virtio_transport_cancel_pkt(struct vsock_sock *vsk)
 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
 {
 	int total_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE;
-	struct scatterlist pkt, *p;
+	struct scatterlist pkt;
 	struct virtqueue *vq;
 	struct sk_buff *skb;
 	int ret;
@@ -325,9 +327,9 @@ static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
 			break;
 
 		memset(skb->head, 0, VIRTIO_VSOCK_SKB_HEADROOM);
+		virtio_vsock_skb_put(skb, total_len - VIRTIO_VSOCK_SKB_HEADROOM);
 		sg_init_one(&pkt, virtio_vsock_hdr(skb), total_len);
-		p = &pkt;
-		ret = virtqueue_add_sgs(vq, &p, 0, 1, skb, GFP_KERNEL);
+		ret = virtqueue_add_inbuf(vq, &pkt, 1, skb, GFP_KERNEL);
 		if (ret < 0) {
 			kfree_skb(skb);
 			break;
@@ -661,6 +663,93 @@ virtio_transport_dgram_allow_check(struct vsock_sock *vsk, u32 cid, u32 port)
 	return dgram_allow;
 }
 
+#define VIRTIO_VSOCK_DGRAM_MAX_FRAGS	64
+
+/* The RX worker holds rx_lock while assembling fragments from one virtqueue. */
+static void virtio_vsock_dgram_reset(struct virtio_vsock *vsock)
+{
+	skb_queue_purge(&vsock->dgram_rx_queue);
+	vsock->dgram_rx_len = 0;
+}
+
+static bool virtio_vsock_dgram_same_peer(struct sk_buff *first,
+					 struct sk_buff *next)
+{
+	const struct virtio_vsock_hdr *a = virtio_vsock_hdr(first);
+	const struct virtio_vsock_hdr *b = virtio_vsock_hdr(next);
+
+	return a->src_cid == b->src_cid && a->dst_cid == b->dst_cid &&
+	       a->src_port == b->src_port && a->dst_port == b->dst_port;
+}
+
+static struct sk_buff *
+virtio_vsock_dgram_reassemble(struct virtio_vsock *vsock, struct sk_buff *skb)
+{
+	struct sk_buff_head *queue = &vsock->dgram_rx_queue;
+	struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
+	struct sk_buff *assembled, *fragment;
+	u32 flags = le32_to_cpu(hdr->flags);
+	int offset = 0;
+
+	if (le16_to_cpu(hdr->op) != VIRTIO_VSOCK_OP_RW ||
+	    flags & ~(VIRTIO_VSOCK_DGRAM_START | VIRTIO_VSOCK_DGRAM_EOM))
+		goto drop_partial;
+
+	/* START also resynchronizes after an abandoned datagram. */
+	if (flags & VIRTIO_VSOCK_DGRAM_START)
+		virtio_vsock_dgram_reset(vsock);
+	else if (skb_queue_empty(queue))
+		goto drop_skb;
+
+	fragment = skb_peek(queue);
+	if (fragment && !virtio_vsock_dgram_same_peer(fragment, skb))
+		goto drop_partial;
+	if (skb->len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE - vsock->dgram_rx_len ||
+	    skb_queue_len(queue) >= VIRTIO_VSOCK_DGRAM_MAX_FRAGS ||
+	    (!(flags & VIRTIO_VSOCK_DGRAM_EOM) && !skb->len))
+		goto drop_partial;
+
+	if (flags & VIRTIO_VSOCK_DGRAM_EOM && skb_queue_empty(queue))
+		return skb;
+
+	vsock->dgram_rx_len += skb->len;
+	skb_queue_tail(queue, skb);
+	if (!(flags & VIRTIO_VSOCK_DGRAM_EOM))
+		return NULL;
+
+	assembled = virtio_vsock_alloc_skb(VIRTIO_VSOCK_SKB_HEADROOM +
+					     vsock->dgram_rx_len, GFP_KERNEL);
+	if (!assembled)
+		goto reset;
+	virtio_vsock_skb_put(assembled, vsock->dgram_rx_len);
+	fragment = skb_peek(queue);
+	memcpy(virtio_vsock_hdr(assembled), virtio_vsock_hdr(fragment),
+	       sizeof(*hdr));
+	hdr = virtio_vsock_hdr(assembled);
+	hdr->len = cpu_to_le32(vsock->dgram_rx_len);
+	hdr->flags = cpu_to_le32(VIRTIO_VSOCK_DGRAM_START |
+				  VIRTIO_VSOCK_DGRAM_EOM);
+	skb_queue_walk(queue, fragment) {
+		if (skb_store_bits(assembled, offset, fragment->data,
+				   fragment->len)) {
+			kfree_skb(assembled);
+			goto reset;
+		}
+		offset += fragment->len;
+	}
+	virtio_vsock_dgram_reset(vsock);
+	return assembled;
+
+drop_partial:
+	virtio_vsock_dgram_reset(vsock);
+drop_skb:
+	kfree_skb(skb);
+	return NULL;
+reset:
+	virtio_vsock_dgram_reset(vsock);
+	return NULL;
+}
+
 static void virtio_transport_rx_work(struct work_struct *work)
 {
 	struct virtio_vsock *vsock =
@@ -697,7 +786,8 @@ static void virtio_transport_rx_work(struct work_struct *work)
 
 			/* Drop short/long packets */
 			if (unlikely(len < sizeof(*hdr) ||
-				     len > virtio_vsock_skb_len(skb))) {
+				     len > sizeof(*hdr) + skb->len)) {
+				virtio_vsock_dgram_reset(vsock);
 				kfree_skb(skb);
 				continue;
 			}
@@ -705,6 +795,7 @@ static void virtio_transport_rx_work(struct work_struct *work)
 			hdr = virtio_vsock_hdr(skb);
 			payload_len = le32_to_cpu(hdr->len);
 			if (unlikely(payload_len > len - sizeof(*hdr))) {
+				virtio_vsock_dgram_reset(vsock);
 				kfree_skb(skb);
 				continue;
 			}
@@ -713,12 +804,19 @@ static void virtio_transport_rx_work(struct work_struct *work)
 			if (le16_to_cpu(hdr->type) == VIRTIO_VSOCK_TYPE_DGRAM &&
 			    (!vsock->dgram_allow ||
 			     le64_to_cpu(hdr->dst_cid) != vsock->guest_cid)) {
+				virtio_vsock_dgram_reset(vsock);
 				kfree_skb(skb);
 				continue;
 			}
 
-			if (payload_len)
-				virtio_vsock_skb_put(skb, payload_len);
+			pskb_trim_unique(skb, payload_len);
+			if (le16_to_cpu(hdr->type) == VIRTIO_VSOCK_TYPE_DGRAM) {
+				skb = virtio_vsock_dgram_reassemble(vsock, skb);
+				if (!skb)
+					continue;
+			} else if (!skb_queue_empty(&vsock->dgram_rx_queue)) {
+				virtio_vsock_dgram_reset(vsock);
+			}
 
 			virtio_transport_deliver_tap_pkt(skb);
 
@@ -730,7 +828,8 @@ static void virtio_transport_rx_work(struct work_struct *work)
 	} while (!virtqueue_enable_cb(vq));
 
 out:
-	if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
+	if (!vsock->rx_buf_nr ||
+	    vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
 		virtio_vsock_rx_fill(vsock);
 out_nofill:
 	mutex_unlock(&vsock->rx_lock);
@@ -824,6 +923,7 @@ static void virtio_vsock_vqs_del(struct virtio_vsock *vsock)
 	mutex_lock(&vsock->rx_lock);
 	while ((skb = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_RX])))
 		kfree_skb(skb);
+	virtio_vsock_dgram_reset(vsock);
 	mutex_unlock(&vsock->rx_lock);
 
 	mutex_lock(&vsock->tx_lock);
@@ -865,6 +965,7 @@ static int virtio_vsock_probe(struct virtio_device *vdev)
 
 	mutex_init(&vsock->tx_lock);
 	mutex_init(&vsock->rx_lock);
+	skb_queue_head_init(&vsock->dgram_rx_queue);
 	mutex_init(&vsock->event_lock);
 	skb_queue_head_init(&vsock->send_pkt_queue);
 	INIT_WORK(&vsock->rx_work, virtio_transport_rx_work);
net/vmw_vsock/virtio_transport_common.c +10 -1
index c540bc40755a..23f141cd786a 100644
@@ -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);
@@ -1877,7 +1884,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 · 2 lines
-- 
2.56.0
#16 lore raw

RFC PATCH v3 8/8 vsock: add datagram tests to vsock_test

In reply to Sergio Lopez

Cover 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             |   33 +-
 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 | 1061 ++++++++++++++++++++++++++++++++
 tools/testing/vsock/vsock_test_dgram.h |   30 +
 7 files changed, 1342 insertions(+), 32 deletions(-)
tools/testing/vsock/Makefile +2 -1
index 88211fd132d2..78e2cb7b1193 100644
@@ -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 +31 -2
index 680ce666ceb5..2c6a34ccfef6 100644
@@ -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,35 @@ 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 and a mounted sysfs available. The guest
+probe reads the negotiated virtio features from sysfs before running peer
+cases. 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
index 9dabb547021e..e23290e7dcc6 100644
@@ -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 +1061 -0

1,061 lines not on this page · raw message

tools/testing/vsock/vsock_test_dgram.h new file +30 -0
new file mode 100644
index 000000000000..3cb3e54ee238
@@ -0,0 +1,30 @@
+/* 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_local_cid(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
#17 lore raw

Re: [PATCH RFC v3 8/8] vsock: add datagram tests to vsock_test

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] selftests/vsock: unguarded loopback SOCK_SEQPACKET connect in local netns - [Low] vsock/test…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] selftests/vsock: unguarded loopback SOCK_SEQPACKET connect in local netns
- [Low] vsock/test: incomplete DGRAM_VMCI probe in dgram_capabilities() --

commit 75f8685914783d3ed99f76000f4d13a1f1c04ea3
Author: Sergio Lopez <[email protected]>

vsock: add datagram tests to vsock_test

This commit adds AF_VSOCK datagram tests to vsock_test covering local semantics, peer transfers, routing, queue accounting, and capability-based skips, along with a --local mode for running local test cases standalone.

Quoted 6 lines
tools/testing/vsock/vsock_test_dgram.c new file +0 -0
new file mode 100644
index 0000000000000..4b45dcf2a15cc
@@ -0,0 +1,1061 @@

[ ... ]

Quoted 10 lines
tools/testing/vsock/vsock_test_dgram.c new file +10 -0
+static unsigned int dgram_capabilities(const struct test_opts *opts,
+				       unsigned int needs)
+{
+	unsigned int caps = DGRAM_SOCKET;
+	struct sockaddr_vm peer;
+	int fd, local_fd;
+
+	fd = socket(AF_VSOCK, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
+	if (fd < 0) {
+		if (dgram_unavailable(errno))

tools/testing/vsock/vsock_test_dgram.c line 211

Quoted 10 lines
tools/testing/vsock/vsock_test_dgram.c new file +10 -0
+			return 0;
+		check(fd, "probe datagram socket");
+	}
+	/* VMCI may own the socket while a directional transport serves peers.
+	 * A nested guest can have both virtio and vhost registered. Probe the
+	 * requested direction so vhost registration cannot mask a virtio device
+	 * that did not negotiate datagrams.
+	 */
+	if (!access("/sys/module/vmw_vsock_vmci_transport", F_OK))
+		caps |= DGRAM_VMCI;

[Severity: Low]
Does checking only for /sys/module/vmw_vsock_vmci_transport leave test_dgram_vmci_inq() unskipped when VMCI cannot route datagrams to itself?

When vmw_vsock_vmci_transport is loaded without vsock_loopback, running in a local-mode vsock network namespace causes vmci_transport_dgram_allow() to return false and sendto() in test_dgram_vmci_inq() to fail with EINVAL (or EADDRNOTAVAIL when VMCI has no active context ID), aborting the test run instead of skipping.

[ ... ]

Quoted 14 lines
tools/testing/vsock/vsock_test_dgram.c new file +14 -0
+	if (needs & DGRAM_LOOPBACK) {
+		/* Binding a stream socket to CID_LOCAL probes registration of
+		 * the loopback transport without sending a packet to the host.
+		 */
+		local_fd = vsock_bind_try(VMADDR_CID_LOCAL, VMADDR_PORT_ANY, SOCK_STREAM);
+		if (local_fd >= 0) {
+			caps |= DGRAM_LOOPBACK;
+			close(local_fd);
+		} else if (errno != EADDRNOTAVAIL) {
+			check(local_fd, "probe loopback transport");
+		}
+	}
+	return caps;
+}

[ ... ]

Quoted 9 lines
tools/testing/vsock/vsock_test_dgram.c new file +9 -0
+void test_dgram_bound_lookup(const struct test_opts *opts)
+{
+	const int types[] = { SOCK_STREAM, SOCK_SEQPACKET };
+	unsigned int cid = local_cid();
+	size_t i;
+
+	for (i = 0; i < ARRAY_SIZE(types); i++) {
+		int listener = vsock_bind(VMADDR_CID_ANY, VMADDR_PORT_ANY, types[i]);
+		int stream = vsock_bind(VMADDR_CID_LOCAL, VMADDR_PORT_ANY, types[i]);

tools/testing/vsock/vsock_test_dgram.c line 735

Quoted 10 lines
tools/testing/vsock/vsock_test_dgram.c new file +10 -0
+		struct sockaddr_vm listen_addr = local_address(listener);
+		struct sockaddr_vm src = sock_name(stream, false);
+		struct sockaddr_vm dest;
+		int accepted, tx, rx;
+		char byte;
+
+		check(listen(listener, 1), "listen");
+		if (vsock_connect_fd(stream, listen_addr.svm_cid, listen_addr.svm_port))
+			fail("%s loopback connect: %s",
+			     types[i] == SOCK_STREAM ? "stream" : "seqpacket", strerror(errno));

[Severity: Medium]
Will the SOCK_SEQPACKET iteration in test_dgram_bound_lookup() and test_dgram_port_space() fail with ESOCKTNOSUPPORT when vsock_test runs in a local-mode vsock network namespace?

dgram_capabilities() probes DGRAM_LOOPBACK with SOCK_STREAM, which is allowed in both global and local namespace modes, whereas vsock_loopback_seqpacket_allow() rejects SOCK_SEQPACKET when the socket is not in global mode:

net/vmw_vsock/vsock_loopback.c:vsock_loopback_seqpacket_allow() {
	return vsock_net_mode_global(vsk);
}

As a result, running vsock_test in a local-mode namespace (such as the local-namespace cases in tools/testing/selftests/vsock/vmtest.sh) will pass the capability check and then abort in test_dgram_bound_lookup() and test_dgram_port_space() when connecting the SOCK_SEQPACKET socket to VMADDR_CID_LOCAL.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
#18 lore raw
In reply to Sergio Lopez
Quoted 138 lines · Sergio Lopez

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.

The Sashiko review of v2 also reported that a VMCI datagram bind in a local-mode network namespace creates a receive handle in VMCI's global port space, so a sender outside the namespace can deliver datagrams across the namespace isolation boundary. The issue predates this series and it's getting addressed in a separate patch:

https://lore.kernel.org/all/[email protected]

Within this series, local-mode namespaces bind datagrams only in the core table and never create a VMCI handle.

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. VMCI's host personality was exercised in a QEMU guest, including coexistence with virtio and self-send without loopback. A VMware VMCI guest device 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 v3:
- Preserve Hyper-V stream routing and prefer virtio for guest CID_HOST
datagrams (thanks to Sashiko AI bot).
- Bind explicit non-VMCI CIDs and local-mode sockets without a VMCI
handle, resolve wildcard destinations, and retain VMCI self routes
(thanks to Sashiko AI bot).
- Recheck bound addresses after receive takes the socket lock and set
TO_HOST only for forwarded guest sources (thanks to Sashiko AI bot).
- Probe negotiated virtio datagrams and numeric local CIDs before running dependent tests; cover wildcard and VMCI coexistence (thanks to Sashiko
AI bot).
- Link to v2: https://lore.kernel.org/r/[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 to vsock_test

 Documentation/admin-guide/sysctl/net.rst |    8 +-
 drivers/vhost/vsock.c                    |   51 +-
 include/linux/virtio_vsock.h             |    8 +-
 include/net/af_vsock.h                   |   31 +-
 include/uapi/linux/virtio_vsock.h        |   10 +
 net/vmw_vsock/af_vsock.c                 |  526 +++++++++++----
 net/vmw_vsock/hyperv_transport.c         |   11 +-
 net/vmw_vsock/virtio_transport.c         |  167 ++++-
 net/vmw_vsock/virtio_transport_common.c  |  172 ++++-
 net/vmw_vsock/vmci_transport.c           |  124 ++--
 net/vmw_vsock/vsock_bpf.c                |    3 +-
 net/vmw_vsock/vsock_loopback.c           |    2 +-
 tools/testing/vsock/Makefile             |    3 +-
 tools/testing/vsock/README               |   33 +-
 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   | 1061 ++++++++++++++++++++++++++++++
 tools/testing/vsock/vsock_test_dgram.h   |   30 +
 19 files changed, 2195 insertions(+), 292 deletions(-)
Quoted 8 lines

---
base-commit: 65ba8f34ec886ec72b2c07ddc8cf3d815c32869d
change-id: 20261027-vsock-dgram-support-683a4e0aebe7

Best regards,

Signature · 1 line
Sergio Lopez <[email protected]>

Hey Sergio,

Just a gentle heads up that net-next asks for 24hrs before reposting.

I'll start working my way through the series, sorry if my reviews come back spread out across a few days, not sure I'll have enough time in a single sitting.

Best,
Bobby

#19 lore raw
Quoted 143 lines · Bobby Eshleman
Quoted 142 lines · Sergio Lopez

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.

The Sashiko review of v2 also reported that a VMCI datagram bind in a local-mode network namespace creates a receive handle in VMCI's global port space, so a sender outside the namespace can deliver datagrams across the namespace isolation boundary. The issue predates this series and it's getting addressed in a separate patch:

https://lore.kernel.org/all/[email protected]

Within this series, local-mode namespaces bind datagrams only in the core table and never create a VMCI handle.

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. VMCI's host personality was exercised in a QEMU guest, including coexistence with virtio and self-send without loopback. A VMware VMCI guest device 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 v3:
- Preserve Hyper-V stream routing and prefer virtio for guest CID_HOST
datagrams (thanks to Sashiko AI bot).
- Bind explicit non-VMCI CIDs and local-mode sockets without a VMCI
handle, resolve wildcard destinations, and retain VMCI self routes
(thanks to Sashiko AI bot).
- Recheck bound addresses after receive takes the socket lock and set
TO_HOST only for forwarded guest sources (thanks to Sashiko AI bot).
- Probe negotiated virtio datagrams and numeric local CIDs before running dependent tests; cover wildcard and VMCI coexistence (thanks to Sashiko
AI bot).
- Link to v2: https://lore.kernel.org/r/[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 to vsock_test

 Documentation/admin-guide/sysctl/net.rst |    8 +-
 drivers/vhost/vsock.c                    |   51 +-
 include/linux/virtio_vsock.h             |    8 +-
 include/net/af_vsock.h                   |   31 +-
 include/uapi/linux/virtio_vsock.h        |   10 +
 net/vmw_vsock/af_vsock.c                 |  526 +++++++++++----
 net/vmw_vsock/hyperv_transport.c         |   11 +-
 net/vmw_vsock/virtio_transport.c         |  167 ++++-
 net/vmw_vsock/virtio_transport_common.c  |  172 ++++-
 net/vmw_vsock/vmci_transport.c           |  124 ++--
 net/vmw_vsock/vsock_bpf.c                |    3 +-
 net/vmw_vsock/vsock_loopback.c           |    2 +-
 tools/testing/vsock/Makefile             |    3 +-
 tools/testing/vsock/README               |   33 +-
 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   | 1061 ++++++++++++++++++++++++++++++
 tools/testing/vsock/vsock_test_dgram.h   |   30 +
 19 files changed, 2195 insertions(+), 292 deletions(-)

---
base-commit: 65ba8f34ec886ec72b2c07ddc8cf3d815c32869d
change-id: 20261027-vsock-dgram-support-683a4e0aebe7

Quoted 8 lines
Quoted 4 lines

Best regards,
--
Sergio Lopez <[email protected]>

Hey Sergio,

Just a gentle heads up that net-next asks for 24hrs before reposting.

Whoops, sorry!

Quoted 3 lines

I'll start working my way through the series, sorry if my reviews come back spread out across a few days, not sure I'll have enough time in a single sitting.

I was planning on sending a v4 fixing Sashiko's issues. Should I wait for your review first?

Thanks!
Sergio.

#20 lore raw
Quoted 160 lines · Sergio Lopez Pascual
Quoted 143 lines · Bobby Eshleman
Quoted 142 lines · Sergio Lopez

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.

The Sashiko review of v2 also reported that a VMCI datagram bind in a local-mode network namespace creates a receive handle in VMCI's global port space, so a sender outside the namespace can deliver datagrams across the namespace isolation boundary. The issue predates this series and it's getting addressed in a separate patch:

https://lore.kernel.org/all/[email protected]

Within this series, local-mode namespaces bind datagrams only in the core table and never create a VMCI handle.

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. VMCI's host personality was exercised in a QEMU guest, including coexistence with virtio and self-send without loopback. A VMware VMCI guest device 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 v3:
- Preserve Hyper-V stream routing and prefer virtio for guest CID_HOST
datagrams (thanks to Sashiko AI bot).
- Bind explicit non-VMCI CIDs and local-mode sockets without a VMCI
handle, resolve wildcard destinations, and retain VMCI self routes
(thanks to Sashiko AI bot).
- Recheck bound addresses after receive takes the socket lock and set
TO_HOST only for forwarded guest sources (thanks to Sashiko AI bot).
- Probe negotiated virtio datagrams and numeric local CIDs before running dependent tests; cover wildcard and VMCI coexistence (thanks to Sashiko
AI bot).
- Link to v2: https://lore.kernel.org/r/[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 to vsock_test

 Documentation/admin-guide/sysctl/net.rst |    8 +-
 drivers/vhost/vsock.c                    |   51 +-
 include/linux/virtio_vsock.h             |    8 +-
 include/net/af_vsock.h                   |   31 +-
 include/uapi/linux/virtio_vsock.h        |   10 +
 net/vmw_vsock/af_vsock.c                 |  526 +++++++++++----
 net/vmw_vsock/hyperv_transport.c         |   11 +-
 net/vmw_vsock/virtio_transport.c         |  167 ++++-
 net/vmw_vsock/virtio_transport_common.c  |  172 ++++-
 net/vmw_vsock/vmci_transport.c           |  124 ++--
 net/vmw_vsock/vsock_bpf.c                |    3 +-
 net/vmw_vsock/vsock_loopback.c           |    2 +-
 tools/testing/vsock/Makefile             |    3 +-
 tools/testing/vsock/README               |   33 +-
 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   | 1061 ++++++++++++++++++++++++++++++
 tools/testing/vsock/vsock_test_dgram.h   |   30 +
 19 files changed, 2195 insertions(+), 292 deletions(-)

---
base-commit: 65ba8f34ec886ec72b2c07ddc8cf3d815c32869d
change-id: 20261027-vsock-dgram-support-683a4e0aebe7

Quoted 8 lines
Quoted 4 lines

Best regards,
--
Sergio Lopez <[email protected]>

Hey Sergio,

Just a gentle heads up that net-next asks for 24hrs before reposting.

Whoops, sorry!

Quoted 3 lines

I'll start working my way through the series, sorry if my reviews come back spread out across a few days, not sure I'll have enough time in a single sitting.

Quoted 6 lines

I was planning on sending a v4 fixing Sashiko's issues. Should I wait for your review first?

Thanks!
Sergio.

Feel free to send without my review, I'm lagging behind!

Best,
Bobby