diff mbox series

[RFC,v6,12/22] virtio/vsock: fetch length for SEQPACKET record

Message ID 20210307180235.3465973-1-arseny.krasnov@kaspersky.com (mailing list archive)
State New, archived
Headers show
Series virtio/vsock: introduce SOCK_SEQPACKET support | expand

Commit Message

Arseny Krasnov March 7, 2021, 6:02 p.m. UTC
This adds transport callback which tries to fetch record begin marker
from socket's rx queue. It is called from af_vsock.c before reading data
packets of record.

Signed-off-by: Arseny Krasnov <arseny.krasnov@kaspersky.com>
---
 include/linux/virtio_vsock.h            |  1 +
 net/vmw_vsock/virtio_transport_common.c | 53 +++++++++++++++++++++++++
 2 files changed, 54 insertions(+)

Comments

Stefano Garzarella March 12, 2021, 3:20 p.m. UTC | #1
On Sun, Mar 07, 2021 at 09:02:31PM +0300, Arseny Krasnov wrote:
>This adds transport callback which tries to fetch record begin marker
>from socket's rx queue. It is called from af_vsock.c before reading data
>packets of record.
>
>Signed-off-by: Arseny Krasnov <arseny.krasnov@kaspersky.com>
>---
> include/linux/virtio_vsock.h            |  1 +
> net/vmw_vsock/virtio_transport_common.c | 53 +++++++++++++++++++++++++
> 2 files changed, 54 insertions(+)
>
>diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
>index 466a5832d2f5..d7edcfeb4cd2 100644
>--- a/include/linux/virtio_vsock.h
>+++ b/include/linux/virtio_vsock.h
>@@ -88,6 +88,7 @@ virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
> 			       struct msghdr *msg,
> 			       size_t len, int flags);
>
>+size_t virtio_transport_seqpacket_seq_get_len(struct vsock_sock *vsk);
> int
> virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
> 				   struct msghdr *msg,
>diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
>index 5f1e283e43f3..6fc78fec41c0 100644
>--- a/net/vmw_vsock/virtio_transport_common.c
>+++ b/net/vmw_vsock/virtio_transport_common.c
>@@ -399,6 +399,59 @@ static inline void virtio_transport_remove_pkt(struct virtio_vsock_pkt *pkt)
> 	virtio_transport_free_pkt(pkt);
> }
>
>+static size_t virtio_transport_drop_until_seq_begin(struct virtio_vsock_sock *vvs)
>+{
>+	struct virtio_vsock_pkt *pkt, *n;
>+	size_t bytes_dropped = 0;
>+
>+	list_for_each_entry_safe(pkt, n, &vvs->rx_queue, list) {
>+		if (le16_to_cpu(pkt->hdr.op) == VIRTIO_VSOCK_OP_SEQ_BEGIN)
>+			break;
>+
>+		bytes_dropped += le32_to_cpu(pkt->hdr.len);
>+		virtio_transport_dec_rx_pkt(vvs, pkt);
>+		virtio_transport_remove_pkt(pkt);
>+	}
>+
>+	return bytes_dropped;
>+}
>+
>+size_t virtio_transport_seqpacket_seq_get_len(struct vsock_sock *vsk)
>+{
>+	struct virtio_vsock_seq_hdr *seq_hdr;
>+	struct virtio_vsock_sock *vvs;
>+	struct virtio_vsock_pkt *pkt;
>+	size_t bytes_dropped;
>+
>+	vvs = vsk->trans;
>+
>+	spin_lock_bh(&vvs->rx_lock);
>+
>+	/* Fetch all orphaned 'RW' packets and send credit update. */
>+	bytes_dropped = virtio_transport_drop_until_seq_begin(vvs);
>+
>+	if (list_empty(&vvs->rx_queue))
>+		goto out;

What do we return to in this case?

IIUC we return the len of the previous packet, should we set 
vvs->seqpacket_state.user_read_seq_len to 0?

>+
>+	pkt = list_first_entry(&vvs->rx_queue, struct virtio_vsock_pkt, list);
>+
>+	vvs->seqpacket_state.user_read_copied = 0;
>+
>+	seq_hdr = (struct virtio_vsock_seq_hdr *)pkt->buf;
>+	vvs->seqpacket_state.user_read_seq_len = 
>le32_to_cpu(seq_hdr->msg_len);
>+	vvs->seqpacket_state.curr_rx_msg_id = le32_to_cpu(seq_hdr->msg_id);
>+	virtio_transport_dec_rx_pkt(vvs, pkt);
>+	virtio_transport_remove_pkt(pkt);
>+out:
>+	spin_unlock_bh(&vvs->rx_lock);
>+
>+	if (bytes_dropped)
>+		virtio_transport_send_credit_update(vsk);
>+
>+	return vvs->seqpacket_state.user_read_seq_len;
>+}
>+EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_seq_get_len);
>+
> static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
> 						 struct msghdr *msg,
> 						 bool *msg_ready)
>-- 
>2.25.1
>
Arseny Krasnov March 15, 2021, 7:49 a.m. UTC | #2
On 12.03.2021 18:20, Stefano Garzarella wrote:
> On Sun, Mar 07, 2021 at 09:02:31PM +0300, Arseny Krasnov wrote:
>> This adds transport callback which tries to fetch record begin marker
> >from socket's rx queue. It is called from af_vsock.c before reading data
>> packets of record.
>>
>> Signed-off-by: Arseny Krasnov <arseny.krasnov@kaspersky.com>
>> ---
>> include/linux/virtio_vsock.h            |  1 +
>> net/vmw_vsock/virtio_transport_common.c | 53 +++++++++++++++++++++++++
>> 2 files changed, 54 insertions(+)
>>
>> diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
>> index 466a5832d2f5..d7edcfeb4cd2 100644
>> --- a/include/linux/virtio_vsock.h
>> +++ b/include/linux/virtio_vsock.h
>> @@ -88,6 +88,7 @@ virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
>> 			       struct msghdr *msg,
>> 			       size_t len, int flags);
>>
>> +size_t virtio_transport_seqpacket_seq_get_len(struct vsock_sock *vsk);
>> int
>> virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
>> 				   struct msghdr *msg,
>> diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
>> index 5f1e283e43f3..6fc78fec41c0 100644
>> --- a/net/vmw_vsock/virtio_transport_common.c
>> +++ b/net/vmw_vsock/virtio_transport_common.c
>> @@ -399,6 +399,59 @@ static inline void virtio_transport_remove_pkt(struct virtio_vsock_pkt *pkt)
>> 	virtio_transport_free_pkt(pkt);
>> }
>>
>> +static size_t virtio_transport_drop_until_seq_begin(struct virtio_vsock_sock *vvs)
>> +{
>> +	struct virtio_vsock_pkt *pkt, *n;
>> +	size_t bytes_dropped = 0;
>> +
>> +	list_for_each_entry_safe(pkt, n, &vvs->rx_queue, list) {
>> +		if (le16_to_cpu(pkt->hdr.op) == VIRTIO_VSOCK_OP_SEQ_BEGIN)
>> +			break;
>> +
>> +		bytes_dropped += le32_to_cpu(pkt->hdr.len);
>> +		virtio_transport_dec_rx_pkt(vvs, pkt);
>> +		virtio_transport_remove_pkt(pkt);
>> +	}
>> +
>> +	return bytes_dropped;
>> +}
>> +
>> +size_t virtio_transport_seqpacket_seq_get_len(struct vsock_sock *vsk)
>> +{
>> +	struct virtio_vsock_seq_hdr *seq_hdr;
>> +	struct virtio_vsock_sock *vvs;
>> +	struct virtio_vsock_pkt *pkt;
>> +	size_t bytes_dropped;
>> +
>> +	vvs = vsk->trans;
>> +
>> +	spin_lock_bh(&vvs->rx_lock);
>> +
>> +	/* Fetch all orphaned 'RW' packets and send credit update. */
>> +	bytes_dropped = virtio_transport_drop_until_seq_begin(vvs);
>> +
>> +	if (list_empty(&vvs->rx_queue))
>> +		goto out;
> What do we return to in this case?
>
> IIUC we return the len of the previous packet, should we set 
> vvs->seqpacket_state.user_read_seq_len to 0?
Yes, i think that's true
>
>> +
>> +	pkt = list_first_entry(&vvs->rx_queue, struct virtio_vsock_pkt, list);
>> +
>> +	vvs->seqpacket_state.user_read_copied = 0;
>> +
>> +	seq_hdr = (struct virtio_vsock_seq_hdr *)pkt->buf;
>> +	vvs->seqpacket_state.user_read_seq_len = 
>> le32_to_cpu(seq_hdr->msg_len);
>> +	vvs->seqpacket_state.curr_rx_msg_id = le32_to_cpu(seq_hdr->msg_id);
>> +	virtio_transport_dec_rx_pkt(vvs, pkt);
>> +	virtio_transport_remove_pkt(pkt);
>> +out:
>> +	spin_unlock_bh(&vvs->rx_lock);
>> +
>> +	if (bytes_dropped)
>> +		virtio_transport_send_credit_update(vsk);
>> +
>> +	return vvs->seqpacket_state.user_read_seq_len;
>> +}
>> +EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_seq_get_len);
>> +
>> static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
>> 						 struct msghdr *msg,
>> 						 bool *msg_ready)
>> -- 
>> 2.25.1
>>
>
diff mbox series

Patch

diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
index 466a5832d2f5..d7edcfeb4cd2 100644
--- a/include/linux/virtio_vsock.h
+++ b/include/linux/virtio_vsock.h
@@ -88,6 +88,7 @@  virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
 			       struct msghdr *msg,
 			       size_t len, int flags);
 
+size_t virtio_transport_seqpacket_seq_get_len(struct vsock_sock *vsk);
 int
 virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
 				   struct msghdr *msg,
diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
index 5f1e283e43f3..6fc78fec41c0 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -399,6 +399,59 @@  static inline void virtio_transport_remove_pkt(struct virtio_vsock_pkt *pkt)
 	virtio_transport_free_pkt(pkt);
 }
 
+static size_t virtio_transport_drop_until_seq_begin(struct virtio_vsock_sock *vvs)
+{
+	struct virtio_vsock_pkt *pkt, *n;
+	size_t bytes_dropped = 0;
+
+	list_for_each_entry_safe(pkt, n, &vvs->rx_queue, list) {
+		if (le16_to_cpu(pkt->hdr.op) == VIRTIO_VSOCK_OP_SEQ_BEGIN)
+			break;
+
+		bytes_dropped += le32_to_cpu(pkt->hdr.len);
+		virtio_transport_dec_rx_pkt(vvs, pkt);
+		virtio_transport_remove_pkt(pkt);
+	}
+
+	return bytes_dropped;
+}
+
+size_t virtio_transport_seqpacket_seq_get_len(struct vsock_sock *vsk)
+{
+	struct virtio_vsock_seq_hdr *seq_hdr;
+	struct virtio_vsock_sock *vvs;
+	struct virtio_vsock_pkt *pkt;
+	size_t bytes_dropped;
+
+	vvs = vsk->trans;
+
+	spin_lock_bh(&vvs->rx_lock);
+
+	/* Fetch all orphaned 'RW' packets and send credit update. */
+	bytes_dropped = virtio_transport_drop_until_seq_begin(vvs);
+
+	if (list_empty(&vvs->rx_queue))
+		goto out;
+
+	pkt = list_first_entry(&vvs->rx_queue, struct virtio_vsock_pkt, list);
+
+	vvs->seqpacket_state.user_read_copied = 0;
+
+	seq_hdr = (struct virtio_vsock_seq_hdr *)pkt->buf;
+	vvs->seqpacket_state.user_read_seq_len = le32_to_cpu(seq_hdr->msg_len);
+	vvs->seqpacket_state.curr_rx_msg_id = le32_to_cpu(seq_hdr->msg_id);
+	virtio_transport_dec_rx_pkt(vvs, pkt);
+	virtio_transport_remove_pkt(pkt);
+out:
+	spin_unlock_bh(&vvs->rx_lock);
+
+	if (bytes_dropped)
+		virtio_transport_send_credit_update(vsk);
+
+	return vvs->seqpacket_state.user_read_seq_len;
+}
+EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_seq_get_len);
+
 static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
 						 struct msghdr *msg,
 						 bool *msg_ready)