Skip to content

Commit e6ab450

Browse files
luigix25davem330
authored andcommitted
vsock/virtio: add SIOCOUTQ support for all virtio based transports
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 <[email protected]> Reviewed-by: Stefano Garzarella <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 744500d commit e6ab450

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

drivers/vhost/vsock.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ vhost_transport_do_send_pkt(struct vhost_vsock *vsock,
244244
restart_tx = true;
245245
}
246246

247-
consume_skb(skb);
247+
virtio_transport_consume_skb_sent(skb, true);
248248
}
249249
} while(likely(!vhost_exceeds_weight(vq, ++pkts, total_len)));
250250
if (added)
@@ -451,6 +451,8 @@ static struct virtio_transport vhost_transport = {
451451
.notify_buffer_size = virtio_transport_notify_buffer_size,
452452
.notify_set_rcvlowat = virtio_transport_notify_set_rcvlowat,
453453

454+
.unsent_bytes = virtio_transport_unsent_bytes,
455+
454456
.read_skb = virtio_transport_read_skb,
455457
},
456458

include/linux/virtio_vsock.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ struct virtio_vsock_sock {
133133
u32 tx_cnt;
134134
u32 peer_fwd_cnt;
135135
u32 peer_buf_alloc;
136+
size_t bytes_unsent;
136137

137138
/* Protected by rx_lock */
138139
u32 fwd_cnt;
@@ -193,6 +194,11 @@ s64 virtio_transport_stream_has_data(struct vsock_sock *vsk);
193194
s64 virtio_transport_stream_has_space(struct vsock_sock *vsk);
194195
u32 virtio_transport_seqpacket_has_data(struct vsock_sock *vsk);
195196

197+
ssize_t virtio_transport_unsent_bytes(struct vsock_sock *vsk);
198+
199+
void virtio_transport_consume_skb_sent(struct sk_buff *skb,
200+
bool consume);
201+
196202
int virtio_transport_do_socket_init(struct vsock_sock *vsk,
197203
struct vsock_sock *psk);
198204
int

net/vmw_vsock/virtio_transport.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ static void virtio_transport_tx_work(struct work_struct *work)
311311

312312
virtqueue_disable_cb(vq);
313313
while ((skb = virtqueue_get_buf(vq, &len)) != NULL) {
314-
consume_skb(skb);
314+
virtio_transport_consume_skb_sent(skb, true);
315315
added = true;
316316
}
317317
} while (!virtqueue_enable_cb(vq));
@@ -540,6 +540,8 @@ static struct virtio_transport virtio_transport = {
540540
.notify_buffer_size = virtio_transport_notify_buffer_size,
541541
.notify_set_rcvlowat = virtio_transport_notify_set_rcvlowat,
542542

543+
.unsent_bytes = virtio_transport_unsent_bytes,
544+
543545
.read_skb = virtio_transport_read_skb,
544546
},
545547

net/vmw_vsock/virtio_transport_common.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,26 @@ void virtio_transport_inc_tx_pkt(struct virtio_vsock_sock *vvs, struct sk_buff *
463463
}
464464
EXPORT_SYMBOL_GPL(virtio_transport_inc_tx_pkt);
465465

466+
void virtio_transport_consume_skb_sent(struct sk_buff *skb, bool consume)
467+
{
468+
struct sock *s = skb->sk;
469+
470+
if (s && skb->len) {
471+
struct vsock_sock *vs = vsock_sk(s);
472+
struct virtio_vsock_sock *vvs;
473+
474+
vvs = vs->trans;
475+
476+
spin_lock_bh(&vvs->tx_lock);
477+
vvs->bytes_unsent -= skb->len;
478+
spin_unlock_bh(&vvs->tx_lock);
479+
}
480+
481+
if (consume)
482+
consume_skb(skb);
483+
}
484+
EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent);
485+
466486
u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
467487
{
468488
u32 ret;
@@ -475,6 +495,7 @@ u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
475495
if (ret > credit)
476496
ret = credit;
477497
vvs->tx_cnt += ret;
498+
vvs->bytes_unsent += ret;
478499
spin_unlock_bh(&vvs->tx_lock);
479500

480501
return ret;
@@ -488,6 +509,7 @@ void virtio_transport_put_credit(struct virtio_vsock_sock *vvs, u32 credit)
488509

489510
spin_lock_bh(&vvs->tx_lock);
490511
vvs->tx_cnt -= credit;
512+
vvs->bytes_unsent -= credit;
491513
spin_unlock_bh(&vvs->tx_lock);
492514
}
493515
EXPORT_SYMBOL_GPL(virtio_transport_put_credit);
@@ -1090,6 +1112,19 @@ void virtio_transport_destruct(struct vsock_sock *vsk)
10901112
}
10911113
EXPORT_SYMBOL_GPL(virtio_transport_destruct);
10921114

1115+
ssize_t virtio_transport_unsent_bytes(struct vsock_sock *vsk)
1116+
{
1117+
struct virtio_vsock_sock *vvs = vsk->trans;
1118+
size_t ret;
1119+
1120+
spin_lock_bh(&vvs->tx_lock);
1121+
ret = vvs->bytes_unsent;
1122+
spin_unlock_bh(&vvs->tx_lock);
1123+
1124+
return ret;
1125+
}
1126+
EXPORT_SYMBOL_GPL(virtio_transport_unsent_bytes);
1127+
10931128
static int virtio_transport_reset(struct vsock_sock *vsk,
10941129
struct sk_buff *skb)
10951130
{

net/vmw_vsock/vsock_loopback.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ static struct virtio_transport loopback_transport = {
9898
.notify_buffer_size = virtio_transport_notify_buffer_size,
9999
.notify_set_rcvlowat = virtio_transport_notify_set_rcvlowat,
100100

101+
.unsent_bytes = virtio_transport_unsent_bytes,
102+
101103
.read_skb = virtio_transport_read_skb,
102104
},
103105

@@ -123,6 +125,10 @@ static void vsock_loopback_work(struct work_struct *work)
123125
spin_unlock_bh(&vsock->pkt_queue.lock);
124126

125127
while ((skb = __skb_dequeue(&pkts))) {
128+
/* Decrement the bytes_unsent counter without deallocating skb
129+
* It is freed by the receiver.
130+
*/
131+
virtio_transport_consume_skb_sent(skb, false);
126132
virtio_transport_deliver_tap_pkt(skb);
127133
virtio_transport_recv_pkt(&loopback_transport, skb);
128134
}

0 commit comments

Comments
 (0)