vsock: add datagram support to virtio and vhost
Subsystems (12)
v4 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 QEMU without loopback for self-send and with loopback for CID_HOST/CID_ANY routes. Active virtio and VMCI coexistence used a synthetic VMCI-like fallback. A VMware VMCI guest device and Hyper-V hardware were not exercised.
Targeted QEMU reproducers for v4 cover a filtered nonlinear skb, repeated peeks of an empty datagram, guest own-CID routing without loopback, and local/global namespace test behavior. The numeric own-CID routing reproducer used a synthetic guest-to-host transport matching virtio; the corresponding selftest has not run with a negotiated virtio guest device.
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 v4:
- Size vhost receive skbs from the declared payload before socket
filters run (thanks to Sashiko AI bot).
- Keep empty datagrams visible across repeated MSG_PEEK calls
(thanks to Sashiko AI bot).
- Reject CID_LOCAL and directional wildcard/self-CID datagrams when
loopback is absent, even with g2h_fallback=1 (thanks to Sashiko AI
bot).
- Preserve VMCI datagram routes to CID_HOST and CID_ANY, including
connected wildcard sends (thanks to Sashiko AI bot).
- Skip unsupported SOCK_SEQPACKET iterations in local-mode vsock
namespaces (thanks to Sashiko AI bot).
- Probe VMCI self-send before running its SIOCINQ test (thanks to
Sashiko AI bot).
- Link to v3: https://lore.kernel.org/r/[email protected]
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 | 83 +- 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 | 550 ++++++++++--- 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 | 42 +- tools/testing/vsock/util.c | 62 +- tools/testing/vsock/util.h | 10 + tools/testing/vsock/vsock_test.c | 189 ++++- tools/testing/vsock/vsock_test_dgram.c | 1230 ++++++++++++++++++++++++++++++ tools/testing/vsock/vsock_test_dgram.h | 34 + 19 files changed, 2428 insertions(+), 311 deletions(-)
---
base-commit: 65ba8f34ec886ec72b2c07ddc8cf3d815c32869d
change-id: 20261027-vsock-dgram-support-683a4e0aebe7
Best regards,
Signature · 1 line
Sergio Lopez <[email protected]>
RFC PATCH v4 1/8 vsock: expose the core port allocator to transports
The core port allocator is currently private to connection-oriented sockets. Virtio datagrams need the core bound table and port allocation, while VMCI datagrams must keep their own endpoint creation and port namespace.
Export vsock_bind_common() with the bound-table locking inside the helper, and use it for stream and SEQPACKET binds. Keep datagram binds delegated to the transport so a transport can opt into the core allocator without changing other transports' binding behavior.
Keep datagram and connection-oriented sockets in separate logical port spaces within the core bound table. Filter bind conflicts and bound-socket lookups by class, including wildcard CIDs and automatic port allocation, so datagram and stream/SEQPACKET sockets can share a CID and port. Stream and SEQPACKET sockets continue to conflict with each other.
--- include/net/af_vsock.h | 3 ++ net/vmw_vsock/af_vsock.c | 98 +++++++++++++++++++++++++++++++----------------- 2 files changed, 66 insertions(+), 35 deletions(-)
include/net/af_vsock.h +3 -0
@@ -220,6 +220,7 @@ void vsock_remove_pending(struct sock *listener, struct sock *pending);void vsock_enqueue_accept (struct sock *listener,struct sock *connected);void vsock_pending_to_accept (struct sock *listener,struct sock *pending);void vsock_insert_connected (struct vsock_sock *vsk); +int vsock_bind_common (struct vsock_sock *vsk,struct sockaddr_vm *addr);void vsock_remove_bound (struct vsock_sock *vsk);void vsock_remove_connected (struct vsock_sock *vsk);struct sock *vsock_find_bound_socket (struct sockaddr_vm *addr);
@@ -227,6 +228,8 @@ struct sock *vsock_find_connected_socket(struct sockaddr_vm *src, struct sockaddr_vm *dst);struct sock *vsock_find_bound_socket_net (struct sockaddr_vm *addr,struct net *net); +struct sock *vsock_find_bound_dgram_socket_net (struct sockaddr_vm *addr, +struct net *net);struct sock *vsock_find_connected_socket_net (struct sockaddr_vm *src,struct sockaddr_vm *dst,struct net *net);
net/vmw_vsock/af_vsock.c +63 -35
175 lines not on this page · raw message
Signature · 1 line
2.56.0
RFC PATCH v4 2/8 vsock: report datagram receive and send queue state
In reply to Sergio LopezDatagram receive queues hold whole messages, and transports may charge queued datagrams to the socket send buffer. Report the size of the next datagram payload for SIOCINQ under the receive-queue lock, so concurrent recvmsg() cannot free the skb during the query. Let transports override the length calculation to exclude headers such as VMCI's.
Report datagram write readiness only while the send buffer has space. Set the asynchronous no-space flag when it is full so freeing pending send buffers can wake poll waiters.
--- include/net/af_vsock.h | 2 ++ net/vmw_vsock/af_vsock.c | 32 ++++++++++++++++++++++++-------- net/vmw_vsock/vmci_transport.c | 9 +++++++++ 3 files changed, 35 insertions(+), 8 deletions(-)
include/net/af_vsock.h +2 -0
@@ -124,6 +124,8 @@ struct vsock_transport {int (*dgram_enqueue)(struct vsock_sock *,struct sockaddr_vm *,struct msghdr *,size_t len);bool (*dgram_allow)(struct vsock_sock *vsk,u32 cid,u32 port); +/* Optional payload length override, called with the receive queue locked. */ +size_t (*dgram_payload_len)(const struct sk_buff *skb);/* STREAM. */ /* TODO: stream_bind() */
net/vmw_vsock/af_vsock.c +24 -8
@@ -1359,16 +1359,20 @@ static __poll_t vsock_poll(struct file *file, struct socket *sock, mask |=vsock_poll_shutdown (sk, peer_shutdown);/* For datagram sockets we can read if there is something in -* the queue and write as long as the socket isn't shutdown for -* sending. +* the queue and write if the send buffer has space and the +* socket isn't shutdown for sending. */ if (!skb_queue_empty_lockless (&sk->sk_receive_queue) || (sk->sk_shutdown &RCV_SHUTDOWN )) { mask |=EPOLLIN |EPOLLRDNORM ; } -if (!(sk->sk_shutdown &SEND_SHUTDOWN )) - mask |=EPOLLOUT |EPOLLWRNORM |EPOLLWRBAND ; +if (!(sk->sk_shutdown &SEND_SHUTDOWN )) { +if (sock_writeable (sk)) + mask |=EPOLLOUT |EPOLLWRNORM |EPOLLWRBAND ; +else +sk_set_bit (SOCKWQ_ASYNC_NOSPACE , sk); + } }else if (sock_type_connectible (sk->sk_type)) {const struct vsock_transport *transport;
@@ -1632,10 +1636,22 @@ static int vsock_do_ioctl(struct socket *sock, unsigned int cmd,break ; } - n_bytes =vsock_stream_has_data (vsk); -if (n_bytes <0 ) { - ret = n_bytes; -break ; +if (sk->sk_type ==SOCK_DGRAM ) { +struct sk_buff *skb; + +spin_lock_bh (&sk->sk_receive_queue.lock); + skb =skb_peek (&sk->sk_receive_queue); +if (skb && vsk->transport->dgram_payload_len) + n_bytes = vsk->transport->dgram_payload_len (skb); +else + n_bytes = skb ? skb->len :0 ; +spin_unlock_bh (&sk->sk_receive_queue.lock); + }else { + n_bytes =vsock_stream_has_data (vsk); +if (n_bytes <0 ) { + ret = n_bytes; +break ; + } } ret =put_user (n_bytes, arg);break ;
net/vmw_vsock/vmci_transport.c +9 -0
@@ -1761,6 +1761,14 @@ static int vmci_transport_dgram_enqueue(return err -sizeof (*dg); } +static size_t vmci_transport_dgram_payload_len (const struct sk_buff *skb) +{ +if (skb->len <sizeof (struct vmci_datagram )) +return 0 ; + +return skb->len -sizeof (struct vmci_datagram ); +} +static int vmci_transport_dgram_dequeue (struct vsock_sock *vsk,struct msghdr *msg,size_t len,int flags)
@@ -2075,6 +2083,7 @@ static struct vsock_transport vmci_transport = { .connect = vmci_transport_connect, .dgram_bind = vmci_transport_dgram_bind, .dgram_dequeue = vmci_transport_dgram_dequeue, + .dgram_payload_len = vmci_transport_dgram_payload_len, .dgram_enqueue = vmci_transport_dgram_enqueue, .dgram_allow = vmci_transport_dgram_allow, .stream_dequeue = vmci_transport_stream_dequeue,
Signature · 1 line
2.56.0
RFC PATCH v4 3/8 vsock/virtio: implement the datagram data path
In reply to Sergio LopezAdd the datagram feature bit and packet type, and implement the common virtio transport operations needed to bind, send, and receive datagrams.
Send each message as an OP_RW packet with a maximum 64 KiB payload, including support for empty messages. Bound pending sends by the socket send buffer instead of connection-oriented peer credits. Use the socket's skb memory charge for datagram SIOCOUTQ, including empty messages, and release it through the normal skb ownership lifetime on completion, rejection, or queued drops.
Deliver incoming datagrams through the datagram bound-socket lookup without entering the connection state machine. Connection-oriented receive lookup excludes datagram sockets, so each class can use the same destination CID and port. Validate the transport and connected peer under the socket lock, including after shutdown, and silently discard undeliverable messages. Use the socket datagram queue for recvmsg(), including source addresses, peeking, and truncation reporting based on the payload length after socket filtering. Reject MSG_OOB and MSG_ERRQUEUE before accessing the receive queue. Reject datagram sockmap admission until its receive callbacks support it.
--- include/uapi/linux/virtio_vsock.h | 2 + net/vmw_vsock/af_vsock.c | 31 +++++--- net/vmw_vsock/virtio_transport_common.c | 132 ++++++++++++++++++++++++++++---- net/vmw_vsock/vsock_bpf.c | 3 +- 4 files changed, 140 insertions(+), 28 deletions(-)
include/uapi/linux/virtio_vsock.h +2 -0
@@ -40,6 +40,7 @@/* The feature bitmap for virtio vsock */ #define VIRTIO_VSOCK_F_SEQPACKET 1/* SOCK_SEQPACKET supported */ +#define VIRTIO_VSOCK_F_DGRAM 3/* SOCK_DGRAM supported */ struct virtio_vsock_config {__le64 guest_cid;
@@ -69,6 +70,7 @@ struct virtio_vsock_hdr {enum virtio_vsock_type {VIRTIO_VSOCK_TYPE_STREAM =1 ,VIRTIO_VSOCK_TYPE_SEQPACKET =2 , +VIRTIO_VSOCK_TYPE_DGRAM =3 , };enum virtio_vsock_op {
net/vmw_vsock/af_vsock.c +19 -12
@@ -1659,20 +1659,27 @@ static int vsock_do_ioctl(struct socket *sock, unsigned int cmd,case SIOCOUTQ : {ssize_t n_bytes; -if (!vsk->transport || !vsk->transport->unsent_bytes) { - ret = -EOPNOTSUPP ; -break ; - } +if (sk->sk_type ==SOCK_DGRAM ) { +/* Match UDP: report charged send memory, including skb +* overhead and empty datagrams. +*/ + n_bytes =sk_wmem_alloc_get (sk); + }else { +if (!vsk->transport || !vsk->transport->unsent_bytes) { + ret = -EOPNOTSUPP ; +break ; + } -if (sock_type_connectible (sk->sk_type) && sk->sk_state ==TCP_LISTEN ) { - ret = -EINVAL ; -break ; - } +if (sk->sk_state ==TCP_LISTEN ) { + ret = -EINVAL ; +break ; + } - n_bytes = vsk->transport->unsent_bytes (vsk); -if (n_bytes <0 ) { - ret = n_bytes; -break ; + n_bytes = vsk->transport->unsent_bytes (vsk); +if (n_bytes <0 ) { + ret = n_bytes; +break ; + } } ret =put_user (n_bytes, arg);
net/vmw_vsock/virtio_transport_common.c +117 -15
211 lines not on this page · raw message
net/vmw_vsock/vsock_bpf.c +2 -1
@@ -161,7 +161,8 @@ int vsock_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restoreif (!vsk->transport)return -ENODEV ; -if (!vsk->transport->read_skb) +/* Datagram receive is not implemented by the sockmap callbacks. */ +if (sk->sk_type ==SOCK_DGRAM || !vsk->transport->read_skb)return -EOPNOTSUPP ;vsock_bpf_check_needs_rebuild (psock->sk_proto);
Signature · 1 line
2.56.0
RFC PATCH v4 4/8 vsock/virtio: negotiate datagram support on the guest
In reply to Sergio LopezAdvertise VIRTIO_VSOCK_F_DGRAM and record whether the device negotiated it. Gate datagram socket initialization and destination checks on the negotiated feature and the transport's supported network namespace mode.
Perform device capability checks at socket creation without requiring a remote address, which is not yet known for an unconnected datagram socket. The common virtio implementation provides the datagram data path.
Discard received datagrams when the device has not negotiated support, including when a socket was initialized through another transport. Reject packets addressed to a different CID using the full 64-bit wire value before bound-socket lookup narrows it or matches a wildcard.
--- net/vmw_vsock/virtio_transport.c | 50 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport.c +47 -3
@@ -57,6 +57,7 @@ struct virtio_vsock {u32 guest_cid;bool seqpacket_allow; +bool dgram_allow;/* These fields are used only in tx path in function * 'virtio_transport_send_pkt_work()', so to save
@@ -547,6 +548,18 @@ bool virtio_transport_stream_allow(struct vsock_sock *vsk, u32 cid, u32 port)static bool virtio_transport_seqpacket_allow (struct vsock_sock *vsk,u32 remote_cid); +static bool virtio_transport_dgram_allow_check (struct vsock_sock *vsk, +u32 cid,u32 port); + +static int virtio_transport_socket_init (struct vsock_sock *vsk, +struct vsock_sock *psk) +{ +if (sk_vsock (vsk)->sk_type ==SOCK_DGRAM && + !virtio_transport_dgram_allow_check (vsk,0 ,0 )) +return -ESOCKTNOSUPPORT ; + +return virtio_transport_do_socket_init (vsk, psk); +}static bool virtio_transport_has_remote_cid (struct vsock_sock *vsk,u32 cid) {
@@ -561,7 +574,7 @@ static struct virtio_transport virtio_transport = { .get_local_cid = virtio_transport_get_local_cid, .has_remote_cid = virtio_transport_has_remote_cid, - .init = virtio_transport_do_socket_init, + .init = virtio_transport_socket_init, .destruct = virtio_transport_destruct, .release = virtio_transport_release, .connect = virtio_transport_connect,
@@ -571,7 +584,7 @@ static struct virtio_transport virtio_transport = { .dgram_bind = virtio_transport_dgram_bind, .dgram_dequeue = virtio_transport_dgram_dequeue, .dgram_enqueue = virtio_transport_dgram_enqueue, - .dgram_allow = virtio_transport_dgram_allow, + .dgram_allow = virtio_transport_dgram_allow_check, .stream_dequeue = virtio_transport_stream_dequeue, .stream_enqueue = virtio_transport_stream_enqueue,
@@ -629,6 +642,25 @@ virtio_transport_seqpacket_allow(struct vsock_sock *vsk, u32 remote_cid)return seqpacket_allow; } +static bool +virtio_transport_dgram_allow_check (struct vsock_sock *vsk,u32 cid,u32 port) +{ +struct virtio_vsock *vsock; +bool dgram_allow; + +if (!vsock_net_mode_global (vsk)) +return false; + + dgram_allow = false; +rcu_read_lock (); + vsock =rcu_dereference (the_virtio_vsock); +if (vsock) + dgram_allow = vsock->dgram_allow; +rcu_read_unlock (); + +return dgram_allow; +} +static void virtio_transport_rx_work (struct work_struct *work) { struct virtio_vsock *vsock =
@@ -677,6 +709,14 @@ static void virtio_transport_rx_work(struct work_struct *work)continue ; } +/* Check the full wire CID before socket lookup truncates it. */ +if (le16_to_cpu (hdr->type) ==VIRTIO_VSOCK_TYPE_DGRAM && + (!vsock->dgram_allow || +le64_to_cpu (hdr->dst_cid) != vsock->guest_cid)) { +kfree_skb (skb); +continue ; + } +if (payload_len)virtio_vsock_skb_put (skb, payload_len);
@@ -835,6 +875,9 @@ static int virtio_vsock_probe(struct virtio_device *vdev)if (virtio_has_feature (vdev,VIRTIO_VSOCK_F_SEQPACKET )) vsock->seqpacket_allow = true; +if (virtio_has_feature (vdev,VIRTIO_VSOCK_F_DGRAM )) + vsock->dgram_allow = true; + vdev->priv = vsock; ret =virtio_vsock_vqs_init (vsock);
@@ -932,7 +975,8 @@ static struct virtio_device_id id_table[] = { };static unsigned int features[] = { -VIRTIO_VSOCK_F_SEQPACKET +VIRTIO_VSOCK_F_SEQPACKET , +VIRTIO_VSOCK_F_DGRAM , };static struct virtio_driver virtio_vsock_driver = {
Signature · 1 line
2.56.0
RFC PATCH v4 5/8 vhost/vsock: negotiate datagram support with guests
In reply to Sergio LopezExpose VIRTIO_VSOCK_F_DGRAM in the vhost feature bitmap and retain the negotiated value for each guest. Keep datagrams disabled until the feature is negotiated.
Use a vhost-specific datagram destination check to look up the guest in the socket's network namespace and require its negotiated datagram feature. The common virtio implementation supplies the bind and message operations; destination validation remains in connect() and sendmsg(), where the remote CID is available.
Reject guest datagrams on ingress unless the receiving virtqueue acknowledged the feature. Complete rejected descriptors normally so the queue can continue processing subsequent traffic.
Validate the guest TX header before allocating the skb, and size the skb from its declared payload length rather than the descriptor chain. A guest can post a descriptor much larger than the packet it sends. The old allocation could then create a nonlinear skb whose data_len exceeds len; a socket filter trimming a datagram could make recvmsg() read beyond the skb data.
--- drivers/vhost/vsock.c | 66 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 22 deletions(-)
drivers/vhost/vsock.c +44 -22
@@ -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 )
@@ -360,7 +362,7 @@ static struct sk_buff * vhost_vsock_alloc_skb(struct vhost_virtqueue *vq, unsigned int out, unsigned int in) { - struct virtio_vsock_hdr *hdr; + struct virtio_vsock_hdr hdr;struct iov_iter iov_iter;struct sk_buff *skb;size_t payload_len;
@@ -378,34 +380,30 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq, len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM)return NULL ; -/* len contains both payload and hdr */ - skb =virtio_vsock_alloc_skb (len,GFP_KERNEL ); -if (!skb) -return NULL ; -iov_iter_init (&iov_iter,ITER_SOURCE , vq->iov, out, len); - hdr =virtio_vsock_hdr (skb); - nbytes =copy_from_iter (hdr,sizeof (*hdr), &iov_iter); -if (nbytes !=sizeof (*hdr)) { + nbytes =copy_from_iter (&hdr,sizeof (hdr), &iov_iter); +if (nbytes !=sizeof (hdr)) {vq_err (vq,"Expected %zu bytes for pkt->hdr, got %zu bytes\n" , -sizeof (*hdr), nbytes); -kfree_skb (skb); +sizeof (hdr), nbytes);return NULL ; } - payload_len =le32_to_cpu (hdr->len); + payload_len =le32_to_cpu (hdr.len); + +/* The pkt is too big or the length in the header is invalid */ +if (payload_len > len -sizeof (hdr)) +return NULL ; + + skb =virtio_vsock_alloc_skb (sizeof (hdr) + payload_len,GFP_KERNEL ); +if (!skb) +return NULL ; + *virtio_vsock_hdr (skb) = hdr;/* No payload */ if (!payload_len)return skb; -/* The pkt is too big or the length in the header is invalid */ -if (payload_len +sizeof (*hdr) > len) { -kfree_skb (skb); -return NULL ; - } -virtio_vsock_skb_put (skb, payload_len);if (skb_copy_datagram_from_iter (skb,0 , &iov_iter, payload_len)) {
@@ -436,6 +434,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 +460,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 +516,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 +591,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 +757,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 +911,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
RFC PATCH v4 6/8 vsock: allow datagrams to use directional transports
In reply to Sergio LopezUse the datagram fallback transport to own sockets that need transport- specific bind state. VMCI needs this ownership to create and destroy its receive handle. Without a fallback, initialize socket state through a directional transport or loopback; use loopback if a directional transport does not support datagrams. Hyper-V still rejects datagrams.
Share local, guest-to-host, host-to-guest, and parent-fallback route selection with stream and seqpacket sockets. Choose a capable directional transport for each datagram destination and use VMCI when none can serve the peer. Keep VMCI as the socket owner across connect and send calls so changing destinations cannot destroy its receive handle. Preserve parent routing in returned addresses and when connecting through the guest-to-host fallback. A host CID remains a loopback alias for connection-oriented sockets when the guest transport has no active device. For VMCI-owned datagrams in a global namespace, preserve VMCI routes to the host and to VMCI's own CID instead of using that alias. An active guest-to-host datagram transport can still serve CID_HOST when the source CID permits.
Resolve CID_ANY to VMCI's own CID for explicitly VMCI-bound datagram sockets, including on connect, so their source remains routable through VMCI even when loopback is loaded.
Treat CID_LOCAL as a loopback-only destination. When loopback is absent, return no route before considering guest-to-host routing or datagram fallback. This also rejects a directional datagram socket's wildcard self-destination, which resolves to CID_LOCAL, regardless of the g2h_fallback setting.
Without loopback, also reject a selected guest-to-host datagram route to its own numeric CID. Otherwise g2h_fallback lets connect() succeed while virtio_transport_send_pkt() rejects every send. Preserve VMCI's own-CID route and connection-oriented routing.
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.
Use sk_peek_offset() with __skb_recv_datagram() for the shared datagram queue. skb_recv_datagram() hard-codes offset zero; after a zero-length skb is peeked, that offset skips it on the next peek. The socket's default negative offset keeps repeated MSG_PEEK calls on the same datagram. Use the returned offset for copying and truncation lengths.
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 synthetic VMCI-like fallback reproduces the host-CID loopback route and VMCI-bound CID_ANY failure, then checks routes with VMCI alone and with inactive and active guest-to-host transports. A child-disappearance reproducer confirms parent fallback and direct-route recovery. A QEMU guest with a synthetic guest-to-host transport rejects numeric own-CID connect with g2h_fallback enabled while CID_HOST still works. 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 | 397 +++++++++++++++++++++++++------ 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, 478 insertions(+), 236 deletions(-)
Documentation/admin-guide/sysctl/net.rst +5 -3
@@ -586,9 +586,11 @@ reachable through the hypervisor (e.g. Nitro Enclaves) to address both using a single CID space, without requiring applications to set ``VMADDR_FLAG_TO_HOST``. -When the fallback is taken, ``VMADDR_FLAG_TO_HOST`` is automatically set on -the remote address so that userspace can determine the path via -``getpeername()``. +When a connect falls back, ``VMADDR_FLAG_TO_HOST`` is automatically set on +the connected peer address so that userspace can determine the selected path +via ``getpeername()``. A datagram send on an existing connection can also fall +back; in that case the flag is set only on the per-send destination. Later +sends can use H2G again if it takes ownership of the destination CID. Note: With this sysctl enabled, user space that attempts to talk to a guest CID which is not implemented by the H2G transport will create host vsock
drivers/vhost/vsock.c +1 -1
@@ -458,9 +458,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
@@ -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
@@ -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 +325 -72
572 lines not on this page · raw message
net/vmw_vsock/hyperv_transport.c +4 -7
@@ -443,6 +443,9 @@ static int hvs_sock_init(struct vsock_sock *vsk, struct vsock_sock *psk)struct hvsock *hvs;struct sock *sk =sk_vsock (vsk); +if (sk->sk_type ==SOCK_DGRAM ) +return -ESOCKTNOSUPPORT ; + hvs =kzalloc_obj (*hvs);if (!hvs)return -ENOMEM ;
@@ -556,13 +559,8 @@ static int hvs_dgram_bind(struct vsock_sock *vsk, struct sockaddr_vm *addr)return -EOPNOTSUPP ; } -static int hvs_dgram_dequeue (struct vsock_sock *vsk,struct msghdr *msg, -size_t len,int flags) -{ -return -EOPNOTSUPP ; -} -static int hvs_dgram_enqueue (struct vsock_sock *vsk, +const struct vsock_transport *transport,struct sockaddr_vm *remote,struct msghdr *msg,size_t dgram_len) {
@@ -871,7 +869,6 @@ static struct vsock_transport hvs_transport = { .shutdown = hvs_shutdown, .dgram_bind = hvs_dgram_bind, - .dgram_dequeue = hvs_dgram_dequeue, .dgram_enqueue = hvs_dgram_enqueue, .dgram_allow = hvs_dgram_allow,
net/vmw_vsock/virtio_transport.c +1 -1
@@ -582,9 +582,9 @@ static struct virtio_transport virtio_transport = { .cancel_pkt = virtio_transport_cancel_pkt, .dgram_bind = virtio_transport_dgram_bind, - .dgram_dequeue = virtio_transport_dgram_dequeue, .dgram_enqueue = virtio_transport_dgram_enqueue, .dgram_allow = virtio_transport_dgram_allow_check, + .dgram_transport_match = virtio_transport_dgram_transport_match, .stream_dequeue = virtio_transport_stream_dequeue, .stream_enqueue = virtio_transport_stream_enqueue,
net/vmw_vsock/virtio_transport_common.c +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
@@ -74,9 +74,9 @@ static struct virtio_transport loopback_transport = { .cancel_pkt = vsock_loopback_cancel_pkt, .dgram_bind = virtio_transport_dgram_bind, - .dgram_dequeue = virtio_transport_dgram_dequeue, .dgram_enqueue = virtio_transport_dgram_enqueue, .dgram_allow = virtio_transport_dgram_allow, + .dgram_transport_match = virtio_transport_dgram_transport_match, .stream_dequeue = virtio_transport_stream_dequeue, .stream_enqueue = virtio_transport_stream_enqueue,
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC v4 6/8] vsock: allow datagrams to use directional transports
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] vsock: inconsistent loopback source CID check in vsock_dgram_get_transport() - [Medium] vsock…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] vsock: inconsistent loopback source CID check in vsock_dgram_get_transport()
- [Medium] vsock: missing VMADDR_CID_ANY check in vsock_dgram_resolve_cid()
- [Medium] vsock: per-packet global vsock_register_mutex lock on datagram hot paths
--
commit 21473b8a230a0146483d0435292761949197fc7c
Author: Sergio Lopez <[email protected]>
vsock: allow datagrams to use directional transports
This commit enables AF_VSOCK datagram sockets to route packets across directional (G2H/H2G) and loopback transports while retaining VMCI as a fallback endpoint owner and unifying datagram receive queueing in the core.
Quoted 4 lines
net/vmw_vsock/af_vsock.c +0
-0
[ ... ]
Quoted 21 lines
net/vmw_vsock/af_vsock.c +17
-0@@ -724,6 +802,113 @@ int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk) }EXPORT_SYMBOL_GPL (vsock_assign_transport); +/** +* vsock_dgram_get_transport - select a transport for a datagram destination +* @vsk: socket whose datagram state will be used +* @remote_addr: destination address +* @vmci_self: wildcard destination resolved to the VMCI owner's local CID +* +* The caller must hold the socket lock and release the returned transport's +* module reference after use. A datagram fallback owns its socket's binding, +* but a directional transport takes precedence when it can reach the peer. +* Parent fallback sets VMADDR_FLAG_TO_HOST to preserve that route on connect. +* +* Return: a referenced transport or an ERR_PTR on failure. +*/ +const struct vsock_transport * +vsock_dgram_get_transport (struct vsock_sock *vsk, +struct sockaddr_vm *remote_addr,bool vmci_self) +{
[ ... ]
Quoted 4 lines
net/vmw_vsock/af_vsock.c +4
-0+if (!transport) + goto out; +if (owner->dgram_transport_match && + !owner->dgram_transport_match (transport)) {
net/vmw_vsock/af_vsock.c int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk) · line 881
Quoted 10 lines
net/vmw_vsock/af_vsock.c +10
-0+ err = -ESOCKTNOSUPPORT ; + goto out; + } +/* Loopback source addresses must route replies back to loopback. */ +if (shared && transport == transport_local && + vsk->local_addr.svm_cid !=VMADDR_CID_ANY && + !vsock_use_local_transport (vsk->local_addr.svm_cid)) { + err = -EADDRNOTAVAIL ; + goto out; + }
[Severity: Medium]
Does this loopback source check miss the VMCI exclusions that vsock_select_transport() applies?
For a VMCI-owned datagram socket in global mode, vsock_select_transport() routes both owner->get_local_cid() and VMADDR_CID_HOST away from transport_local even when vsock_use_local_transport() returns true. Checking only vsock_use_local_transport(vsk->local_addr.svm_cid) here lets a socket explicitly bound to VMADDR_CID_HOST (when no active G2H CID is present) or to VMCI's local CID (when VMCI is registered as transport_g2h) send or connect over transport_local, but replies to that source CID and connected-peer verification in virtio_transport_dgram_source_allow() resolve to vmci_transport instead of transport_local, causing connected loopback receivers to drop the packets.
[ ... ]
Quoted 18 lines
net/vmw_vsock/af_vsock.c +14
-0@@ -1461,6 +1660,31 @@ 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); +if (owner->dgram_fallback &&vsock_net_mode_global (vsk) && + vsk->local_addr.svm_cid == owner->get_local_cid ()) { + remote_addr->svm_cid = vsk->local_addr.svm_cid; + *vmci_self = true;
net/vmw_vsock/af_vsock.c static int vsock_read_skb(struct sock *sk, skb_read_actor_t read_actor) · line 1686
Quoted 10 lines
net/vmw_vsock/af_vsock.c +10
-0+ }else 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]
Should the first branch also check that vsk->local_addr.svm_cid != VMADDR_CID_ANY?
vmci_transport_init() registers vmci_transport as the datagram fallback at module load time even when neither VMCI guest nor host personality is active, in which case owner->get_local_cid() returns VMCI_INVALID_ID (VMADDR_CID_ANY). When a wildcard-bound or auto-bound socket calls connect() or sendmsg() with remote_addr->svm_cid == VMADDR_CID_ANY, vsk->local_addr.svm_cid == owner->get_local_cid() matches on VMADDR_CID_ANY, skipping the transport_local branch and failing with -EADDRNOTAVAIL even when vsock_loopback is loaded.
Quoted 3 lines
net/vmw_vsock/af_vsock.c +0
-0static int vsock_dgram_sendmsg (struct socket *sock,struct msghdr *msg,size_t len) {
[ ... ]
Quoted 48 lines
net/vmw_vsock/af_vsock.c +10
-25@@ -1493,42 +1717,34 @@ static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg,if (msg->msg_name &&vsock_addr_cast (msg->msg_name, msg->msg_namelen, &remote_addr) ==0 ) { -/* Ensure this address is of the right type and is a valid -* destination. -*/ - -if (remote_addr->svm_cid ==VMADDR_CID_ANY ) - remote_addr->svm_cid = transport->get_local_cid (); - -if (!vsock_addr_bound (remote_addr)) { - err = -EINVAL ; - goto out; - } + connected_addr = *remote_addr; + remote_addr = &connected_addr; }else if (sock->state ==SS_CONNECTED ) { - remote_addr = &vsk->remote_addr; - -if (remote_addr->svm_cid ==VMADDR_CID_ANY ) - remote_addr->svm_cid = transport->get_local_cid (); - -/* XXX Should connect() or this function ensure remote_addr is -* bound? -*/ -if (!vsock_addr_bound (&vsk->remote_addr)) { - err = -EINVAL ; - goto out; - } +/* A fallback must not pin subsequent sends to the host. */ + connected_addr = vsk->remote_addr; + remote_addr = &connected_addr; + vmci_self = vsk->dgram_peer_is_vmci_self; }else { err = -EINVAL ; goto out; } -if (!transport->dgram_allow (vsk, remote_addr->svm_cid, - remote_addr->svm_port)) { + err =vsock_dgram_resolve_cid (vsk, remote_addr, &vmci_self); +if (err) + goto out; +if (!vsock_addr_bound (remote_addr)) { err = -EINVAL ; goto out; }
net/vmw_vsock/af_vsock.c static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg, · line 1747
Quoted 10 lines
net/vmw_vsock/af_vsock.c +8
-1- err = transport->dgram_enqueue (vsk, remote_addr, msg, len); + transport =vsock_dgram_get_transport (vsk, remote_addr, vmci_self); +if (IS_ERR (transport)) { + err =PTR_ERR (transport); + goto out; + } + + err = transport->dgram_enqueue (vsk, transport, remote_addr, msg, len); +module_put (transport->module);
[Severity: Medium]
Can we avoid taking vsock_register_mutex and bouncing module refcounts on every datagram send and connected receive?
vsock_dgram_sendmsg() and virtio_transport_dgram_source_allow() (on every incoming datagram for a connected socket, while holding the virtqueue lock and lock_sock()) now call vsock_dgram_get_transport(), which acquires the global vsock_register_mutex, does try_module_get()/module_put(), and performs multiple vhost_vsock_get() lookups per packet, serializing datagram traffic across all sockets system-wide.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
RFC PATCH v4 7/8 vsock: fragment datagrams across receive buffers
In reply to Sergio LopezThe guest already uses page-sized RX allocations. A datagram can exceed one RX buffer. Growing every posted buffer to hold a full datagram would increase virtqueue memory use and make small queued datagrams retain large backing allocations. With delayed reads, a bounded receive queue could then fill after only a few messages.
Define START and EOM flags for datagram packets. Mark outgoing messages with both flags, and let vhost split a message across guest RX buffers, keeping START on the first fragment and EOM on the last. When a data packet meets a descriptor with no payload space, vhost returns that descriptor with zero used bytes. It drops datagrams but requeues stream and seqpacket data for a later buffer.
Reassemble consecutive fragments in the guest before tap or socket delivery. Bound a partial message to 64 fragments and 64 KiB. Discard incomplete messages on malformed packets, a new START, a peer mismatch, an intervening non-datagram packet, or virtqueue teardown. Require complete framing for host and loopback ingress.
A QEMU reproducer now delivers a 9 KiB datagram as one message and all 16 messages of a delayed-read burst. Paired host/guest selftests cover 64 KiB messages and the burst through both a userspace backend and the patched Linux vhost backend, with stream and seqpacket controls. A nested vhost-vsock guest posting header-only RX buffers also passes stream and seqpacket receive tests.
--- drivers/vhost/vsock.c | 15 +++++ include/uapi/linux/virtio_vsock.h | 8 +++ net/vmw_vsock/virtio_transport.c | 115 ++++++++++++++++++++++++++++++-- net/vmw_vsock/virtio_transport_common.c | 11 ++- 4 files changed, 141 insertions(+), 8 deletions(-)
drivers/vhost/vsock.c +15 -0
@@ -185,6 +185,19 @@ vhost_transport_do_send_pkt(struct vhost_vsock *vsock,* buffer, we split it using multiple buffers. */ if (payload_len > iov_len -sizeof (*hdr)) { +if (iov_len ==sizeof (*hdr)) { +/* Connected sockets must retry with the next buffer. */ +if (le16_to_cpu (hdr->type) == +VIRTIO_VSOCK_TYPE_DGRAM ) +kfree_skb (skb); +else +virtio_vsock_skb_queue_head ( + &vsock->send_pkt_queue, skb); +vhost_add_used (vq, head,0 ); + added = true; +continue ; + } + payload_len = iov_len -sizeof (*hdr);/* As we are copying pieces of large packet's buffer to
@@ -244,6 +257,8 @@ vhost_transport_do_send_pkt(struct vhost_vsock *vsock,*/ if (VIRTIO_VSOCK_SKB_CB (skb)->offset < skb->len) { hdr->flags |=cpu_to_le32 (flags_to_restore); +if (le16_to_cpu (hdr->type) ==VIRTIO_VSOCK_TYPE_DGRAM ) + hdr->flags &= ~cpu_to_le32 (VIRTIO_VSOCK_DGRAM_START );/* We are queueing the same skb to handle * the remaining bytes, and we want to deliver it
include/uapi/linux/virtio_vsock.h +8 -0
@@ -101,6 +101,14 @@ enum virtio_vsock_shutdown {enum virtio_vsock_rw {VIRTIO_VSOCK_SEQ_EOM =1 ,VIRTIO_VSOCK_SEQ_EOR =2 , +/* +* With VIRTIO_VSOCK_F_DGRAM, START marks the first RW packet of a +* datagram and EOM marks the last. Both are set for a datagram that +* fits in one packet. Fragments must be consecutive on the receive +* virtqueue. Each packet's len is its fragment length. +*/ +VIRTIO_VSOCK_DGRAM_EOM =VIRTIO_VSOCK_SEQ_EOM , +VIRTIO_VSOCK_DGRAM_START =4 , };#endif /* _UAPI_LINUX_VIRTIO_VSOCK_H */
net/vmw_vsock/virtio_transport.c +108 -7
180 lines not on this page · raw message
net/vmw_vsock/virtio_transport_common.c +10 -1
@@ -275,6 +275,13 @@ static struct sk_buff *virtio_transport_alloc_skb(struct virtio_vsock_pkt_info * hdr->flags |=cpu_to_le32 (VIRTIO_VSOCK_SEQ_EOR ); } } +if (info->type ==VIRTIO_VSOCK_TYPE_DGRAM ) { +struct virtio_vsock_hdr *hdr =virtio_vsock_hdr (skb); + +/* Vhost may split this message across guest RX buffers. */ + hdr->flags |=cpu_to_le32 (VIRTIO_VSOCK_DGRAM_START | +VIRTIO_VSOCK_DGRAM_EOM ); + }if (info->reply)virtio_vsock_skb_set_reply (skb);
@@ -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
RFC PATCH v4 8/8 vsock: add datagram tests to vsock_test
In reply to Sergio LopezCover local semantics, host/guest transfers, routing and queue accounting. Synchronize capability-based skips and document manual checks.
Probe VMCI and virtio/vhost independently for capability skips. VMCI can own a datagram socket while a directional transport serves its peer. Most local cases require loopback; they work without 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.
Distinguish VMCI module presence from a usable self-send route. A local-mode namespace may have a VMCI-owned datagram socket while VMCI rejects its self-send. Module presence alone would run the queue-length test and fail. With loopback absent, probe a wildcard datagram connect before enabling that test. Keep the module check for the no-loopback directional case, which must still exclude VMCI-owned sockets. On a QEMU VMCI host personality, the queue-length case passes in global mode and skips in local mode; with loopback loaded, both modes retain the existing loopback skip.
Exercise VMCI host datagram routes to CID_HOST and CID_ANY, including a connected wildcard send from an explicitly VMCI-CID-bound socket. This case runs in a global namespace with the VMCI host context, whether or not loopback is loaded. Update the README case IDs accordingly.
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.
Loopback rejects SEQPACKET connections in local-mode network namespaces. Skip only the SEQPACKET iterations of the connected-lookup and shared-port tests there, retaining their stream checks. Both tests pass in local and global namespaces; the global runs still exercise SEQPACKET.
Peek twice at an empty datagram, both alone and with a later message queued, to ensure MSG_PEEK does not skip it.
Add a no-loopback datagram case for CID_LOCAL and wildcard sends and connects, plus connect to the guest's numeric own CID. It requires a guest with negotiated virtio datagrams and skips when VMCI is present. Run it with g2h_fallback enabled to check self-connect rejection. Keep it last to preserve existing test IDs and document the configuration in the README.
--- tools/testing/vsock/Makefile | 3 +- tools/testing/vsock/README | 42 +- tools/testing/vsock/util.c | 62 +- tools/testing/vsock/util.h | 10 + tools/testing/vsock/vsock_test.c | 189 ++++- tools/testing/vsock/vsock_test_dgram.c | 1230 ++++++++++++++++++++++++++++++++ tools/testing/vsock/vsock_test_dgram.h | 34 + 7 files changed, 1538 insertions(+), 32 deletions(-)
tools/testing/vsock/Makefile +2 -1
@@ -1,7 +1,8 @@ # SPDX-License-Identifier: GPL-2.0-only all: test vsock_perf test: vsock_test vsock_diag_test vsock_uring_test -vsock_test: vsock_test.o vsock_test_zerocopy.o timeout.o control.o util.o msg_zerocopy_common.o +vsock_test: vsock_test.o vsock_test_zerocopy.o vsock_test_dgram.o \ + timeout.o control.o util.o msg_zerocopy_common.o vsock_diag_test: vsock_diag_test.o timeout.o control.o util.o vsock_perf: vsock_perf.o msg_zerocopy_common.o
tools/testing/vsock/README +40 -2
60 lines not on this page · raw message
tools/testing/vsock/util.c +35 -27
82 lines not on this page · raw message
tools/testing/vsock/util.h +10 -0
@@ -51,6 +51,10 @@ struct test_opts {enum test_mode mode;unsigned int peer_cid;unsigned int peer_port; +unsigned int bind_cid; +bool local; +bool to_host; +bool expect_to_host; };/* A test case definition. Test functions must print failures to stderr and
@@ -66,6 +70,12 @@ struct test_case {void (*run_server)(const struct test_opts *opts);bool skip; +bool local;/* Can run without a control connection */ + +/* Return a skip reason or NULL after the selection barrier. Checks +* that coordinate over the control channel must agree on both peers. +*/ +const char *(*check)(const struct test_opts *opts); };void init_signals (void );
tools/testing/vsock/vsock_test.c +187 -2
246 lines not on this page · raw message
tools/testing/vsock/vsock_test_dgram.c new file +1230 -0
1,230 lines not on this page · raw message
tools/testing/vsock/vsock_test_dgram.h new file +34 -0
@@ -0,0 +1,34 @@ +/* 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_no_loopback (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); +const char *vsock_dgram_check_vmci_host (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_no_loopback (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); +void test_dgram_vmci_host_routes (const struct test_opts *opts); + +#endif /* VSOCK_TEST_DGRAM_H */
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC v4 8/8] vsock: add datagram tests to vsock_test
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] vsock_test: vsock_dgram_check_vmci_host() skips when vhost_vsock is unloaded - [Medium]…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] vsock_test: vsock_dgram_check_vmci_host() skips when vhost_vsock is unloaded
- [Medium] vsock_test: local CID tests fail when VMCI coexists with vhost and loopback
--
commit 792ed67e2ec454b7d9e33e144a3b395a0eb9843c
Author: Sergio Lopez <[email protected]>
vsock: add datagram tests to vsock_test
This commit adds SOCK_DGRAM test cases to vsock_test covering local loopback semantics, host/guest transfers, transport routing, and queue accounting, along with capability-based skip checks and a --local mode.
Quoted 5 lines
tools/testing/vsock/vsock_test_dgram.c new file +0
-0
[ ... ]
tools/testing/vsock/vsock_test_dgram.c line 191
Quoted 12 lines
tools/testing/vsock/vsock_test_dgram.c new file +12
-0+static bool usable_local_cid (void ) +{ +unsigned int cid; +int fd, ret; + + fd =open ("/dev/vsock" ,O_RDONLY |O_CLOEXEC ); +if (fd <0 ) +return false; + ret =ioctl (fd,IOCTL_VM_SOCKETS_GET_LOCAL_CID , &cid); +close (fd); +return ret ==0 && cid !=VMADDR_CID_ANY && cid >VMADDR_CID_LOCAL ; +}
[Severity: Medium]
Can usable_local_cid() enable the local-CID loopback tests when VMCI owns VMADDR_CID_HOST?
When vhost_vsock, vsock_loopback, and vmw_vsock_vmci_transport are loaded together in a global namespace without a guest-to-host transport, IOCTL_VM_SOCKETS_GET_LOCAL_CID returns VMADDR_CID_HOST (from vhost_vsock), so vsock_dgram_check_local_cid() enables test_dgram_bound_lookup() and test_dgram_connected_cid(). However, VMCI is the datagram owner in a global namespace and its local CID is also VMADDR_CID_HOST, so vsock_select_transport() routes packets addressed to VMADDR_CID_HOST through vmci_transport instead of vsock_loopback.
As a result, test_dgram_bound_lookup() fails with EADDRNOTAVAIL when sending from a socket bound to VMADDR_CID_LOCAL to one bound to VMADDR_CID_HOST, and test_dgram_connected_cid() times out because the receiver connects to VMADDR_CID_HOST (selecting vmci_transport) while the packet arrives over loopback_transport and is dropped by virtio_transport_dgram_source_allow().
[ ... ]
Quoted 26 lines
tools/testing/vsock/vsock_test_dgram.c new file +26
-0+const char *vsock_dgram_check_vmci_host (const struct test_opts *opts) +{ +unsigned int caps =dgram_capabilities (opts,0 ); +unsigned int cid; +char mode[16 ]; +FILE *file; +int fd; + +if (!(caps &DGRAM_SOCKET )) +return "datagrams unavailable locally" ; +if (!(caps &DGRAM_VMCI_MODULE )) +return "requires VMCI datagram transport" ; + + file =fopen ("/proc/sys/net/vsock/ns_mode" ,"r" ); +if (!file) +return "requires a mounted vsock namespace sysctl" ; +if (!fgets (mode,sizeof (mode), file)) { +fclose (file); +return "cannot read vsock namespace mode" ; + } +fclose (file); +if (strncmp (mode,"global" ,6 )) +return "requires a global vsock namespace" ; + + fd =open ("/dev/vsock" ,O_RDONLY |O_CLOEXEC ); +if (fd <0 )
tools/testing/vsock/vsock_test_dgram.c line 388
Quoted 10 lines
tools/testing/vsock/vsock_test_dgram.c new file +10
-0+return "cannot open /dev/vsock" ; +if (ioctl (fd,IOCTL_VM_SOCKETS_GET_LOCAL_CID , &cid)) { +close (fd); +return "cannot read local CID" ; + } +close (fd); +if (cid !=VMADDR_CID_HOST ) +return "requires VMCI host CID without an active guest CID" ; +return NULL ; +}
[Severity: Medium]
Does checking IOCTL_VM_SOCKETS_GET_LOCAL_CID == VMADDR_CID_HOST skip test_dgram_vmci_host_routes() whenever vhost_vsock is unloaded and no /dev/vmci host context is active?
Loading vmw_vsock_vmci_transport only registers transport_dgram_fallback; vmci_register_vsock_callback() registers transport_h2g only while vmci_host_users() > 0. Because vsock_dev_do_ioctl() queries only transport_g2h, transport_h2g, and transport_local for IOCTL_VM_SOCKETS_GET_LOCAL_CID (never transport_dgram_fallback), the ioctl returns VMADDR_CID_ANY or VMADDR_CID_LOCAL when vhost_vsock is not loaded, causing vsock_dgram_check_vmci_host() to skip the test even though VMCI host datagram sockets can bind and send to VMADDR_CID_HOST.
Instead of requiring the ioctl to return VMADDR_CID_HOST, could this verify that the ioctl does not return a guest CID (> VMADDR_CID_HOST) and probe binding a datagram socket to VMADDR_CID_HOST?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
[email protected]
AI reviewSashiko has finished reviewing this 8-patch series:
Sashiko has finished reviewing this 8-patch series:
- Patches 1-5 and 7: no issues found.
- Patch 6 ("vsock: allow datagrams to use directional transports"): 3 potential issues (3 Medium)
- Patch 8 ("vsock: add datagram tests to vsock_test"): 2 potential issues (2 Medium)
Detailed reviews have been sent as separate replies to patches 6 and 8.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]