Skip to content

Commit

Permalink
vsock/virtio: add SIOCOUTQ support for all virtio based transports
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Luigi Leonardi authored and David S. Miller committed Aug 2, 2024
1 parent 744500d commit e6ab450
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
4 changes: 3 additions & 1 deletion drivers/vhost/vsock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
},

Expand Down
6 changes: 6 additions & 0 deletions include/linux/virtio_vsock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion net/vmw_vsock/virtio_transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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,
},

Expand Down
35 changes: 35 additions & 0 deletions net/vmw_vsock/virtio_transport_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down
6 changes: 6 additions & 0 deletions net/vmw_vsock/vsock_loopback.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},

Expand All @@ -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);
}
Expand Down

0 comments on commit e6ab450

Please sign in to comment.