vsock: add datagram support to virtio and vhost
Subsystems (11)
cover vsock: add datagram support to virtio and vhost
This series implements SOCK_DGRAM support for virtio-vsock and vhost-vsock, allowing applications to exchange connectionless messages through the existing AF_VSOCK datagram API.
Support is negotiated through VIRTIO_VSOCK_F_DGRAM. Each datagram uses a single VIRTIO_VSOCK_TYPE_DGRAM packet, supporting payloads from zero bytes through 64 KiB. Pending sends are bounded by the socket send buffer rather than peer credits, with queue accounting and poll readiness reflecting pending output.
Datagrams use destination-based routing across guest-to-host, host-to-guest and loopback transports while retaining the socket's transport state. VMCI keeps priority as the dedicated datagram transport. Guest RX buffers accommodate complete messages when datagrams are negotiated, preserving message boundaries.
The patches first expose the core port allocator and add datagram queue reporting, then implement the common data path, guest and host feature negotiation, directional routing and RX buffer handling. The final patch extends vsock_test with local and host/guest datagram coverage, including peer filtering, route changes, message boundaries and queue ioctls. Unsupported cases skip automatically.
Tested using QEMU with a patch that exposed the VIRTIO_VSOCK_F_DGRAM feature:
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).
---
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: preserve host-to-guest datagram boundaries
vsock: add datagram tests to vsock_test
drivers/vhost/vsock.c | 37 +- include/linux/virtio_vsock.h | 6 + include/net/af_vsock.h | 14 +- include/uapi/linux/virtio_vsock.h | 2 + net/vmw_vsock/af_vsock.c | 206 ++++++-- net/vmw_vsock/hyperv_transport.c | 4 + net/vmw_vsock/virtio_transport.c | 71 ++- net/vmw_vsock/virtio_transport_common.c | 197 +++++++- net/vmw_vsock/vmci_transport.c | 10 + net/vmw_vsock/vsock_bpf.c | 3 +- net/vmw_vsock/vsock_loopback.c | 1 + tools/testing/vsock/Makefile | 3 +- tools/testing/vsock/README | 23 +- tools/testing/vsock/util.c | 62 +-- tools/testing/vsock/util.h | 10 + tools/testing/vsock/vsock_test.c | 154 +++++- tools/testing/vsock/vsock_test_dgram.c | 809 ++++++++++++++++++++++++++++++++ tools/testing/vsock/vsock_test_dgram.h | 26 + 18 files changed, 1545 insertions(+), 93 deletions(-)
---
base-commit: 65ba8f34ec886ec72b2c07ddc8cf3d815c32869d
change-id: 20261027-vsock-dgram-support-683a4e0aebe7
Best regards,
Signature · 1 line
Sergio Lopez <[email protected]>
RFC PATCH 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 same bound-socket lookup tables 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.
--- include/net/af_vsock.h | 1 + net/vmw_vsock/af_vsock.c | 31 ++++++++++++++++++++++--------- 2 files changed, 23 insertions(+), 9 deletions(-)
include/net/af_vsock.h +1 -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);
net/vmw_vsock/af_vsock.c +22 -9
@@ -832,8 +832,8 @@ static void vsock_pending_work(struct work_struct *work)/**** SOCKET OPERATIONS ****/ -static int __vsock_bind_connectible (struct vsock_sock *vsk, -struct sockaddr_vm *addr) +static int __vsock_bind_common (struct vsock_sock *vsk, +struct sockaddr_vm *addr) {struct net *net =sock_net (sk_vsock (vsk));struct sockaddr_vm new_addr;
@@ -888,11 +888,26 @@ static int __vsock_bind_connectible(struct vsock_sock *vsk,return 0 ; } -static int __vsock_bind_dgram (struct vsock_sock *vsk, -struct sockaddr_vm *addr) +/** +* vsock_bind_common - bind a socket using the core port allocator +* @vsk: socket to bind +* @addr: local address to bind to +* +* The caller must hold the socket lock. +* +* Return: 0 on success or a negative error code on failure. +*/ +int vsock_bind_common (struct vsock_sock *vsk,struct sockaddr_vm *addr) { -return vsk->transport->dgram_bind (vsk, addr); +int ret; + +spin_lock_bh (&vsock_table_lock); + ret =__vsock_bind_common (vsk, addr); +spin_unlock_bh (&vsock_table_lock); + +return ret; } +EXPORT_SYMBOL_GPL (vsock_bind_common);static int __vsock_bind (struct sock *sk,struct sockaddr_vm *addr) {
@@ -914,13 +929,11 @@ static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr)switch (sk->sk_socket->type) {case SOCK_STREAM :case SOCK_SEQPACKET : -spin_lock_bh (&vsock_table_lock); - retval =__vsock_bind_connectible (vsk, addr); -spin_unlock_bh (&vsock_table_lock); + retval =vsock_bind_common (vsk, addr);break ;case SOCK_DGRAM : - retval =__vsock_bind_dgram (vsk, addr); + retval = vsk->transport->dgram_bind (vsk, addr);break ;default :
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC 1/8] vsock: expose the core port allocator to transports
Sashiko has reviewed this patch and found no issues.
Quoted 11 lines
The core port allocator is currently private to connection-oriented sockets. Virtio datagrams need the same bound-socket lookup tables 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.Signed-off-by Sergio Lopez <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
RFC PATCH 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
@@ -1344,16 +1344,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;
@@ -1617,10 +1621,22 @@ static int vsock_do_ioctl(struct socket *sock, unsigned int cmd,break ; } - n_bytes =vsock_stream_has_data (vsk); -if (n_bytes <0 ) { - ret = n_bytes; -break ; +if (sk->sk_type ==SOCK_DGRAM ) { +struct sk_buff *skb; + +spin_lock_bh (&sk->sk_receive_queue.lock); + skb =skb_peek (&sk->sk_receive_queue); +if (skb && vsk->transport->dgram_payload_len) + n_bytes = vsk->transport->dgram_payload_len (skb); +else + n_bytes = skb ? skb->len :0 ; +spin_unlock_bh (&sk->sk_receive_queue.lock); + }else { + n_bytes =vsock_stream_has_data (vsk); +if (n_bytes <0 ) { + ret = n_bytes; +break ; + } } ret =put_user (n_bytes, arg);break ;
net/vmw_vsock/vmci_transport.c +9 -0
@@ -1761,6 +1761,14 @@ static int vmci_transport_dgram_enqueue(return err -sizeof (*dg); } +static size_t vmci_transport_dgram_payload_len (const struct sk_buff *skb) +{ +if (skb->len <sizeof (struct vmci_datagram )) +return 0 ; + +return skb->len -sizeof (struct vmci_datagram ); +} +static int vmci_transport_dgram_dequeue (struct vsock_sock *vsk,struct msghdr *msg,size_t len,int flags)
@@ -2075,6 +2083,7 @@ static struct vsock_transport vmci_transport = { .connect = vmci_transport_connect, .dgram_bind = vmci_transport_dgram_bind, .dgram_dequeue = vmci_transport_dgram_dequeue, + .dgram_payload_len = vmci_transport_dgram_payload_len, .dgram_enqueue = vmci_transport_dgram_enqueue, .dgram_allow = vmci_transport_dgram_allow, .stream_dequeue = vmci_transport_stream_dequeue,
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC 2/8] vsock: report datagram receive and send queue state
Sashiko has reviewed this patch and found no issues.
Quoted 7 lines
Datagram receive queues hold whole messages, and transports may charge queued datagrams to the socket send buffer. Report the size of the next datagram payload for SIOCINQ under the receive-queue lock, so concurrent
recvmsg()cannot free the skb during the query. Let transports override the length calculation to exclude headers such as VMCI's.
Quoted 5 lines
Report datagram write readiness only while the send buffer has space. Set the asynchronous no-space flag when it is full so freeing pending send buffers can wake poll waiters.
Signed-off-by Sergio Lopez <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
RFC PATCH 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. Charge pending payload bytes before handing the skb to the backend and release that charge exactly once on completion, rejection, or queued drops.
Deliver incoming datagrams through the bound table without entering the connection state machine. 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/linux/virtio_vsock.h | 2 + include/uapi/linux/virtio_vsock.h | 2 + net/vmw_vsock/virtio_transport_common.c | 150 +++++++++++++++++++++++++++++--- net/vmw_vsock/vsock_bpf.c | 3 +- 4 files changed, 142 insertions(+), 15 deletions(-)
include/linux/virtio_vsock.h +2 -0
@@ -12,6 +12,8 @@struct virtio_vsock_skb_cb {bool reply;bool tap_delivered; +/* Datagram payload is still charged to bytes_unsent. */ +bool tx_accounted;u32 offset; };
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/virtio_transport_common.c +136 -14
238 lines not on this page · raw message
net/vmw_vsock/vsock_bpf.c +2 -1
@@ -161,7 +161,8 @@ int vsock_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restoreif (!vsk->transport)return -ENODEV ; -if (!vsk->transport->read_skb) +/* Datagram receive is not implemented by the sockmap callbacks. */ +if (sk->sk_type ==SOCK_DGRAM || !vsk->transport->read_skb)return -EOPNOTSUPP ;vsock_bpf_check_needs_rebuild (psock->sk_proto);
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC 3/8] vsock/virtio: implement the datagram data path
Sashiko has reviewed this patch and found no issues.
Quoted 20 lines
Add the datagram feature bit and packet type, and implement the common virtio transport operations needed to bind, send, and receive datagrams.
Send each message as an
OP_RWpacket with a maximum 64 KiB payload, including support for empty messages. Bound pending sends by the socket send buffer instead of connection-oriented peer credits. Charge pending payload bytes before handing the skb to the backend and release that charge exactly once on completion, rejection, or queued drops.Deliver incoming datagrams through the bound table without entering the connection state machine. Validate the transport and connected peer under the socket lock, including after shutdown, and silently discard undeliverable messages. Use the socket datagram queue for
recvmsg(), including source addresses, peeking, and truncation reporting based on the payload length after socket filtering. RejectMSG_OOBandMSG_ERRQUEUEbefore accessing the receive queue. Reject datagram sockmap admission until its receive callbacks support it.
Quoted 1 line
Signed-off-by Sergio Lopez <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Bobby Eshleman
reviewerRe: [PATCH RFC 3/8] vsock/virtio: implement the datagram data path
In reply to Sergio LopezQuoted 297 lines · 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_RWpacket with a maximum 64 KiB payload, including support for empty messages. Bound pending sends by the socket send buffer instead of connection-oriented peer credits. Charge pending payload bytes before handing the skb to the backend and release that charge exactly once on completion, rejection, or queued drops.Deliver incoming datagrams through the bound table without entering the connection state machine. Validate the transport and connected peer under the socket lock, including after shutdown, and silently discard undeliverable messages. Use the socket datagram queue for
recvmsg(), including source addresses, peeking, and truncation reporting based on the payload length after socket filtering. RejectMSG_OOBandMSG_ERRQUEUEbefore accessing the receive queue. Reject datagram sockmap admission until its receive callbacks support it.Signed-off-by Sergio Lopez <[email protected]>--- include/linux/virtio_vsock.h | 2 + include/uapi/linux/virtio_vsock.h | 2 + net/vmw_vsock/virtio_transport_common.c | 150 +++++++++++++++++++++++++++++--- net/vmw_vsock/vsock_bpf.c | 3 +- 4 files changed, 142 insertions(+), 15 deletions(-)include/linux/virtio_vsock.h +2
-0@@ -12,6 +12,8 @@struct virtio_vsock_skb_cb {bool reply;bool tap_delivered; +/* Datagram payload is still charged to bytes_unsent. */ +bool tx_accounted;u32 offset; };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/virtio_transport_common.c +120
-14@@ -204,10 +204,18 @@ static u16 virtio_transport_get_type(struct sock *sk) {if (sk->sk_type ==SOCK_STREAM )return VIRTIO_VSOCK_TYPE_STREAM ; +else if (sk->sk_type ==SOCK_DGRAM ) +return VIRTIO_VSOCK_TYPE_DGRAM ;else return VIRTIO_VSOCK_TYPE_SEQPACKET ; } +static void virtio_transport_dgram_skb_destructor (struct sk_buff *skb) +{ +virtio_transport_consume_skb_sent (skb, false); +sock_wfree (skb); +} +/* Returns new sk_buff on success, otherwise returns NULL. */ static struct sk_buff *virtio_transport_alloc_skb(struct virtio_vsock_pkt_info *info, size_t payload_len,@@ -303,6 +311,7 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,u32 src_cid, src_port, dst_cid, dst_port;const struct virtio_transport *t_ops;struct iov_iter_state msg_iter_state; +struct sock *sk =sk_vsock (vsk);struct virtio_vsock_sock *vvs;struct ubuf_info *uarg =NULL ;u32 pkt_len = info->pkt_len;@@ -329,12 +338,24 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk, vvs = vsk->trans; -/* virtio_transport_get_credit might return less than pkt_len credit */ - pkt_len =virtio_transport_get_credit (vvs, pkt_len); +if (info->type ==VIRTIO_VSOCK_TYPE_DGRAM ) { +if (pkt_len >VIRTIO_VSOCK_MAX_PKT_BUF_SIZE ) +return -EMSGSIZE ; -/* Do not send zero length OP_RW pkt */ -if (pkt_len ==0 && info->op ==VIRTIO_VSOCK_OP_RW ) -return pkt_len; +/* Datagram sends are serialized by the socket lock. */ +if (sk_wmem_alloc_get (sk) >=READ_ONCE (sk->sk_sndbuf)) { +sk_set_bit (SOCKWQ_ASYNC_NOSPACE , sk); +set_bit (SOCK_NOSPACE , &sk->sk_socket->flags); +return -EAGAIN ; + } + }else { +/* virtio_transport_get_credit might return less than pkt_len credit */ + pkt_len =virtio_transport_get_credit (vvs, pkt_len); + +/* Do not send zero length OP_RW pkt */ +if (pkt_len ==0 && info->op ==VIRTIO_VSOCK_OP_RW ) +return pkt_len; + }if (info->msg && (info->msg->msg_flags &MSG_ZEROCOPY )) {/* If 'info->msg' is not NULL, this is only VIRTIO_VSOCK_OP_RW. @@ -348,7 +369,8 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk, uarg =msg_zerocopy_realloc (sk_vsock (vsk), pkt_len,NULL , false);if (!uarg) { -virtio_transport_put_credit (vvs, pkt_len); +if (info->type !=VIRTIO_VSOCK_TYPE_DGRAM ) +virtio_transport_put_credit (vvs, pkt_len);return -ENOMEM ; }@@ -393,7 +415,15 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,break ; } -virtio_transport_inc_tx_pkt (vvs, skb); +if (info->type ==VIRTIO_VSOCK_TYPE_DGRAM ) { +spin_lock_bh (&vvs->tx_lock); + vvs->bytes_unsent += skb_len; +spin_unlock_bh (&vvs->tx_lock); +VIRTIO_VSOCK_SKB_CB (skb)->tx_accounted = true; + skb->destructor = virtio_transport_dgram_skb_destructor; + }else { +virtio_transport_inc_tx_pkt (vvs, skb); + } ret = t_ops->send_pkt (skb, info->net);if (ret <0 )@@ -416,7 +446,8 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,if (info->msg && ret <0 )iov_iter_restore (&info->msg->msg_iter, &msg_iter_state); -virtio_transport_put_credit (vvs, rest_len); +if (info->type !=VIRTIO_VSOCK_TYPE_DGRAM ) +virtio_transport_put_credit (vvs, rest_len);/* msg_zerocopy_realloc() initializes the ubuf_info refcnt to 1. * skb_zcopy_set() increases it for each skb, so we can drop that @@ -620,7 +651,9 @@ void virtio_transport_consume_skb_sent(struct sk_buff *skb, bool consume) {struct sock *s = skb->sk; -if (s && skb->len) { +if (s && skb->len && + (s->sk_type !=SOCK_DGRAM || +VIRTIO_VSOCK_SKB_CB (skb)->tx_accounted)) {struct vsock_sock *vs =vsock_sk (s);struct virtio_vsock_sock *vvs;@@ -631,6 +664,9 @@ void virtio_transport_consume_skb_sent(struct sk_buff *skb, bool consume)spin_unlock_bh (&vvs->tx_lock); } +if (s && s->sk_type ==SOCK_DGRAM ) +VIRTIO_VSOCK_SKB_CB (skb)->tx_accounted = false; +if (consume)consume_skb (skb); }@@ -999,7 +1035,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; +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; + + hdr =virtio_vsock_hdr (skb); + payload_len = skb->len; + +if (payload_len > len) { + payload_len = len; + msg->msg_flags |=MSG_TRUNC ; + } + + err =skb_copy_datagram_iter (skb,0 , &msg->msg_iter, payload_len); +if (err) + goto out; + +if (msg->msg_name) { + DECLARE_SOCKADDR(struct sockaddr_vm *, vm_addr, msg->msg_name); + +vsock_addr_init (vm_addr,le64_to_cpu (hdr->src_cid), +le32_to_cpu (hdr->src_port)); + msg->msg_namelen =sizeof (*vm_addr); + } + +if (flags &MSG_TRUNC ) + err = skb->len; +else + err = payload_len; + +out: +skb_free_datagram (sk, skb); +return err; }EXPORT_SYMBOL_GPL (virtio_transport_dgram_dequeue);@@ -1202,13 +1278,13 @@ EXPORT_SYMBOL_GPL(virtio_transport_stream_is_active);int virtio_transport_dgram_bind (struct vsock_sock *vsk,struct sockaddr_vm *addr) { -return -EOPNOTSUPP ; +return vsock_bind_common (vsk, addr); }EXPORT_SYMBOL_GPL (virtio_transport_dgram_bind);bool virtio_transport_dgram_allow (struct vsock_sock *vsk,u32 cid,u32 port) { -return false; +return true; }EXPORT_SYMBOL_GPL (virtio_transport_dgram_allow);@@ -1246,7 +1322,17 @@ virtio_transport_dgram_enqueue(struct vsock_sock *vsk, struct msghdr *msg, size_t dgram_len) { -return -EOPNOTSUPP ; +struct virtio_vsock_pkt_info info = { + .op =VIRTIO_VSOCK_OP_RW , + .msg = msg, + .pkt_len = dgram_len, + .vsk = vsk, + .remote_cid = remote_addr->svm_cid, + .remote_port = remote_addr->svm_port, + .net =sock_net (sk_vsock (vsk)), + }; + +return virtio_transport_send_pkt_info (vsk, &info); }EXPORT_SYMBOL_GPL (virtio_transport_dgram_enqueue);@@ -1771,7 +1857,8 @@ virtio_transport_recv_listen(struct sock *sk, struct sk_buff *skb,static bool virtio_transport_valid_type (u16 type) {return (type ==VIRTIO_VSOCK_TYPE_STREAM ) || - (type ==VIRTIO_VSOCK_TYPE_SEQPACKET ); + (type ==VIRTIO_VSOCK_TYPE_SEQPACKET ) || + (type ==VIRTIO_VSOCK_TYPE_DGRAM ); }/* We are under the virtio-vsock's vsock->rx_lock or vhost-vsock's vq->mutex @@ -1805,6 +1892,41 @@ 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_socket_net (&dst, net); + +if (!sk || sk->sk_type !=SOCK_DGRAM ) { +if (sk) +sock_put (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 1923
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);
I'd love to see the receive path be free of lock_sock the way that UDP does.
My understanding from udp is that we can avoid the race with close/SOCK_DEAD by using SOCK_RCU_FREE and putting this receive in an rcu read-side section. I think this makes sk->sk_socket->state safe inside the RCU read side section too.
I think vsock_check_source() in a later patch goes away with virtio_transport_dgram_source_allow(), so I think maybe that patch should come before this?
Best,
Bobby
Quoted 27 lines
net/vmw_vsock/virtio_transport_common.c +6
-0+ +release_sock (sk); +sock_put (sk); +return ; + } +/* The socket must be in connected or bound table * otherwise send reset back */ 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 · 2 lines
-- 2.56.0
Bobby Eshleman
reviewerRe: [PATCH RFC 3/8] vsock/virtio: implement the datagram data path
Quoted 317 lines · Bobby Eshleman
Quoted 297 lines · 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_RWpacket with a maximum 64 KiB payload, including support for empty messages. Bound pending sends by the socket send buffer instead of connection-oriented peer credits. Charge pending payload bytes before handing the skb to the backend and release that charge exactly once on completion, rejection, or queued drops.Deliver incoming datagrams through the bound table without entering the connection state machine. Validate the transport and connected peer under the socket lock, including after shutdown, and silently discard undeliverable messages. Use the socket datagram queue for
recvmsg(), including source addresses, peeking, and truncation reporting based on the payload length after socket filtering. RejectMSG_OOBandMSG_ERRQUEUEbefore accessing the receive queue. Reject datagram sockmap admission until its receive callbacks support it.Signed-off-by Sergio Lopez <[email protected]>--- include/linux/virtio_vsock.h | 2 + include/uapi/linux/virtio_vsock.h | 2 + net/vmw_vsock/virtio_transport_common.c | 150 +++++++++++++++++++++++++++++--- net/vmw_vsock/vsock_bpf.c | 3 +- 4 files changed, 142 insertions(+), 15 deletions(-)include/linux/virtio_vsock.h +2
-0@@ -12,6 +12,8 @@struct virtio_vsock_skb_cb {bool reply;bool tap_delivered; +/* Datagram payload is still charged to bytes_unsent. */ +bool tx_accounted;u32 offset; };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/virtio_transport_common.c +120
-14@@ -204,10 +204,18 @@ static u16 virtio_transport_get_type(struct sock *sk) {if (sk->sk_type ==SOCK_STREAM )return VIRTIO_VSOCK_TYPE_STREAM ; +else if (sk->sk_type ==SOCK_DGRAM ) +return VIRTIO_VSOCK_TYPE_DGRAM ;else return VIRTIO_VSOCK_TYPE_SEQPACKET ; } +static void virtio_transport_dgram_skb_destructor (struct sk_buff *skb) +{ +virtio_transport_consume_skb_sent (skb, false); +sock_wfree (skb); +} +/* Returns new sk_buff on success, otherwise returns NULL. */ static struct sk_buff *virtio_transport_alloc_skb(struct virtio_vsock_pkt_info *info, size_t payload_len,@@ -303,6 +311,7 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,u32 src_cid, src_port, dst_cid, dst_port;const struct virtio_transport *t_ops;struct iov_iter_state msg_iter_state; +struct sock *sk =sk_vsock (vsk);struct virtio_vsock_sock *vvs;struct ubuf_info *uarg =NULL ;u32 pkt_len = info->pkt_len;@@ -329,12 +338,24 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk, vvs = vsk->trans; -/* virtio_transport_get_credit might return less than pkt_len credit */ - pkt_len =virtio_transport_get_credit (vvs, pkt_len); +if (info->type ==VIRTIO_VSOCK_TYPE_DGRAM ) { +if (pkt_len >VIRTIO_VSOCK_MAX_PKT_BUF_SIZE ) +return -EMSGSIZE ; -/* Do not send zero length OP_RW pkt */ -if (pkt_len ==0 && info->op ==VIRTIO_VSOCK_OP_RW ) -return pkt_len; +/* Datagram sends are serialized by the socket lock. */ +if (sk_wmem_alloc_get (sk) >=READ_ONCE (sk->sk_sndbuf)) { +sk_set_bit (SOCKWQ_ASYNC_NOSPACE , sk); +set_bit (SOCK_NOSPACE , &sk->sk_socket->flags); +return -EAGAIN ; + } + }else { +/* virtio_transport_get_credit might return less than pkt_len credit */ + pkt_len =virtio_transport_get_credit (vvs, pkt_len); + +/* Do not send zero length OP_RW pkt */ +if (pkt_len ==0 && info->op ==VIRTIO_VSOCK_OP_RW ) +return pkt_len; + }if (info->msg && (info->msg->msg_flags &MSG_ZEROCOPY )) {/* If 'info->msg' is not NULL, this is only VIRTIO_VSOCK_OP_RW. @@ -348,7 +369,8 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk, uarg =msg_zerocopy_realloc (sk_vsock (vsk), pkt_len,NULL , false);if (!uarg) { -virtio_transport_put_credit (vvs, pkt_len); +if (info->type !=VIRTIO_VSOCK_TYPE_DGRAM ) +virtio_transport_put_credit (vvs, pkt_len);return -ENOMEM ; }@@ -393,7 +415,15 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,break ; } -virtio_transport_inc_tx_pkt (vvs, skb); +if (info->type ==VIRTIO_VSOCK_TYPE_DGRAM ) { +spin_lock_bh (&vvs->tx_lock); + vvs->bytes_unsent += skb_len; +spin_unlock_bh (&vvs->tx_lock); +VIRTIO_VSOCK_SKB_CB (skb)->tx_accounted = true; + skb->destructor = virtio_transport_dgram_skb_destructor; + }else { +virtio_transport_inc_tx_pkt (vvs, skb); + } ret = t_ops->send_pkt (skb, info->net);if (ret <0 )@@ -416,7 +446,8 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,if (info->msg && ret <0 )iov_iter_restore (&info->msg->msg_iter, &msg_iter_state); -virtio_transport_put_credit (vvs, rest_len); +if (info->type !=VIRTIO_VSOCK_TYPE_DGRAM ) +virtio_transport_put_credit (vvs, rest_len);/* msg_zerocopy_realloc() initializes the ubuf_info refcnt to 1. * skb_zcopy_set() increases it for each skb, so we can drop that @@ -620,7 +651,9 @@ void virtio_transport_consume_skb_sent(struct sk_buff *skb, bool consume) {struct sock *s = skb->sk; -if (s && skb->len) { +if (s && skb->len && + (s->sk_type !=SOCK_DGRAM || +VIRTIO_VSOCK_SKB_CB (skb)->tx_accounted)) {struct vsock_sock *vs =vsock_sk (s);struct virtio_vsock_sock *vvs;@@ -631,6 +664,9 @@ void virtio_transport_consume_skb_sent(struct sk_buff *skb, bool consume)spin_unlock_bh (&vvs->tx_lock); } +if (s && s->sk_type ==SOCK_DGRAM ) +VIRTIO_VSOCK_SKB_CB (skb)->tx_accounted = false; +if (consume)consume_skb (skb); }@@ -999,7 +1035,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; +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; + + hdr =virtio_vsock_hdr (skb); + payload_len = skb->len; + +if (payload_len > len) { + payload_len = len; + msg->msg_flags |=MSG_TRUNC ; + } + + err =skb_copy_datagram_iter (skb,0 , &msg->msg_iter, payload_len); +if (err) + goto out; + +if (msg->msg_name) { + DECLARE_SOCKADDR(struct sockaddr_vm *, vm_addr, msg->msg_name); + +vsock_addr_init (vm_addr,le64_to_cpu (hdr->src_cid), +le32_to_cpu (hdr->src_port)); + msg->msg_namelen =sizeof (*vm_addr); + } + +if (flags &MSG_TRUNC ) + err = skb->len; +else + err = payload_len; + +out: +skb_free_datagram (sk, skb); +return err; }EXPORT_SYMBOL_GPL (virtio_transport_dgram_dequeue);@@ -1202,13 +1278,13 @@ EXPORT_SYMBOL_GPL(virtio_transport_stream_is_active);int virtio_transport_dgram_bind (struct vsock_sock *vsk,struct sockaddr_vm *addr) { -return -EOPNOTSUPP ; +return vsock_bind_common (vsk, addr); }EXPORT_SYMBOL_GPL (virtio_transport_dgram_bind);bool virtio_transport_dgram_allow (struct vsock_sock *vsk,u32 cid,u32 port) { -return false; +return true; }EXPORT_SYMBOL_GPL (virtio_transport_dgram_allow);@@ -1246,7 +1322,17 @@ virtio_transport_dgram_enqueue(struct vsock_sock *vsk, struct msghdr *msg, size_t dgram_len) { -return -EOPNOTSUPP ; +struct virtio_vsock_pkt_info info = { + .op =VIRTIO_VSOCK_OP_RW , + .msg = msg, + .pkt_len = dgram_len, + .vsk = vsk, + .remote_cid = remote_addr->svm_cid, + .remote_port = remote_addr->svm_port, + .net =sock_net (sk_vsock (vsk)), + }; + +return virtio_transport_send_pkt_info (vsk, &info); }EXPORT_SYMBOL_GPL (virtio_transport_dgram_enqueue);@@ -1771,7 +1857,8 @@ virtio_transport_recv_listen(struct sock *sk, struct sk_buff *skb,static bool virtio_transport_valid_type (u16 type) {return (type ==VIRTIO_VSOCK_TYPE_STREAM ) || - (type ==VIRTIO_VSOCK_TYPE_SEQPACKET ); + (type ==VIRTIO_VSOCK_TYPE_SEQPACKET ) || + (type ==VIRTIO_VSOCK_TYPE_DGRAM ); }/* We are under the virtio-vsock's vsock->rx_lock or vhost-vsock's vq->mutex @@ -1805,6 +1892,41 @@ 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_socket_net (&dst, net); + +if (!sk || sk->sk_type !=SOCK_DGRAM ) { +if (sk) +sock_put (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 1923
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);I'd love to see the receive path be free of
lock_sockthe way that UDP does.My understanding from udp is that we can avoid the race with close/SOCK_DEAD by using
SOCK_RCU_FREEand putting this receive in an rcu read-side section. I think this makessk->sk_socket->statesafe inside the RCU read side section too.
Quoted 6 lines
I think
vsock_check_source()in a later patch goes away withvirtio_transport_dgram_source_allow(), so I think maybe that patch should come before this?Best,
Bobby
On second thought, the lock sock does not matter here since this isn't the recvmsg path and there are not multiple rx workers vying for this lock (if there were multiple, they'd already be vying for the vsock->rx_lock anyway).
Feel free to ignore my noise about that.
Quoted 28 lines
Quoted 28 lines
net/vmw_vsock/virtio_transport_common.c +6
-0+ +release_sock (sk); +sock_put (sk); +return ; + } +/* The socket must be in connected or bound table * otherwise send reset back */ 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 · 2 lines
-- 2.56.0
RFC PATCH 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.
--- net/vmw_vsock/virtio_transport.c | 42 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport.c +39 -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 =
@@ -835,6 +867,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 +967,8 @@ static struct virtio_device_id id_table[] = { };static unsigned int features[] = { -VIRTIO_VSOCK_F_SEQPACKET +VIRTIO_VSOCK_F_SEQPACKET , +VIRTIO_VSOCK_F_DGRAM , };static struct virtio_driver virtio_vsock_driver = {
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC 4/8] vsock/virtio: negotiate datagram support on the guest
Sashiko has reviewed this patch and found no issues.
Quoted 11 lines
Advertise
VIRTIO_VSOCK_F_DGRAMand record whether the device negotiated it. Gate datagram socket initialization and destination checks on the negotiated feature and the transport's supported network namespace mode.Perform device capability checks at socket creation without requiring a remote address, which is not yet known for an unconnected datagram socket. The common virtio implementation provides the datagram data path.
Signed-off-by Sergio Lopez <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
RFC PATCH 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.
--- drivers/vhost/vsock.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-)
drivers/vhost/vsock.c +26 -2
@@ -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,
@@ -737,6 +759,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 +913,7 @@ static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features) } vsock->seqpacket_allow = features & (1ULL <<VIRTIO_VSOCK_F_SEQPACKET ); + vsock->dgram_allow = features & (1ULL <<VIRTIO_VSOCK_F_DGRAM );for (i =0 ; i <ARRAY_SIZE (vsock->vqs); i++) { vq = &vsock->vqs[i];
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC 5/8] vhost/vsock: negotiate datagram support with guests
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] vhost/vsock: missing dgram_allow check in vhost_vsock_handle_tx_kick() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] vhost/vsock: missing dgram_allow check in vhost_vsock_handle_tx_kick() --
commit 635912e90dc603f9b7384d44cf174e18056f799d
Author: Sergio Lopez <[email protected]>
vhost/vsock: negotiate datagram support with guests
This commit exposes VIRTIO_VSOCK_F_DGRAM in the vhost feature bitmap and tracks whether datagram support was negotiated per guest via vsock->dgram_allow. It also wires up vhost_transport_dgram_allow() to check the negotiated feature on the target guest for outbound datagrams.
Quoted 4 lines
drivers/vhost/vsock.c +0
-0
[ ... ]
Quoted 15 lines
drivers/vhost/vsock.c +11
-0@@ -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)
drivers/vhost/vsock.c static bool vhost_transport_seqpacket_allow(struct vsock_sock *vsk, · line 543
Quoted 10 lines
drivers/vhost/vsock.c +7
-0+ 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,
[Severity: Medium]
Can a guest that has not negotiated VIRTIO_VSOCK_F_DGRAM still send VIRTIO_VSOCK_TYPE_DGRAM packets to unconnected bound SOCK_DGRAM sockets on the host?
vhost_transport_dgram_allow() checks vsock->dgram_allow on outbound host connect() and sendmsg() paths, but on the guest-to-host path in drivers/vhost/vsock.c:vhost_vsock_handle_tx_kick(), incoming packets are passed to virtio_transport_recv_pkt() without checking vsock->dgram_allow:
drivers/vhost/vsock.c:vhost_vsock_handle_tx_kick() {
...
/* Only accept correctly addressed packets */
if (le64_to_cpu(hdr->src_cid) == vsock->guest_cid &&
le64_to_cpu(hdr->dst_cid) ==
vhost_transport_get_local_cid())
virtio_transport_recv_pkt(&vhost_transport, skb,
vsock->net);
else
kfree_skb(skb);
...
}Unlike VIRTIO_VSOCK_TYPE_SEQPACKET, where inbound connection requests go through virtio_transport_recv_listen() -> vsock_assign_transport() -> vhost_transport_seqpacket_allow(), inbound VIRTIO_VSOCK_TYPE_DGRAM packets in net/vmw_vsock/virtio_transport_common.c:virtio_transport_recv_pkt() are queued directly onto bound unconnected SOCK_DGRAM sockets without calling dgram_allow():
net/vmw_vsock/virtio_transport_common.c:virtio_transport_recv_pkt() {
...
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_socket_net(&dst, net);
...
if (sock_flag(sk, SOCK_DEAD) ||
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);
...
}Even with the later virtio_transport_dgram_source_allow() check, unconnected bound SOCK_DGRAM sockets return true before consulting dgram_allow().
Should vhost_vsock_handle_tx_kick() drop VIRTIO_VSOCK_TYPE_DGRAM packets when !vsock->dgram_allow, or should virtio_transport_recv_pkt() validate dgram_allow() for incoming datagrams regardless of whether the receiving socket is connected?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
RFC PATCH 6/8 vsock: allow datagrams to use directional transports
In reply to Sergio LopezUse a dedicated datagram transport when present; otherwise initialize socket state through a directional transport, falling back to vhost when the guest transport lacks datagram support. Keep VMCI's existing priority and reject datagrams during Hyper-V socket initialization.
Select a compatible transport for each datagram destination using the stream routing policy. Keep the socket's transport state intact for queued sends, pin the selected backend during dispatch, and check connected sources against the peer's route. Preserve parent routing in returned addresses and when connecting through the guest-to-host fallback. Preserve explicit source CIDs on loopback datagrams so replies reach the bound socket. Reject loopback source bindings whose reply addresses select another transport.
Remove sockets from the core tables even when transport initialization fails.
--- drivers/vhost/vsock.c | 1 + include/linux/virtio_vsock.h | 4 + include/net/af_vsock.h | 11 ++- net/vmw_vsock/af_vsock.c | 143 +++++++++++++++++++++++++++++--- net/vmw_vsock/hyperv_transport.c | 4 + net/vmw_vsock/virtio_transport.c | 1 + net/vmw_vsock/virtio_transport_common.c | 53 ++++++++++-- net/vmw_vsock/vmci_transport.c | 1 + net/vmw_vsock/vsock_loopback.c | 1 + 9 files changed, 201 insertions(+), 18 deletions(-)
drivers/vhost/vsock.c +1 -0
@@ -465,6 +465,7 @@ static struct virtio_transport vhost_transport = { .dgram_dequeue = virtio_transport_dgram_dequeue, .dgram_bind = virtio_transport_dgram_bind, .dgram_allow = vhost_transport_dgram_allow, + .dgram_transport_match = virtio_transport_dgram_transport_match, .stream_enqueue = virtio_transport_stream_enqueue, .stream_dequeue = virtio_transport_stream_dequeue,
include/linux/virtio_vsock.h +4 -0
@@ -14,6 +14,7 @@ struct virtio_vsock_skb_cb {bool tap_delivered;/* Datagram payload is still charged to bytes_unsent. */ bool tx_accounted; +bool to_host;u32 offset; };
@@ -173,6 +174,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;
@@ -263,6 +265,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);
@@ -276,6 +279,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 +9 -2
@@ -121,9 +121,13 @@ struct vsock_transport {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: another transport can use this socket's datagram state. */ +bool (*dgram_transport_match)(const struct vsock_transport *transport);/* Optional payload length override, called with the receive queue locked. */ size_t (*dgram_payload_len)(const struct sk_buff *skb);
@@ -240,6 +244,9 @@ void vsock_remove_sock(struct vsock_sock *vsk);void vsock_for_each_connected_socket (struct vsock_transport *transport,void (*fn)(struct sock *sk));int vsock_assign_transport (struct vsock_sock *vsk,struct vsock_sock *psk); +const struct vsock_transport * +vsock_dgram_get_transport (struct vsock_sock *vsk, +struct sockaddr_vm *remote_addr);bool vsock_find_cid (unsigned int cid);void vsock_linger (struct sock *sk);
net/vmw_vsock/af_vsock.c +132 -11
220 lines not on this page · raw message
net/vmw_vsock/hyperv_transport.c +4 -0
@@ -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 ;
@@ -563,6 +566,7 @@ static int hvs_dgram_dequeue(struct vsock_sock *vsk, struct msghdr *msg, }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) {
net/vmw_vsock/virtio_transport.c +1 -0
@@ -585,6 +585,7 @@ static struct virtio_transport virtio_transport = { .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 +48 -5
105 lines not on this page · raw message
net/vmw_vsock/vmci_transport.c +1 -0
@@ -1723,6 +1723,7 @@ static int vmci_transport_dgram_bind(struct vsock_sock *vsk,static int vmci_transport_dgram_enqueue (struct vsock_sock *vsk, +const struct vsock_transport *transport,struct sockaddr_vm *remote_addr,struct msghdr *msg,size_t len)
net/vmw_vsock/vsock_loopback.c +1 -0
@@ -77,6 +77,7 @@ static struct virtio_transport loopback_transport = { .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 6/8] vsock: allow datagrams to use directional transports
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] vsock/virtio: bypass of checks in virtio_transport_dgram_source_allow() - [Medium] vsock…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] vsock/virtio: bypass of checks in virtio_transport_dgram_source_allow()
- [Medium] vsock: vsock_dgram_sendmsg() mutates connected vsk->remote_addr on fallback --
commit 634f5c11de9d307a1a88027855c76c28c15f7d96
Author: Sergio Lopez <[email protected]>
vsock: allow datagrams to use directional transports
This commit enables AF_VSOCK datagram sockets to initialize through and route across directional transports (such as virtio, vhost, and loopback) when a dedicated datagram transport is not registered.
Quoted 4 lines
net/vmw_vsock/af_vsock.c +0
-0
[ ... ]
Quoted 12 lines
net/vmw_vsock/af_vsock.c +2
-1@@ -1493,7 +1606,8 @@ static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg, remote_addr = &vsk->remote_addr;if (remote_addr->svm_cid ==VMADDR_CID_ANY ) - remote_addr->svm_cid = transport->get_local_cid (); + remote_addr->svm_cid = transport->dgram_transport_match ? +VMADDR_CID_LOCAL : transport->get_local_cid ();/* XXX Should connect() or this function ensure remote_addr is * bound? @@ -1507,13 +1621,14 @@ static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg, goto out;
net/vmw_vsock/af_vsock.c static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg, · line 1628
Quoted 10 lines
net/vmw_vsock/af_vsock.c +3
-3} -if (!transport->dgram_allow (vsk, remote_addr->svm_cid, - remote_addr->svm_port)) { - err = -EINVAL ; + transport =vsock_dgram_get_transport (vsk, remote_addr); +if (IS_ERR (transport)) { + err =PTR_ERR (transport); goto out; }
[Severity: Medium]
When vsock_dgram_sendmsg() is called without msg_name on a connected socket, remote_addr points directly to &vsk->remote_addr.
If the socket connected to an active H2G guest CID (leaving vsk->remote_addr.svm_flags without VMADDR_FLAG_TO_HOST) and that guest is temporarily unavailable while g2h_fallback is enabled, vsock_dgram_get_transport() takes the fallback path and mutates remote_addr in place:
net/vmw_vsock/af_vsock.c:vsock_dgram_get_transport() {
...
} else if (sock_net(sk)->vsock.g2h_fallback && transport_g2h &&
transport_g2h->has_remote_cid &&
transport_g2h->has_remote_cid(vsk, cid)) {
transport = transport_g2h;
to_host = true;
}
...
if (to_host)
remote_addr->svm_flags |= VMADDR_FLAG_TO_HOST;
}Because vsock_dgram_get_transport() checks VMADDR_FLAG_TO_HOST before transport_h2g->has_remote_cid(), won't this permanently pin vsk->remote_addr to transport_g2h for subsequent sends, receive source checks, and getpeername() even after the H2G guest restarts?
Quoted 20 lines
net/vmw_vsock/virtio_transport_common.c +6
-1@@ -322,13 +322,18 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk, info->type =virtio_transport_get_type (sk_vsock (vsk)); - t_ops =virtio_transport_get_ops (vsk); + t_ops = info->t_ops ?:virtio_transport_get_ops (vsk);if (unlikely (!t_ops))return -EFAULT ; src_cid = t_ops->transport.get_local_cid (); +/* Preserve explicit datagram bindings so loopback replies can find them. */ +if (info->type ==VIRTIO_VSOCK_TYPE_DGRAM && + src_cid ==VMADDR_CID_LOCAL && + vsk->local_addr.svm_cid !=VMADDR_CID_ANY ) + src_cid = vsk->local_addr.svm_cid; src_port = vsk->local_addr.svm_port;
[ ... ]
Quoted 36 lines
net/vmw_vsock/virtio_transport_common.c +27
-0@@ -1861,6 +1877,33 @@ static bool virtio_transport_valid_type(u16 type) (type ==VIRTIO_VSOCK_TYPE_DGRAM ); } +static bool +virtio_transport_dgram_source_allow (struct vsock_sock *vsk, +const struct vsock_transport *transport, +const struct sockaddr_vm *src) +{ +const struct vsock_transport *peer_transport; +struct sockaddr_vm peer = vsk->remote_addr; +bool allow; + +if (!virtio_transport_dgram_transport_match (vsk->transport) || + !virtio_transport_dgram_transport_match (transport)) +return false; +/* Shutdown preserves the peer; only disconnect removes it. */ +if (sk_vsock (vsk)->sk_socket->state ==SS_UNCONNECTED ) +return true; + + peer_transport =vsock_dgram_get_transport (vsk, &peer); +if (IS_ERR (peer_transport)) +return false; + allow = peer_transport == transport && + src->svm_port == vsk->remote_addr.svm_port && + (src->svm_cid == vsk->remote_addr.svm_cid || + transport->get_local_cid () ==VMADDR_CID_LOCAL ); +module_put (peer_transport->module); +return allow; +} +/* We are under the virtio-vsock's vsock->rx_lock or vhost-vsock's vq->mutex * lock. */ @@ -1911,14 +1954,14 @@ void virtio_transport_recv_pkt(struct virtio_transport *t, vsk =vsock_sk (sk);
net/vmw_vsock/virtio_transport_common.c void virtio_transport_recv_pkt(struct virtio_transport *t, · line 1961
Quoted 10 lines
net/vmw_vsock/virtio_transport_common.c +1
-3lock_sock (sk);if (sock_flag (sk,SOCK_DEAD ) || - vsk->transport != &t->transport || - (sk->sk_socket->state !=SS_UNCONNECTED && - !vsock_check_source (vsk, &t->transport, &src))) { + !virtio_transport_dgram_source_allow (vsk, &t->transport, &src)) {release_sock (sk);sock_put (sk); goto free_pkt; }
[Severity: High]
Can virtio_transport_dgram_source_allow() bypass feature negotiation, local CID transport isolation, and connected peer CID checks?
First, for SS_UNCONNECTED sockets, virtio_transport_dgram_source_allow() returns true immediately after virtio_transport_dgram_transport_match(), which is a static callback comparison that succeeds for virtio_transport, vhost_transport, and loopback_transport without calling transport->dgram_allow(vsk, src->svm_cid, src->svm_port).
Since neither vhost_vsock_handle_tx_kick() nor virtio_transport_rx_work() checks vsock->dgram_allow before calling virtio_transport_recv_pkt(), a peer that has not negotiated VIRTIO_VSOCK_F_DGRAM can deliver VIRTIO_VSOCK_TYPE_DGRAM packets to any unconnected bound socket on the host (bypassing vhost_transport_dgram_allow()) or on a guest initialized via the vhost_transport fallback (bypassing virtio_transport_dgram_allow_check()).
Second, returning true early for SS_UNCONNECTED sockets skips checking whether vsk->local_addr.svm_cid is compatible with the receiving transport. Because virtio_transport_rx_work() does not validate hdr->dst_cid against the guest CID and __vsock_find_bound_socket_net() matches dst.svm_cid == vsk->local_addr.svm_cid or VMADDR_CID_ANY, an external peer on virtio_transport can send datagrams addressed to VMADDR_CID_LOCAL, VMADDR_CID_HOST, or VMADDR_CID_ANY and reach an unconnected socket bound to VMADDR_CID_LOCAL or VMADDR_CID_HOST.
Third, on loopback_transport, virtio_transport_send_pkt_info() now preserves explicit vsk->local_addr.svm_cid values in hdr->src_cid, and __vsock_find_bound_socket_net() allows distinct local sockets to bind to the same port on different local CIDs (such as VMADDR_CID_LOCAL and the guest or host CID). Because transport->get_local_cid() == VMADDR_CID_LOCAL is always true for loopback_transport, won't a local socket bound to (VMADDR_CID_LOCAL, port) be accepted by a socket connected to (guest_cid, port) despite having a mismatched src->svm_cid?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
RFC PATCH 7/8 vsock: preserve host-to-guest datagram boundaries
In reply to Sergio LopezVhost splits large datagrams across guest RX buffers, delivering each fragment as a separate message. Allocate full-size RX buffers with page fragments when datagrams are negotiated, and drop whole datagrams if a guest buffer is too small. Refill empty RX queues even when only one full-size buffer fits in the ring.
--- drivers/vhost/vsock.c | 8 ++++++++ net/vmw_vsock/virtio_transport.c | 28 +++++++++++++++++++--------- 2 files changed, 27 insertions(+), 9 deletions(-)
drivers/vhost/vsock.c +8 -0
@@ -185,6 +185,14 @@ vhost_transport_do_send_pkt(struct vhost_vsock *vsock,* buffer, we split it using multiple buffers. */ if (payload_len > iov_len -sizeof (*hdr)) { +/* Datagrams must fit in a single descriptor chain. */ +if (le16_to_cpu (hdr->type) ==VIRTIO_VSOCK_TYPE_DGRAM ) { +kfree_skb (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
net/vmw_vsock/virtio_transport.c +19 -9
@@ -54,6 +54,7 @@ struct virtio_vsock {bool rx_run;int rx_buf_nr;int rx_buf_max_nr; +struct scatterlist in_bufs[MAX_SKB_FRAGS +1 ];u32 guest_cid;bool seqpacket_allow;
@@ -312,22 +313,31 @@ 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 virtqueue *vq;struct sk_buff *skb;int ret; vq = vsock->vqs[VSOCK_VQ_RX ]; +/* A datagram must fit in one RX buffer, including its header. */ +if (vsock->dgram_allow) + total_len =VIRTIO_VSOCK_SKB_HEADROOM + +VIRTIO_VSOCK_MAX_PKT_BUF_SIZE ; do { - skb = virtio_vsock_alloc_linear_skb(total_len, GFP_KERNEL); + skb = virtio_vsock_alloc_skb(total_len, GFP_KERNEL); if (!skb) break; memset(skb->head, 0, 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); + /* Post the header and the entire payload as one RX buffer. */ + virtio_vsock_skb_put(skb, total_len - VIRTIO_VSOCK_SKB_HEADROOM); + skb_push(skb, VIRTIO_VSOCK_SKB_HEADROOM); + sg_init_table(vsock->in_bufs, ARRAY_SIZE(vsock->in_bufs)); + ret = skb_to_sgvec(skb, vsock->in_bufs, 0, total_len); + skb_pull(skb, VIRTIO_VSOCK_SKB_HEADROOM); + if (ret >= 0) + ret = virtqueue_add_inbuf(vq, vsock->in_bufs, ret, skb, + GFP_KERNEL); if (ret < 0) { kfree_skb(skb); break;
@@ -698,7 +708,7 @@ 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)) {kfree_skb (skb);continue ; }
@@ -710,8 +720,7 @@ static void virtio_transport_rx_work(struct work_struct *work)continue ; } -if (payload_len) -virtio_vsock_skb_put (skb, payload_len); +pskb_trim_unique (skb, payload_len);virtio_transport_deliver_tap_pkt (skb);
@@ -723,7 +732,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);
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC 7/8] vsock: preserve host-to-guest datagram boundaries
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] vsock/virtio: uncondensed 64 KiB rx skbs exhaust sk_rcvbuf on small packets --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] vsock/virtio: uncondensed 64 KiB rx skbs exhaust sk_rcvbuf on small packets --
commit 7675c425557daffdbb774353bcdf90cea6ee4417
Author: Sergio Lopez <[email protected]>
vsock: preserve host-to-guest datagram boundaries
This patch preserves host-to-guest datagram boundaries by dropping oversized datagrams in vhost and allocating full-size paged RX buffers in virtio_transport when datagrams are negotiated.
Quoted 4 lines
net/vmw_vsock/virtio_transport.c +0
-0
[ ... ]
Quoted 47 lines
net/vmw_vsock/virtio_transport.c +15
-6@@ -312,22 +313,31 @@ 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 virtqueue *vq;struct sk_buff *skb;int ret; vq = vsock->vqs[VSOCK_VQ_RX ]; +/* A datagram must fit in one RX buffer, including its header. */ +if (vsock->dgram_allow) + total_len =VIRTIO_VSOCK_SKB_HEADROOM + +VIRTIO_VSOCK_MAX_PKT_BUF_SIZE ; do { - skb = virtio_vsock_alloc_linear_skb(total_len, GFP_KERNEL); + skb = virtio_vsock_alloc_skb(total_len, GFP_KERNEL); if (!skb) break; memset(skb->head, 0, 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); + /* Post the header and the entire payload as one RX buffer. */ + virtio_vsock_skb_put(skb, total_len - VIRTIO_VSOCK_SKB_HEADROOM); + skb_push(skb, VIRTIO_VSOCK_SKB_HEADROOM); + sg_init_table(vsock->in_bufs, ARRAY_SIZE(vsock->in_bufs)); + ret = skb_to_sgvec(skb, vsock->in_bufs, 0, total_len); + skb_pull(skb, VIRTIO_VSOCK_SKB_HEADROOM); + if (ret >= 0) + ret = virtqueue_add_inbuf(vq, vsock->in_bufs, ret, skb, + GFP_KERNEL); if (ret < 0) { kfree_skb(skb); break;@@ -698,7 +708,7 @@ 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)) {kfree_skb (skb);continue ; }
net/vmw_vsock/virtio_transport.c static void virtio_transport_rx_work(struct work_struct *work) · line 723
Quoted 6 lines
net/vmw_vsock/virtio_transport.c +1
-2@@ -710,8 +720,7 @@ static void virtio_transport_rx_work(struct work_struct *work)continue ; } -if (payload_len) -virtio_vsock_skb_put (skb, payload_len); +pskb_trim_unique (skb, payload_len);
[Severity: Medium]
Can allocating 64 KiB nonlinear RX buffers here leave skb->truesize at ~66 KiB after pskb_trim_unique() and prematurely exhaust sk->sk_rcvbuf on small packets?
When vsock->dgram_allow is true, virtio_vsock_rx_fill() calls virtio_vsock_alloc_skb() with total_len = VIRTIO_VSOCK_SKB_HEADROOM + VIRTIO_VSOCK_MAX_PKT_BUF_SIZE (44 + 65536 bytes):
include/linux/virtio_vsock.h:virtio_vsock_alloc_skb() {
if (size <= SKB_WITH_OVERHEAD(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
return virtio_vsock_alloc_linear_skb(size, mask);
size -= VIRTIO_VSOCK_SKB_HEADROOM;
return __virtio_vsock_alloc_skb_with_frags(VIRTIO_VSOCK_SKB_HEADROOM,
size, mask);
}On 4 KiB and 16 KiB page systems, this calls alloc_skb_with_frags() with a 44-byte linear header request and 65536 bytes in page fragments. Because skb->head comes from skb_small_head_cache, reserving 44 bytes leaves only about 276 bytes of linear tailroom (skb->end - skb->tail), while all 64 KiB of payload capacity is placed in page fragments with skb->truesize set to ~66 KiB.
When virtio_transport_rx_work() receives a packet and calls pskb_trim_unique(skb, payload_len) -> pskb_trim() -> ___pskb_trim(), trailing unused fragments are unreferenced without reducing skb->truesize, and skb_condense() is called:
net/core/skbuff.c:skb_condense() {
if (skb->data_len) {
if (skb->data_len > skb->end - skb->tail ||
skb_cloned(skb) || !skb_frags_readable(skb))
return;
/* Nice, we can free page frag(s) right now */
__pskb_pull_tail(skb, skb->data_len);
}
skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
}Whenever payload_len exceeds skb->end - skb->tail (~276 bytes), skb_condense() returns early without pulling data into skb->head, freeing the first 32 KiB page fragment, or updating skb->truesize.
In virtio_transport_recv_pkt(), sock_queue_rcv_skb(sk, skb) then charges the full ~66 KiB skb->truesize against sk->sk_rcvbuf (212992 bytes by default), so queuing just 4 datagrams of ~300 bytes will fill sk_rcvbuf and drop subsequent datagrams, while also pinning 32 KiB page fragments per packet across SOCK_DGRAM, SOCK_STREAM, and SOCK_SEQPACKET receive queues sharing the RX virtqueue.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Bobby Eshleman
reviewerRe: [PATCH RFC 7/8] vsock: preserve host-to-guest datagram boundaries
In reply to Sergio LopezQuoted 5 lines · Sergio Lopez
Vhost splits large datagrams across guest RX buffers, delivering each fragment as a separate message. Allocate full-size RX buffers with page fragments when datagrams are negotiated, and drop whole datagrams if a guest buffer is too small. Refill empty RX queues even when only one full-size buffer fits in the ring.
I wonder if it might be better to do what seqpacket does, and just split the datagram across the buffers (for seqpacket, until the EOM flag is seen)? I'd expect that we will get better utilization of our vq buffers this way?
Best,
Bobby
Quoted 108 lines
Signed-off-by Sergio Lopez <[email protected]>--- drivers/vhost/vsock.c | 8 ++++++++ net/vmw_vsock/virtio_transport.c | 28 +++++++++++++++++++--------- 2 files changed, 27 insertions(+), 9 deletions(-)drivers/vhost/vsock.c +8
-0@@ -185,6 +185,14 @@ vhost_transport_do_send_pkt(struct vhost_vsock *vsock,* buffer, we split it using multiple buffers. */ if (payload_len > iov_len -sizeof (*hdr)) { +/* Datagrams must fit in a single descriptor chain. */ +if (le16_to_cpu (hdr->type) ==VIRTIO_VSOCK_TYPE_DGRAM ) { +kfree_skb (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 net/vmw_vsock/virtio_transport.c +19
-9@@ -54,6 +54,7 @@ struct virtio_vsock {bool rx_run;int rx_buf_nr;int rx_buf_max_nr; +struct scatterlist in_bufs[MAX_SKB_FRAGS +1 ];u32 guest_cid;bool seqpacket_allow;@@ -312,22 +313,31 @@ 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 virtqueue *vq;struct sk_buff *skb;int ret; vq = vsock->vqs[VSOCK_VQ_RX ]; +/* A datagram must fit in one RX buffer, including its header. */ +if (vsock->dgram_allow) + total_len =VIRTIO_VSOCK_SKB_HEADROOM + +VIRTIO_VSOCK_MAX_PKT_BUF_SIZE ; do { - skb = virtio_vsock_alloc_linear_skb(total_len, GFP_KERNEL); + skb = virtio_vsock_alloc_skb(total_len, GFP_KERNEL); if (!skb) break; memset(skb->head, 0, 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); + /* Post the header and the entire payload as one RX buffer. */ + virtio_vsock_skb_put(skb, total_len - VIRTIO_VSOCK_SKB_HEADROOM); + skb_push(skb, VIRTIO_VSOCK_SKB_HEADROOM); + sg_init_table(vsock->in_bufs, ARRAY_SIZE(vsock->in_bufs)); + ret = skb_to_sgvec(skb, vsock->in_bufs, 0, total_len); + skb_pull(skb, VIRTIO_VSOCK_SKB_HEADROOM); + if (ret >= 0) + ret = virtqueue_add_inbuf(vq, vsock->in_bufs, ret, skb, + GFP_KERNEL); if (ret < 0) { kfree_skb(skb); break;@@ -698,7 +708,7 @@ 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)) {kfree_skb (skb);continue ; }@@ -710,8 +720,7 @@ static void virtio_transport_rx_work(struct work_struct *work)continue ; } -if (payload_len) -virtio_vsock_skb_put (skb, payload_len); +pskb_trim_unique (skb, payload_len);virtio_transport_deliver_tap_pkt (skb);@@ -723,7 +732,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);Signature · 2 lines
-- 2.56.0
Re: [PATCH RFC 7/8] vsock: preserve host-to-guest datagram boundaries
Quoted 7 lines · Bobby Eshleman
Quoted 5 lines · Sergio Lopez
Vhost splits large datagrams across guest RX buffers, delivering each fragment as a separate message. Allocate full-size RX buffers with page fragments when datagrams are negotiated, and drop whole datagrams if a guest buffer is too small. Refill empty RX queues even when only one full-size buffer fits in the ring.
Quoted 5 lines
I wonder if it might be better to do what seqpacket does, and just split the datagram across the buffers (for seqpacket, until the EOM flag is seen)? I'd expect that we will get better utilization of our vq buffers this way?
Yeah, I do like that approach better. I'm going to implement it for v2.
Thanks!
Sergio.
RFC PATCH 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.
--- tools/testing/vsock/Makefile | 3 +- tools/testing/vsock/README | 23 +- tools/testing/vsock/util.c | 62 +-- tools/testing/vsock/util.h | 10 + tools/testing/vsock/vsock_test.c | 154 ++++++- tools/testing/vsock/vsock_test_dgram.c | 809 +++++++++++++++++++++++++++++++++ tools/testing/vsock/vsock_test_dgram.h | 26 ++ 7 files changed, 1055 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 +21 -2
@@ -5,7 +5,7 @@ Hyper-V. The following tests are available: - * vsock_test - core AF_VSOCK socket functionality + * vsock_test - stream, seqpacket and datagram functionality * vsock_diag_test - vsock_diag.ko module for listing open sockets The following prerequisite steps are not automated and must be performed prior
@@ -32,7 +32,7 @@ Invoke test binaries in both directions as follows: --control-port=1234 \ --peer-cid=2 (host)# $TEST_BINARY --mode=client \ - --control-port=$GUEST_IP \ + --control-host=$GUEST_IP \ --control-port=1234 \ --peer-cid=3
@@ -51,6 +51,25 @@ suite run, e.g. For more information see Documentation/dev-tools/kmemleak.rst. +Datagram tests +-------------- +vsock_test includes datagram cases in the client/server runs above. Unsupported +cases print a skip reason. + +Virtio/vhost datagrams require kernel support on both sides and a VMM that +negotiates VIRTIO_VSOCK_F_DGRAM. Load vsock_loopback on both host and guest for +the local cases, and make /dev/vsock available. VMCI's dedicated datagram +transport takes priority when loaded, causing virtio/vhost cases to skip. + +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 (48, requiring BPF privileges) and nested routing test (49, requiring both +virtio and vhost transports) need explicit selection. The VMCI queue-length +test (53) runs only with VMCI. + vsock_perf utility ------------------- 'vsock_perf' is a simple tool to measure vsock performance. It works in
tools/testing/vsock/util.c +35 -27
82 lines not on this page · raw message
tools/testing/vsock/util.h +10 -0
@@ -51,6 +51,10 @@ struct test_opts {enum test_mode mode;unsigned int peer_cid;unsigned int peer_port; +unsigned int bind_cid; +bool local; +bool to_host; +bool expect_to_host; };/* A test case definition. Test functions must print failures to stderr and
@@ -66,6 +70,12 @@ struct test_case {void (*run_server)(const struct test_opts *opts);bool skip; +bool local;/* Can run without a control connection */ + +/* Return a skip reason or NULL after the selection barrier. Checks +* that coordinate over the control channel must agree on both peers. +*/ +const char *(*check)(const struct test_opts *opts); };void init_signals (void );
tools/testing/vsock/vsock_test.c +152 -2
211 lines not on this page · raw message
tools/testing/vsock/vsock_test_dgram.c new file +809 -0
809 lines not on this page · raw message
tools/testing/vsock/vsock_test_dgram.h new file +26 -0
@@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef VSOCK_TEST_DGRAM_H +#define VSOCK_TEST_DGRAM_H + +#include "util.h" + +const char *vsock_dgram_check_local (const struct test_opts *opts); +const char *vsock_dgram_check_peer (const struct test_opts *opts); +const char *vsock_dgram_check_routes (const struct test_opts *opts); +const char *vsock_dgram_check_vmci (const struct test_opts *opts); + +void test_dgram_bind (const struct test_opts *opts); +void test_dgram_receive (const struct test_opts *opts); +void test_dgram_receive_flags (const struct test_opts *opts); +void test_dgram_filter (const struct test_opts *opts); +void test_dgram_connect (const struct test_opts *opts); +void test_dgram_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_routes (const struct test_opts *opts); +void test_dgram_peer_filter (const struct test_opts *opts); +void test_dgram_vmci_inq (const struct test_opts *opts); + +#endif /* VSOCK_TEST_DGRAM_H */
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH RFC 8/8] vsock: add datagram tests to vsock_test
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Low] vsock/test: missing opening brace in test_cases[] breaks compilation - [Low] vsock/test: outdated…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Low] vsock/test: missing opening brace in test_cases[] breaks compilation
- [Low] vsock/test: outdated datagram test IDs documented in README
--
commit f2fbe86b3992eecc82537eceffe22f137e4055d3
Author: Sergio Lopez <[email protected]>
vsock: add datagram tests to vsock_test
This commit adds datagram test cases to vsock_test covering local semantics, host/guest transfers, routing, and queue accounting, along with capability checks and documentation updates.
Quoted 4 lines
tools/testing/vsock/README +0
-0
[ ... ]
Quoted 12 lines
tools/testing/vsock/README +8
-0@@ -51,6 +51,25 @@ suite run, e.g. For more information see Documentation/dev-tools/kmemleak.rst. +Datagram tests +-------------- +vsock_test includes datagram cases in the client/server runs above. Unsupported +cases print a skip reason. + +Virtio/vhost datagrams require kernel support on both sides and a VMM that +negotiates VIRTIO_VSOCK_F_DGRAM. Load vsock_loopback on both host and guest for +the local cases, and make /dev/vsock available. VMCI's dedicated datagram
tools/testing/vsock/README suite run, e.g. · line 71
Quoted 10 lines
tools/testing/vsock/README +10
-0+transport takes priority when loaded, causing virtio/vhost cases to skip. + +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 (48, requiring BPF privileges) and nested routing test (49, requiring both +virtio and vhost transports) need explicit selection. The VMCI queue-length +test (53) runs only with VMCI.
[Severity: Low]
Are these test IDs off by 3?
In tools/testing/vsock/vsock_test.c, there are 44 existing entries in test_cases[] before the new datagram tests (indices 0 through 43, ending with the three SO_INQ test cases). That places test_dgram_sockmap() at index 51, test_dgram_nested_source() at index 52, and test_dgram_vmci_inq() at index 56, while indices 48, 49, and 53 correspond to test_dgram_connect(), test_dgram_source_cid(), and test_dgram_sizes().
Quoted 4 lines
tools/testing/vsock/vsock_test.c +0
-0
[ ... ]
tools/testing/vsock/vsock_test.c static struct test_case test_cases[] = { · line 2921
Quoted 11 lines
tools/testing/vsock/vsock_test.c +6
-0@@ -2911,6 +2912,95 @@ static struct test_case test_cases[] = { { .name = "SO_INQ option validation", .run_client = test_so_inq_validation_client, + }, + .name = "SOCK_DGRAM autobind, duplicate bind and wildcard destination", + .run_client = test_dgram_bind, + .run_server = test_dgram_bind, + .check = vsock_dgram_check_local, + .local = true, },
[Severity: Low]
Is there a missing opening brace before the first SOCK_DGRAM entry in test_cases[]? Without it, compiling tools/testing/vsock/vsock_test.c fails with a syntax error because .name is parsed at the top level of the array initializer and the following closing brace terminates test_cases[] early.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Quoted 3 lines · Sergio Lopez
This series implements
SOCK_DGRAMsupport for virtio-vsock and vhost-vsock, allowing applications to exchange connectionless messages through the existingAF_VSOCKdatagram API.
The cover doesn't say who needs it.
Bobby and Amery took the same feature to v6:
https://lore.kernel.org/all/[email protected]/
Stefano said there that it can work well and that the virtio spec change is the next step. The last mails of the thread have Amery's v7 WIP and a short list of what is left.
The cover doesn't mention it. The spec change is still missing.
Why not finish v7 instead ?
This RFC goes the other way in these places:
what v6 this RFC
----------------- ------------------------ ----------------------------
(cid, port) space own bind table for dgram shared with stream. The 2nd
bind() gets EADDRINUSE
vsk->transport empty until connect(), g2h or h2g, picked in
lookup per packet socket(). -ENODEV with
vsock_loopback only
routing one helper for stream 2nd copy of the chain in
and dgram vsock_assign_transport()
recvmsg() in af_vsock.c, dgram_dequeue() stays,
no dgram_dequeue() dgram_payload_len() is new
VMCI fallback first. No virtio dgram when its module is loaded
dgram > RX buffer drop, EHOSTUNREACH in every RX buffer is 64K
the error queue
Quoted 3 lines
Datagrams use destination-based routing across guest-to-host, host-to-guest and loopback transports while retaining the socket's transport state. VMCI keeps priority as the dedicated datagram
A dgram socket uses two members of struct virtio_vsock_sock: tx_lock and bytes_unsent. They serve SIOCOUTQ only. virtio_transport_alloc_skb() does skb_set_owner_w() already. udp_ioctl() returns sk_wmem_alloc_get() for SIOCOUTQ.
Without them a dgram socket has no transport state, like in v6. Then no need for the owner transport, dgram_transport_match(), fallback_transport, tx_accounted, the skb destructor, etc.
Quoted 2 lines
transport. Guest RX buffers accommodate complete messages when datagrams are negotiated, preserving message boundaries.
That's 64K per RX buffer instead of 4K, for stream and seqpacket too. It undoes commit 03a92f036a04 ("vsock/virtio: Resize receive buffers so that each SKB fits in a 4K page").
pskb_trim_unique() doesn't reduce skb->truesize. So a datagram of 1 byte charges more than 64K to sk_rmem_alloc of the guest socket. The default sk_rcvbuf is about 208K on x86-64. The 5th datagram in the queue is dropped.
Quoted 7 lines · Alexei Starovoitov
Quoted 3 lines · Sergio Lopez
This series implements
SOCK_DGRAMsupport for virtio-vsock and vhost-vsock, allowing applications to exchange connectionless messages through the existingAF_VSOCKdatagram API.The cover doesn't say who needs it.
Quoted 8 lines
Bobby and Amery took the same feature to v6:
https://lore.kernel.org/all/[email protected]/
Stefano said there that it can work well and that the virtio spec change is the next step. The last mails of the thread have Amery's v7 WIP and a short list of what is left.
The cover doesn't mention it. The spec change is still missing.Why not finish v7 instead ?
The previous series is more that two years old, and vsock has changed significantly since then. I thought this deserved a complete refresh.
I should have linked the old series in the cover, though. I'll do that in v2.
As for the spec, I asked Stefano and we agreed it'd be better to have a series that's pretty close to be merged before sending the spec change, and I agree that's a good idea. In fact, adopting SEQPACKET's strategy for splitting up packets in the vq would have an impact on the spec.
Quoted 34 lines
This RFC goes the other way in these places:
what v6 this RFC ----------------- ------------------------ ---------------------------- (cid, port) space own bind table for dgram shared with stream. The 2nd bind() gets EADDRINUSE vsk->transport empty until connect(), g2h or h2g, picked in lookup per packet socket(). -ENODEV with vsock_loopback only routing one helper for stream 2nd copy of the chain in and dgram vsock_assign_transport() recvmsg() in af_vsock.c, dgram_dequeue() stays, no dgram_dequeue() dgram_payload_len() is new VMCI fallback first. No virtio dgram whenits module is loaded
dgram > RX buffer drop, EHOSTUNREACH in every RX buffer is 64K
the error queueQuoted 3 lines
Datagrams use destination-based routing across guest-to-host, host-to-guest and loopback transports while retaining the socket's transport state. VMCI keeps priority as the dedicated datagram
A dgram socket uses two members of struct
virtio_vsock_sock:tx_lockandbytes_unsent. They serve SIOCOUTQ only.virtio_transport_alloc_skb()doesskb_set_owner_w()already.udp_ioctl()returnssk_wmem_alloc_get()for SIOCOUTQ.Without them a dgram socket has no transport state, like in v6. Then no need for the owner transport,
dgram_transport_match(),fallback_transport,tx_accounted, the skb destructor, etc.Quoted 2 lines
transport. Guest RX buffers accommodate complete messages when datagrams are negotiated, preserving message boundaries.
Quoted 8 lines
That's 64K per RX buffer instead of 4K, for stream and seqpacket too. It undoes commit
03a92f036a04("vsock/virtio: Resize receive buffers so that each SKB fits in a 4K page").
pskb_trim_unique()doesn't reduceskb->truesize. So a datagram of 1 byte charges more than 64K tosk_rmem_allocof the guest socket. The defaultsk_rcvbufis about 208K on x86-64. The 5th datagram in the queue is dropped.
Thanks for the review, I'm putting together a v2 addressing these concerns.
Cheers,
Sergio.