vsock/virtio: add SIOCOUTQ support for all virtio based transports
authorLuigi Leonardi <luigi.leonardi@outlook.com>
Tue, 30 Jul 2024 19:43:07 +0000 (21:43 +0200)
committerDavid S. Miller <davem@davemloft.net>
Fri, 2 Aug 2024 08:20:28 +0000 (09:20 +0100)
Introduce support for virtio_transport_unsent_bytes
ioctl for virtio_transport, vhost_vsock and vsock_loopback.

For all transports the unsent bytes counter is incremented
in virtio_transport_get_credit.

In virtio_transport (G2H) and in vhost-vsock (H2G) the counter
is decremented when the skbuff is consumed. In vsock_loopback the
same skbuff is passed from the transmitter to the receiver, so
the counter is decremented before queuing the skbuff to the
receiver.

Signed-off-by: Luigi Leonardi <luigi.leonardi@outlook.com>
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/vhost/vsock.c
include/linux/virtio_vsock.h
net/vmw_vsock/virtio_transport.c
net/vmw_vsock/virtio_transport_common.c
net/vmw_vsock/vsock_loopback.c

index bf664ec9341b35112df166140892b982230a602c..802153e230730bdbfbbb6f4ae263ae99502ef532 100644 (file)
@@ -244,7 +244,7 @@ vhost_transport_do_send_pkt(struct vhost_vsock *vsock,
                                        restart_tx = true;
                        }
 
-                       consume_skb(skb);
+                       virtio_transport_consume_skb_sent(skb, true);
                }
        } while(likely(!vhost_exceeds_weight(vq, ++pkts, total_len)));
        if (added)
@@ -451,6 +451,8 @@ static struct virtio_transport vhost_transport = {
                .notify_buffer_size       = virtio_transport_notify_buffer_size,
                .notify_set_rcvlowat      = virtio_transport_notify_set_rcvlowat,
 
+               .unsent_bytes             = virtio_transport_unsent_bytes,
+
                .read_skb = virtio_transport_read_skb,
        },
 
index c82089dee0c831af449de8a6cc410e86b660fea3..0387d64e2c66c69dd7ab0cad58db5cf0682ad424 100644 (file)
@@ -133,6 +133,7 @@ struct virtio_vsock_sock {
        u32 tx_cnt;
        u32 peer_fwd_cnt;
        u32 peer_buf_alloc;
+       size_t bytes_unsent;
 
        /* Protected by rx_lock */
        u32 fwd_cnt;
@@ -193,6 +194,11 @@ s64 virtio_transport_stream_has_data(struct vsock_sock *vsk);
 s64 virtio_transport_stream_has_space(struct vsock_sock *vsk);
 u32 virtio_transport_seqpacket_has_data(struct vsock_sock *vsk);
 
+ssize_t virtio_transport_unsent_bytes(struct vsock_sock *vsk);
+
+void virtio_transport_consume_skb_sent(struct sk_buff *skb,
+                                      bool consume);
+
 int virtio_transport_do_socket_init(struct vsock_sock *vsk,
                                 struct vsock_sock *psk);
 int
index 64a07acfef12311cb9ad4ce26d6733dae806df74..e0160da4ef43bcce2c6e53b57f1d0257e111e593 100644 (file)
@@ -311,7 +311,7 @@ static void virtio_transport_tx_work(struct work_struct *work)
 
                virtqueue_disable_cb(vq);
                while ((skb = virtqueue_get_buf(vq, &len)) != NULL) {
-                       consume_skb(skb);
+                       virtio_transport_consume_skb_sent(skb, true);
                        added = true;
                }
        } while (!virtqueue_enable_cb(vq));
@@ -540,6 +540,8 @@ static struct virtio_transport virtio_transport = {
                .notify_buffer_size       = virtio_transport_notify_buffer_size,
                .notify_set_rcvlowat      = virtio_transport_notify_set_rcvlowat,
 
+               .unsent_bytes             = virtio_transport_unsent_bytes,
+
                .read_skb = virtio_transport_read_skb,
        },
 
index 16ff976a86e3ed73adaa7c4141189eecbbb74abb..884ee128851e5ce8b01c78fcb95a408986f62936 100644 (file)
@@ -463,6 +463,26 @@ void virtio_transport_inc_tx_pkt(struct virtio_vsock_sock *vvs, struct sk_buff *
 }
 EXPORT_SYMBOL_GPL(virtio_transport_inc_tx_pkt);
 
+void virtio_transport_consume_skb_sent(struct sk_buff *skb, bool consume)
+{
+       struct sock *s = skb->sk;
+
+       if (s && skb->len) {
+               struct vsock_sock *vs = vsock_sk(s);
+               struct virtio_vsock_sock *vvs;
+
+               vvs = vs->trans;
+
+               spin_lock_bh(&vvs->tx_lock);
+               vvs->bytes_unsent -= skb->len;
+               spin_unlock_bh(&vvs->tx_lock);
+       }
+
+       if (consume)
+               consume_skb(skb);
+}
+EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent);
+
 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
 {
        u32 ret;
@@ -475,6 +495,7 @@ u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
        if (ret > credit)
                ret = credit;
        vvs->tx_cnt += ret;
+       vvs->bytes_unsent += ret;
        spin_unlock_bh(&vvs->tx_lock);
 
        return ret;
@@ -488,6 +509,7 @@ void virtio_transport_put_credit(struct virtio_vsock_sock *vvs, u32 credit)
 
        spin_lock_bh(&vvs->tx_lock);
        vvs->tx_cnt -= credit;
+       vvs->bytes_unsent -= credit;
        spin_unlock_bh(&vvs->tx_lock);
 }
 EXPORT_SYMBOL_GPL(virtio_transport_put_credit);
@@ -1090,6 +1112,19 @@ void virtio_transport_destruct(struct vsock_sock *vsk)
 }
 EXPORT_SYMBOL_GPL(virtio_transport_destruct);
 
+ssize_t virtio_transport_unsent_bytes(struct vsock_sock *vsk)
+{
+       struct virtio_vsock_sock *vvs = vsk->trans;
+       size_t ret;
+
+       spin_lock_bh(&vvs->tx_lock);
+       ret = vvs->bytes_unsent;
+       spin_unlock_bh(&vvs->tx_lock);
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(virtio_transport_unsent_bytes);
+
 static int virtio_transport_reset(struct vsock_sock *vsk,
                                  struct sk_buff *skb)
 {
index 6dea6119f5b2812f104298498eeb9d880da7400e..6e78927a598e07cf77386a420b9d05d3f491dc7c 100644 (file)
@@ -98,6 +98,8 @@ static struct virtio_transport loopback_transport = {
                .notify_buffer_size       = virtio_transport_notify_buffer_size,
                .notify_set_rcvlowat      = virtio_transport_notify_set_rcvlowat,
 
+               .unsent_bytes             = virtio_transport_unsent_bytes,
+
                .read_skb = virtio_transport_read_skb,
        },
 
@@ -123,6 +125,10 @@ static void vsock_loopback_work(struct work_struct *work)
        spin_unlock_bh(&vsock->pkt_queue.lock);
 
        while ((skb = __skb_dequeue(&pkts))) {
+               /* Decrement the bytes_unsent counter without deallocating skb
+                * It is freed by the receiver.
+                */
+               virtio_transport_consume_skb_sent(skb, false);
                virtio_transport_deliver_tap_pkt(skb);
                virtio_transport_recv_pkt(&loopback_transport, skb);
        }