Skip to content

Commit 482b3f9

Browse files
bergwolfgregkh
authored andcommitted
vhost-vsock: add pkt cancel capability
[ Upstream commit 16320f3 ] To allow canceling all packets of a connection. Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Reviewed-by: Jorgen Hansen <jhansen@vmware.com> Signed-off-by: Peng Tao <bergwolf@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Sasha Levin <alexander.levin@verizon.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 6f1848e commit 482b3f9

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

drivers/vhost/vsock.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,46 @@ vhost_transport_send_pkt(struct virtio_vsock_pkt *pkt)
218218
return len;
219219
}
220220

221+
static int
222+
vhost_transport_cancel_pkt(struct vsock_sock *vsk)
223+
{
224+
struct vhost_vsock *vsock;
225+
struct virtio_vsock_pkt *pkt, *n;
226+
int cnt = 0;
227+
LIST_HEAD(freeme);
228+
229+
/* Find the vhost_vsock according to guest context id */
230+
vsock = vhost_vsock_get(vsk->remote_addr.svm_cid);
231+
if (!vsock)
232+
return -ENODEV;
233+
234+
spin_lock_bh(&vsock->send_pkt_list_lock);
235+
list_for_each_entry_safe(pkt, n, &vsock->send_pkt_list, list) {
236+
if (pkt->vsk != vsk)
237+
continue;
238+
list_move(&pkt->list, &freeme);
239+
}
240+
spin_unlock_bh(&vsock->send_pkt_list_lock);
241+
242+
list_for_each_entry_safe(pkt, n, &freeme, list) {
243+
if (pkt->reply)
244+
cnt++;
245+
list_del(&pkt->list);
246+
virtio_transport_free_pkt(pkt);
247+
}
248+
249+
if (cnt) {
250+
struct vhost_virtqueue *tx_vq = &vsock->vqs[VSOCK_VQ_TX];
251+
int new_cnt;
252+
253+
new_cnt = atomic_sub_return(cnt, &vsock->queued_replies);
254+
if (new_cnt + cnt >= tx_vq->num && new_cnt < tx_vq->num)
255+
vhost_poll_queue(&tx_vq->poll);
256+
}
257+
258+
return 0;
259+
}
260+
221261
static struct virtio_vsock_pkt *
222262
vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq,
223263
unsigned int out, unsigned int in)
@@ -669,6 +709,7 @@ static struct virtio_transport vhost_transport = {
669709
.release = virtio_transport_release,
670710
.connect = virtio_transport_connect,
671711
.shutdown = virtio_transport_shutdown,
712+
.cancel_pkt = vhost_transport_cancel_pkt,
672713

673714
.dgram_enqueue = virtio_transport_dgram_enqueue,
674715
.dgram_dequeue = virtio_transport_dgram_dequeue,

include/net/af_vsock.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ struct vsock_transport {
100100
void (*destruct)(struct vsock_sock *);
101101
void (*release)(struct vsock_sock *);
102102

103+
/* Cancel all pending packets sent on vsock. */
104+
int (*cancel_pkt)(struct vsock_sock *vsk);
105+
103106
/* Connections. */
104107
int (*connect)(struct vsock_sock *);
105108

0 commit comments

Comments
 (0)