Peng Tao
2016-Dec-08 17:12 UTC
[PATCH v3 0/4] vsock: cancel connect packets when failing to connect
Currently, if a connect call fails on a signal or timeout (e.g., guest is still in the process of starting up), we'll just return to caller and leave the connect packet queued and they are sent even though the connection is considered a failure, which can confuse applications with unwanted false connect attempt. The patchset enables vsock (both host and guest) to cancel queued packets when a connect attempt is considered to fail. v3 changelog: - define cancel_pkt callback in struct vsock_transport rather than struct virtio_transport - rename virtio_vsock_pkt->vsk to virtio_vsock_pkt->cancel_token v2 changelog: - fix queued_replies counting and resume tx/rx when necessary Peng Tao (4): vsock: track pkt owner vsock vhost-vsock: add pkt cancel capability vsock: add pkt cancel capability vsock: cancel packets when failing to connect drivers/vhost/vsock.c | 41 ++++++++++++++++++++++++++++++++ include/linux/virtio_vsock.h | 2 ++ include/net/af_vsock.h | 3 +++ net/vmw_vsock/af_vsock.c | 14 +++++++++++ net/vmw_vsock/virtio_transport.c | 42 +++++++++++++++++++++++++++++++++ net/vmw_vsock/virtio_transport_common.c | 7 ++++++ 6 files changed, 109 insertions(+) -- 2.7.4
So that we can cancel a queued pkt later if necessary. Signed-off-by: Peng Tao <bergwolf at gmail.com> --- include/linux/virtio_vsock.h | 2 ++ net/vmw_vsock/virtio_transport_common.c | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h index 9638bfe..193ad3a 100644 --- a/include/linux/virtio_vsock.h +++ b/include/linux/virtio_vsock.h @@ -48,6 +48,7 @@ struct virtio_vsock_pkt { struct virtio_vsock_hdr hdr; struct work_struct work; struct list_head list; + void *cancel_token; /* only used for cancellation */ void *buf; u32 len; u32 off; @@ -56,6 +57,7 @@ struct virtio_vsock_pkt { struct virtio_vsock_pkt_info { u32 remote_cid, remote_port; + struct vsock_sock *vsk; struct msghdr *msg; u32 pkt_len; u16 type; diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c index a53b3a1..ef94eb8 100644 --- a/net/vmw_vsock/virtio_transport_common.c +++ b/net/vmw_vsock/virtio_transport_common.c @@ -57,6 +57,7 @@ virtio_transport_alloc_pkt(struct virtio_vsock_pkt_info *info, pkt->len = len; pkt->hdr.len = cpu_to_le32(len); pkt->reply = info->reply; + pkt->cancel_token = info->vsk; if (info->msg && len > 0) { pkt->buf = kmalloc(len, GFP_KERNEL); @@ -180,6 +181,7 @@ static int virtio_transport_send_credit_update(struct vsock_sock *vsk, struct virtio_vsock_pkt_info info = { .op = VIRTIO_VSOCK_OP_CREDIT_UPDATE, .type = type, + .vsk = vsk, }; return virtio_transport_send_pkt_info(vsk, &info); @@ -519,6 +521,7 @@ int virtio_transport_connect(struct vsock_sock *vsk) struct virtio_vsock_pkt_info info = { .op = VIRTIO_VSOCK_OP_REQUEST, .type = VIRTIO_VSOCK_TYPE_STREAM, + .vsk = vsk, }; return virtio_transport_send_pkt_info(vsk, &info); @@ -534,6 +537,7 @@ int virtio_transport_shutdown(struct vsock_sock *vsk, int mode) VIRTIO_VSOCK_SHUTDOWN_RCV : 0) | (mode & SEND_SHUTDOWN ? VIRTIO_VSOCK_SHUTDOWN_SEND : 0), + .vsk = vsk, }; return virtio_transport_send_pkt_info(vsk, &info); @@ -560,6 +564,7 @@ virtio_transport_stream_enqueue(struct vsock_sock *vsk, .type = VIRTIO_VSOCK_TYPE_STREAM, .msg = msg, .pkt_len = len, + .vsk = vsk, }; return virtio_transport_send_pkt_info(vsk, &info); @@ -581,6 +586,7 @@ static int virtio_transport_reset(struct vsock_sock *vsk, .op = VIRTIO_VSOCK_OP_RST, .type = VIRTIO_VSOCK_TYPE_STREAM, .reply = !!pkt, + .vsk = vsk, }; /* Send RST only if the original pkt is not a RST pkt */ @@ -826,6 +832,7 @@ virtio_transport_send_response(struct vsock_sock *vsk, .remote_cid = le32_to_cpu(pkt->hdr.src_cid), .remote_port = le32_to_cpu(pkt->hdr.src_port), .reply = true, + .vsk = vsk, }; return virtio_transport_send_pkt_info(vsk, &info); -- 2.7.4
To allow canceling all packets of a connection. Reviewed-by: Stefan Hajnoczi <stefanha at redhat.com> Signed-off-by: Peng Tao <bergwolf at gmail.com> --- drivers/vhost/vsock.c | 41 +++++++++++++++++++++++++++++++++++++++++ include/net/af_vsock.h | 3 +++ 2 files changed, 44 insertions(+) diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c index a504e2e0..db64d51 100644 --- a/drivers/vhost/vsock.c +++ b/drivers/vhost/vsock.c @@ -218,6 +218,46 @@ vhost_transport_send_pkt(struct virtio_vsock_pkt *pkt) return len; } +static int +vhost_transport_cancel_pkt(struct vsock_sock *vsk) +{ + struct vhost_vsock *vsock; + struct virtio_vsock_pkt *pkt, *n; + int cnt = 0; + LIST_HEAD(freeme); + + /* Find the vhost_vsock according to guest context id */ + vsock = vhost_vsock_get(vsk->remote_addr.svm_cid); + if (!vsock) + return -ENODEV; + + spin_lock_bh(&vsock->send_pkt_list_lock); + list_for_each_entry_safe(pkt, n, &vsock->send_pkt_list, list) { + if (pkt->cancel_token != (void *)vsk) + continue; + list_move(&pkt->list, &freeme); + } + spin_unlock_bh(&vsock->send_pkt_list_lock); + + list_for_each_entry_safe(pkt, n, &freeme, list) { + if (pkt->reply) + cnt++; + list_del(&pkt->list); + virtio_transport_free_pkt(pkt); + } + + if (cnt) { + struct vhost_virtqueue *tx_vq = &vsock->vqs[VSOCK_VQ_TX]; + int new_cnt; + + new_cnt = atomic_sub_return(cnt, &vsock->queued_replies); + if (new_cnt + cnt >= tx_vq->num && new_cnt < tx_vq->num) + vhost_poll_queue(&tx_vq->poll); + } + + return 0; +} + static struct virtio_vsock_pkt * vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq, unsigned int out, unsigned int in) @@ -664,6 +704,7 @@ static struct virtio_transport vhost_transport = { .release = virtio_transport_release, .connect = virtio_transport_connect, .shutdown = virtio_transport_shutdown, + .cancel_pkt = vhost_transport_cancel_pkt, .dgram_enqueue = virtio_transport_dgram_enqueue, .dgram_dequeue = virtio_transport_dgram_dequeue, diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h index f275896..ce5f100 100644 --- a/include/net/af_vsock.h +++ b/include/net/af_vsock.h @@ -100,6 +100,9 @@ struct vsock_transport { void (*destruct)(struct vsock_sock *); void (*release)(struct vsock_sock *); + /* Cancel packets belonging the same vsock */ + int (*cancel_pkt)(struct vsock_sock *vsk); + /* Connections. */ int (*connect)(struct vsock_sock *); -- 2.7.4
Stefan Hajnoczi
2016-Dec-09 10:18 UTC
[PATCH v3 0/4] vsock: cancel connect packets when failing to connect
On Fri, Dec 09, 2016 at 01:12:32AM +0800, Peng Tao wrote:> Currently, if a connect call fails on a signal or timeout (e.g., guest is still > in the process of starting up), we'll just return to caller and leave the connect > packet queued and they are sent even though the connection is considered a failure, > which can confuse applications with unwanted false connect attempt. > > The patchset enables vsock (both host and guest) to cancel queued packets when > a connect attempt is considered to fail. > > v3 changelog: > - define cancel_pkt callback in struct vsock_transport rather than struct virtio_transport > - rename virtio_vsock_pkt->vsk to virtio_vsock_pkt->cancel_token > v2 changelog: > - fix queued_replies counting and resume tx/rx when necessary > > > Peng Tao (4): > vsock: track pkt owner vsock > vhost-vsock: add pkt cancel capability > vsock: add pkt cancel capability > vsock: cancel packets when failing to connect > > drivers/vhost/vsock.c | 41 ++++++++++++++++++++++++++++++++ > include/linux/virtio_vsock.h | 2 ++ > include/net/af_vsock.h | 3 +++ > net/vmw_vsock/af_vsock.c | 14 +++++++++++ > net/vmw_vsock/virtio_transport.c | 42 +++++++++++++++++++++++++++++++++ > net/vmw_vsock/virtio_transport_common.c | 7 ++++++ > 6 files changed, 109 insertions(+)I'm happy although I pointed out two unnecessary (void*) casts. Please wait for Jorgen to go happy on the af_vsock.c changes before applying. -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 455 bytes Desc: not available URL: <http://lists.linuxfoundation.org/pipermail/virtualization/attachments/20161209/dc1370ee/attachment.sig>
Peng Tao
2016-Dec-12 09:17 UTC
[PATCH v3 0/4] vsock: cancel connect packets when failing to connect
On Fri, Dec 9, 2016 at 6:18 PM, Stefan Hajnoczi <stefanha at gmail.com> wrote:> On Fri, Dec 09, 2016 at 01:12:32AM +0800, Peng Tao wrote: >> Currently, if a connect call fails on a signal or timeout (e.g., guest is still >> in the process of starting up), we'll just return to caller and leave the connect >> packet queued and they are sent even though the connection is considered a failure, >> which can confuse applications with unwanted false connect attempt. >> >> The patchset enables vsock (both host and guest) to cancel queued packets when >> a connect attempt is considered to fail. >> >> v3 changelog: >> - define cancel_pkt callback in struct vsock_transport rather than struct virtio_transport >> - rename virtio_vsock_pkt->vsk to virtio_vsock_pkt->cancel_token >> v2 changelog: >> - fix queued_replies counting and resume tx/rx when necessary >> >> >> Peng Tao (4): >> vsock: track pkt owner vsock >> vhost-vsock: add pkt cancel capability >> vsock: add pkt cancel capability >> vsock: cancel packets when failing to connect >> >> drivers/vhost/vsock.c | 41 ++++++++++++++++++++++++++++++++ >> include/linux/virtio_vsock.h | 2 ++ >> include/net/af_vsock.h | 3 +++ >> net/vmw_vsock/af_vsock.c | 14 +++++++++++ >> net/vmw_vsock/virtio_transport.c | 42 +++++++++++++++++++++++++++++++++ >> net/vmw_vsock/virtio_transport_common.c | 7 ++++++ >> 6 files changed, 109 insertions(+) > > I'm happy although I pointed out two unnecessary (void*) casts. > > Please wait for Jorgen to go happy on the af_vsock.c changes before > applying.Thanks for reviewing! Jorgen, would you please see if the changes to af_vsock.c is OK to you? Cheers, Tao
Possibly Parallel Threads
- [PATCH v3 0/4] vsock: cancel connect packets when failing to connect
- [PATCH v3 0/4] vsock: cancel connect packets when failing to connect
- [PATCH-v5 0/4] vsock: cancel connect packets when failing to connect
- [PATCH-v5 0/4] vsock: cancel connect packets when failing to connect
- [PATCH v4 0/4] vsock: cancel connect packets when failing to connect