This patchset implements support of SOCK_SEQPACKET for virtio
transport.
As SOCK_SEQPACKET guarantees to save record boundaries, so to
do it, new bit for field 'flags' was added: SEQ_EOR. This bit is
set to 1 in last RW packet of message.
Now as packets of one socket are not reordered neither on vsock
nor on vhost transport layers, such bit allows to restore original
message on receiver's side. If user's buffer is smaller than message
length, when all out of size data is dropped.
Maximum length of datagram is not limited as in stream socket,
because same credit logic is used. Difference with stream socket is
that user is not woken up until whole record is received or error
occurred. Implementation also supports 'MSG_TRUNC' flags.
Tests also implemented.
Thanks to [email protected] for encouragements and initial design
recommendations.
Arseny Krasnov (18):
af_vsock: update functions for connectible socket
af_vsock: separate wait data loop
af_vsock: separate receive data loop
af_vsock: implement SEQPACKET receive loop
af_vsock: implement send logic for SEQPACKET
af_vsock: rest of SEQPACKET support
af_vsock: update comments for stream sockets
virtio/vsock: set packet's type in virtio_transport_send_pkt_info()
virtio/vsock: simplify credit update function API
virtio/vsock: defines and constants for SEQPACKET
virtio/vsock: dequeue callback for SOCK_SEQPACKET
virtio/vsock: add SEQPACKET receive logic
virtio/vsock: rest of SOCK_SEQPACKET support
virtio/vsock: enable SEQPACKET for transport
vhost/vsock: enable SEQPACKET for transport
vsock/loopback: enable SEQPACKET for transport
vsock_test: add SOCK_SEQPACKET tests
virtio/vsock: update trace event for SEQPACKET
drivers/vhost/vsock.c | 44 +-
include/linux/virtio_vsock.h | 9 +
include/net/af_vsock.h | 7 +
.../events/vsock_virtio_transport_common.h | 5 +-
include/uapi/linux/virtio_vsock.h | 9 +
net/vmw_vsock/af_vsock.c | 465 +++++++++++------
net/vmw_vsock/virtio_transport.c | 25 +
net/vmw_vsock/virtio_transport_common.c | 133 ++++-
net/vmw_vsock/vsock_loopback.c | 11 +
tools/testing/vsock/util.c | 32 +-
tools/testing/vsock/util.h | 3 +
tools/testing/vsock/vsock_test.c | 116 ++++
12 files changed, 672 insertions(+), 187 deletions(-)
v9 -> v10:
General changelog:
- patch for write serialization removed from patchset
- commit messages rephrased
- RFC tag removed
Per patch changelog:
see every patch after '---' line.
v8 -> v9:
General changelog:
- see per patch change log.
Per patch changelog:
see every patch after '---' line.
v7 -> v8:
General changelog:
- whole idea is simplified: channel now considered reliable,
so SEQ_BEGIN, SEQ_END, 'msg_len' and 'msg_id' were removed.
Only thing that is used to mark end of message is bit in
'flags' field of packet header: VIRTIO_VSOCK_SEQ_EOR. Packet
with such bit set to 1 means, that this is last packet of
message.
- POSIX MSG_EOR support is removed, as there is no exact
description how it works.
- all changes to 'include/uapi/linux/virtio_vsock.h' moved
to dedicated patch, as these changes linked with patch to
spec.
- patch 'virtio/vsock: SEQPACKET feature bit support' now merged
to 'virtio/vsock: setup SEQPACKET ops for transport'.
- patch 'vhost/vsock: SEQPACKET feature bit support' now merged
to 'vhost/vsock: setup SEQPACKET ops for transport'.
Per patch changelog:
see every patch after '---' line.
v6 -> v7:
General changelog:
- virtio transport callback for message length now removed
from transport. Length of record is returned by dequeue
callback.
- function which tries to get message length now returns 0
when rx queue is empty. Also length of current message in
progress is set to 0, when message processed or error
happens.
- patches for virtio feature bit moved after patches with
transport ops.
Per patch changelog:
see every patch after '---' line.
v5 -> v6:
General changelog:
- virtio transport specific callbacks which send SEQ_BEGIN or
SEQ_END now hidden inside virtio transport. Only enqueue,
dequeue and record length callbacks are provided by transport.
- virtio feature bit for SEQPACKET socket support introduced:
VIRTIO_VSOCK_F_SEQPACKET.
- 'msg_cnt' field in 'struct virtio_vsock_seq_hdr' renamed to
'msg_id' and used as id.
Per patch changelog:
- 'af_vsock: separate wait data loop':
1) Commit message updated.
2) 'prepare_to_wait()' moved inside while loop(thanks to
Jorgen Hansen).
Marked 'Reviewed-by' with 1), but as 2) I removed R-b.
- 'af_vsock: separate receive data loop': commit message
updated.
Marked 'Reviewed-by' with that fix.
- 'af_vsock: implement SEQPACKET receive loop': style fixes.
- 'af_vsock: rest of SEQPACKET support':
1) 'module_put()' added when transport callback check failed.
2) Now only 'seqpacket_allow()' callback called to check
support of SEQPACKET by transport.
- 'af_vsock: update comments for stream sockets': commit message
updated.
Marked 'Reviewed-by' with that fix.
- 'virtio/vsock: set packet's type in send':
1) Commit message updated.
2) Parameter 'type' from 'virtio_transport_send_credit_update()'
also removed in this patch instead of in next.
- 'virtio/vsock: dequeue callback for SOCK_SEQPACKET': SEQPACKET
related state wrapped to special struct.
- 'virtio/vsock: update trace event for SEQPACKET': format strings
now not broken by new lines.
v4 -> v5:
- patches reorganized:
1) Setting of packet's type in 'virtio_transport_send_pkt_info()'
is moved to separate patch.
2) Simplifying of 'virtio_transport_send_credit_update()' is
moved to separate patch and before main virtio/vsock patches.
- style problem fixed
- in 'af_vsock: separate receive data loop' extra 'release_sock()'
removed
- added trace event fields for SEQPACKET
- in 'af_vsock: separate wait data loop':
1) 'vsock_wait_data()' removed 'goto out;'
2) Comment for invalid data amount is changed.
- in 'af_vsock: rest of SEQPACKET support', 'new_transport' pointer
check is moved after 'try_module_get()'
- in 'af_vsock: update comments for stream sockets', 'connect-oriented'
replaced with 'connection-oriented'
- in 'loopback/vsock: setup SEQPACKET ops for transport',
'loopback/vsock' replaced with 'vsock/loopback'
v3 -> v4:
- SEQPACKET specific metadata moved from packet header to payload
and called 'virtio_vsock_seq_hdr'
- record integrity check:
1) SEQ_END operation was added, which marks end of record.
2) Both SEQ_BEGIN and SEQ_END carries counter which is incremented
on every marker send.
- af_vsock.c: socket operations for STREAM and SEQPACKET call same
functions instead of having own "gates" differs only by names:
'vsock_seqpacket/stream_getsockopt()' now replaced with
'vsock_connectible_getsockopt()'.
- af_vsock.c: 'seqpacket_dequeue' callback returns error and flag that
record ready. There is no need to return number of copied bytes,
because case when record received successfully is checked at virtio
transport layer, when SEQ_END is processed. Also user doesn't need
number of copied bytes, because 'recv()' from SEQPACKET could return
error, length of users's buffer or length of whole record(both are
known in af_vsock.c).
- af_vsock.c: both wait loops in af_vsock.c(for data and space) moved
to separate functions because now both called from several places.
- af_vsock.c: 'vsock_assign_transport()' checks that 'new_transport'
pointer is not NULL and returns 'ESOCKTNOSUPPORT' instead of 'ENODEV'
if failed to use transport.
- tools/testing/vsock/vsock_test.c: rename tests
v2 -> v3:
- patches reorganized: split for prepare and implementation patches
- local variables are declared in "Reverse Christmas tree" manner
- virtio_transport_common.c: valid leXX_to_cpu() for vsock header
fields access
- af_vsock.c: 'vsock_connectible_*sockopt()' added as shared code
between stream and seqpacket sockets.
- af_vsock.c: loops in '__vsock_*_recvmsg()' refactored.
- af_vsock.c: 'vsock_wait_data()' refactored.
v1 -> v2:
- patches reordered: af_vsock.c related changes now before virtio vsock
- patches reorganized: more small patches, where +/- are not mixed
- tests for SOCK_SEQPACKET added
- all commit messages updated
- af_vsock.c: 'vsock_pre_recv_check()' inlined to
'vsock_connectible_recvmsg()'
- af_vsock.c: 'vsock_assign_transport()' returns ENODEV if transport
was not found
- virtio_transport_common.c: transport callback for seqpacket dequeue
- virtio_transport_common.c: simplified
'virtio_transport_recv_connected()'
- virtio_transport_common.c: send reset on socket and packet type
mismatch.
Signed-off-by: Arseny Krasnov <[email protected]>
--
2.25.1
Update current stream enqueue function for SEQPACKET
support:
1) Call transport's seqpacket enqueue callback.
2) Return value from enqueue function is whole record length or error
for SOCK_SEQPACKET.
Signed-off-by: Arseny Krasnov <[email protected]>
Reviewed-by: Stefano Garzarella <[email protected]>
---
include/net/af_vsock.h | 2 ++
net/vmw_vsock/af_vsock.c | 20 +++++++++++++++-----
2 files changed, 17 insertions(+), 5 deletions(-)
diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h
index 5175f5a52ce1..5860027d5173 100644
--- a/include/net/af_vsock.h
+++ b/include/net/af_vsock.h
@@ -138,6 +138,8 @@ struct vsock_transport {
/* SEQ_PACKET. */
ssize_t (*seqpacket_dequeue)(struct vsock_sock *vsk, struct msghdr *msg,
int flags, bool *msg_ready);
+ int (*seqpacket_enqueue)(struct vsock_sock *vsk, struct msghdr *msg,
+ size_t len);
/* Notification. */
int (*notify_poll_in)(struct vsock_sock *, size_t, bool *);
diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
index aede474343d1..c89f84af4744 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -1808,9 +1808,13 @@ static int vsock_connectible_sendmsg(struct socket *sock, struct msghdr *msg,
* responsibility to check how many bytes we were able to send.
*/
- written = transport->stream_enqueue(
- vsk, msg,
- len - total_written);
+ if (sk->sk_type == SOCK_SEQPACKET) {
+ written = transport->seqpacket_enqueue(vsk,
+ msg, len - total_written);
+ } else {
+ written = transport->stream_enqueue(vsk,
+ msg, len - total_written);
+ }
if (written < 0) {
err = -ENOMEM;
goto out_err;
@@ -1826,8 +1830,14 @@ static int vsock_connectible_sendmsg(struct socket *sock, struct msghdr *msg,
}
out_err:
- if (total_written > 0)
- err = total_written;
+ if (total_written > 0) {
+ /* Return number of written bytes only if:
+ * 1) SOCK_STREAM socket.
+ * 2) SOCK_SEQPACKET socket when whole buffer is sent.
+ */
+ if (sk->sk_type == SOCK_STREAM || total_written == len)
+ err = total_written;
+ }
out:
release_sock(sk);
return err;
--
2.25.1
Callback fetches RW packets from rx queue of socket until whole record
is copied(if user's buffer is full, user is not woken up). This is done
to not stall sender, because if we wake up user and it leaves syscall,
nobody will send credit update for rest of record, and sender will wait
for next enter of read syscall at receiver's side. So if user buffer is
full, we just send credit update and drop data.
Signed-off-by: Arseny Krasnov <[email protected]>
---
v9 -> v10:
1) Number of dequeued bytes incremented even in case when
user's buffer is full.
2) Use 'msg_data_left()' instead of direct access to 'msg_hdr'.
3) Rename variable 'err' to 'dequeued_len', in case of error
it has negative value.
include/linux/virtio_vsock.h | 5 ++
net/vmw_vsock/virtio_transport_common.c | 65 +++++++++++++++++++++++++
2 files changed, 70 insertions(+)
diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
index dc636b727179..02acf6e9ae04 100644
--- a/include/linux/virtio_vsock.h
+++ b/include/linux/virtio_vsock.h
@@ -80,6 +80,11 @@ virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
struct msghdr *msg,
size_t len, int flags);
+ssize_t
+virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
+ struct msghdr *msg,
+ int flags,
+ bool *msg_ready);
s64 virtio_transport_stream_has_data(struct vsock_sock *vsk);
s64 virtio_transport_stream_has_space(struct vsock_sock *vsk);
diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
index ad0d34d41444..61349b2ea7fe 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -393,6 +393,59 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
return err;
}
+static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
+ struct msghdr *msg,
+ int flags,
+ bool *msg_ready)
+{
+ struct virtio_vsock_sock *vvs = vsk->trans;
+ struct virtio_vsock_pkt *pkt;
+ int dequeued_len = 0;
+ size_t user_buf_len = msg_data_left(msg);
+
+ *msg_ready = false;
+ spin_lock_bh(&vvs->rx_lock);
+
+ while (!*msg_ready && !list_empty(&vvs->rx_queue) && dequeued_len >= 0) {
+ size_t bytes_to_copy;
+ size_t pkt_len;
+
+ pkt = list_first_entry(&vvs->rx_queue, struct virtio_vsock_pkt, list);
+ pkt_len = (size_t)le32_to_cpu(pkt->hdr.len);
+ bytes_to_copy = min(user_buf_len, pkt_len);
+
+ if (bytes_to_copy) {
+ /* sk_lock is held by caller so no one else can dequeue.
+ * Unlock rx_lock since memcpy_to_msg() may sleep.
+ */
+ spin_unlock_bh(&vvs->rx_lock);
+
+ if (memcpy_to_msg(msg, pkt->buf, bytes_to_copy))
+ dequeued_len = -EINVAL;
+ else
+ user_buf_len -= bytes_to_copy;
+
+ spin_lock_bh(&vvs->rx_lock);
+ }
+
+ if (dequeued_len >= 0)
+ dequeued_len += pkt_len;
+
+ if (le32_to_cpu(pkt->hdr.flags) & VIRTIO_VSOCK_SEQ_EOR)
+ *msg_ready = true;
+
+ virtio_transport_dec_rx_pkt(vvs, pkt);
+ list_del(&pkt->list);
+ virtio_transport_free_pkt(pkt);
+ }
+
+ spin_unlock_bh(&vvs->rx_lock);
+
+ virtio_transport_send_credit_update(vsk);
+
+ return dequeued_len;
+}
+
ssize_t
virtio_transport_stream_dequeue(struct vsock_sock *vsk,
struct msghdr *msg,
@@ -405,6 +458,18 @@ virtio_transport_stream_dequeue(struct vsock_sock *vsk,
}
EXPORT_SYMBOL_GPL(virtio_transport_stream_dequeue);
+ssize_t
+virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
+ struct msghdr *msg,
+ int flags, bool *msg_ready)
+{
+ if (flags & MSG_PEEK)
+ return -EOPNOTSUPP;
+
+ return virtio_transport_seqpacket_do_dequeue(vsk, msg, flags, msg_ready);
+}
+EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_dequeue);
+
int
virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
struct msghdr *msg,
--
2.25.1
Small updates to make SOCK_SEQPACKET work:
1) Send SHUTDOWN on socket close for SEQPACKET type.
2) Set SEQPACKET packet type during send.
3) Set 'VIRTIO_VSOCK_SEQ_EOR' bit in flags for last
packet of message.
Signed-off-by: Arseny Krasnov <[email protected]>
---
v9 -> v10:
1) Use 'msg_data_left()' instead of direct access to 'msg_hdr'.
2) Commit message updated.
3) Add check for socket type when setting SEQ_EOR bit.
include/linux/virtio_vsock.h | 4 ++++
net/vmw_vsock/virtio_transport_common.c | 18 ++++++++++++++++--
2 files changed, 20 insertions(+), 2 deletions(-)
diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
index 02acf6e9ae04..7360ab7ea0af 100644
--- a/include/linux/virtio_vsock.h
+++ b/include/linux/virtio_vsock.h
@@ -80,6 +80,10 @@ virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
struct msghdr *msg,
size_t len, int flags);
+int
+virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk,
+ struct msghdr *msg,
+ size_t len);
ssize_t
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 a6f8b0f39775..f7a3281b3eab 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -74,6 +74,11 @@ virtio_transport_alloc_pkt(struct virtio_vsock_pkt_info *info,
err = memcpy_from_msg(pkt->buf, info->msg, len);
if (err)
goto out;
+
+ if (msg_data_left(info->msg) == 0 &&
+ info->type == VIRTIO_VSOCK_TYPE_SEQPACKET)
+ pkt->hdr.flags = cpu_to_le32(info->flags |
+ VIRTIO_VSOCK_SEQ_EOR);
}
trace_virtio_transport_alloc_pkt(src_cid, src_port,
@@ -187,7 +192,7 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
struct virtio_vsock_pkt *pkt;
u32 pkt_len = info->pkt_len;
- info->type = VIRTIO_VSOCK_TYPE_STREAM;
+ info->type = virtio_transport_get_type(sk_vsock(vsk));
t_ops = virtio_transport_get_ops(vsk);
if (unlikely(!t_ops))
@@ -478,6 +483,15 @@ virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
}
EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_dequeue);
+int
+virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk,
+ struct msghdr *msg,
+ size_t len)
+{
+ return virtio_transport_stream_enqueue(vsk, msg, len);
+}
+EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_enqueue);
+
int
virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
struct msghdr *msg,
@@ -912,7 +926,7 @@ void virtio_transport_release(struct vsock_sock *vsk)
struct sock *sk = &vsk->sk;
bool remove_sock = true;
- if (sk->sk_type == SOCK_STREAM)
+ if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)
remove_sock = virtio_transport_close(vsk);
if (remove_sock) {
--
2.25.1
There is no need to set type of packet which differs from type
of socket, so move passing type of packet from 'info' structure
to 'virtio_transport_send_pkt_info()' function. Since at current
time only stream type is supported, set it directly in 'virtio_
transport_send_pkt_info()', so callers don't need to set it.
Signed-off-by: Arseny Krasnov <[email protected]>
Reviewed-by: Stefano Garzarella <[email protected]>
---
net/vmw_vsock/virtio_transport_common.c | 19 +++++--------------
1 file changed, 5 insertions(+), 14 deletions(-)
diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
index 902cb6dd710b..6503a8370130 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -179,6 +179,8 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
struct virtio_vsock_pkt *pkt;
u32 pkt_len = info->pkt_len;
+ info->type = VIRTIO_VSOCK_TYPE_STREAM;
+
t_ops = virtio_transport_get_ops(vsk);
if (unlikely(!t_ops))
return -EFAULT;
@@ -270,12 +272,10 @@ void virtio_transport_put_credit(struct virtio_vsock_sock *vvs, u32 credit)
EXPORT_SYMBOL_GPL(virtio_transport_put_credit);
static int virtio_transport_send_credit_update(struct vsock_sock *vsk,
- int type,
struct virtio_vsock_hdr *hdr)
{
struct virtio_vsock_pkt_info info = {
.op = VIRTIO_VSOCK_OP_CREDIT_UPDATE,
- .type = type,
.vsk = vsk,
};
@@ -383,11 +383,8 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
* messages, we set the limit to a high value. TODO: experiment
* with different values.
*/
- if (free_space < VIRTIO_VSOCK_MAX_PKT_BUF_SIZE) {
- virtio_transport_send_credit_update(vsk,
- VIRTIO_VSOCK_TYPE_STREAM,
- NULL);
- }
+ if (free_space < VIRTIO_VSOCK_MAX_PKT_BUF_SIZE)
+ virtio_transport_send_credit_update(vsk, NULL);
return total;
@@ -496,8 +493,7 @@ void virtio_transport_notify_buffer_size(struct vsock_sock *vsk, u64 *val)
vvs->buf_alloc = *val;
- virtio_transport_send_credit_update(vsk, VIRTIO_VSOCK_TYPE_STREAM,
- NULL);
+ virtio_transport_send_credit_update(vsk, NULL);
}
EXPORT_SYMBOL_GPL(virtio_transport_notify_buffer_size);
@@ -624,7 +620,6 @@ 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,
};
@@ -636,7 +631,6 @@ int virtio_transport_shutdown(struct vsock_sock *vsk, int mode)
{
struct virtio_vsock_pkt_info info = {
.op = VIRTIO_VSOCK_OP_SHUTDOWN,
- .type = VIRTIO_VSOCK_TYPE_STREAM,
.flags = (mode & RCV_SHUTDOWN ?
VIRTIO_VSOCK_SHUTDOWN_RCV : 0) |
(mode & SEND_SHUTDOWN ?
@@ -665,7 +659,6 @@ virtio_transport_stream_enqueue(struct vsock_sock *vsk,
{
struct virtio_vsock_pkt_info info = {
.op = VIRTIO_VSOCK_OP_RW,
- .type = VIRTIO_VSOCK_TYPE_STREAM,
.msg = msg,
.pkt_len = len,
.vsk = vsk,
@@ -688,7 +681,6 @@ static int virtio_transport_reset(struct vsock_sock *vsk,
{
struct virtio_vsock_pkt_info info = {
.op = VIRTIO_VSOCK_OP_RST,
- .type = VIRTIO_VSOCK_TYPE_STREAM,
.reply = !!pkt,
.vsk = vsk,
};
@@ -1000,7 +992,6 @@ virtio_transport_send_response(struct vsock_sock *vsk,
{
struct virtio_vsock_pkt_info info = {
.op = VIRTIO_VSOCK_OP_RESPONSE,
- .type = VIRTIO_VSOCK_TYPE_STREAM,
.remote_cid = le64_to_cpu(pkt->hdr.src_cid),
.remote_port = le32_to_cpu(pkt->hdr.src_port),
.reply = true,
--
2.25.1
Hi Arseny,
On Thu, May 20, 2021 at 10:13:53PM +0300, Arseny Krasnov wrote:
> This patchset implements support of SOCK_SEQPACKET for virtio
>transport.
I'll carefully review and test this series next Monday, in the mean time
I think we should have at least an agreement about the changes that
regards virtio-spec before merge this series, to avoid any compatibility
issues.
Do you plan to send a new version of the specification changes?
Thanks,
Stefano
> As SOCK_SEQPACKET guarantees to save record boundaries, so to
>do it, new bit for field 'flags' was added: SEQ_EOR. This bit is
>set to 1 in last RW packet of message.
> Now as packets of one socket are not reordered neither on vsock
>nor on vhost transport layers, such bit allows to restore original
>message on receiver's side. If user's buffer is smaller than message
>length, when all out of size data is dropped.
> Maximum length of datagram is not limited as in stream socket,
>because same credit logic is used. Difference with stream socket is
>that user is not woken up until whole record is received or error
>occurred. Implementation also supports 'MSG_TRUNC' flags.
> Tests also implemented.
>
> Thanks to [email protected] for encouragements and initial design
>recommendations.
>
> Arseny Krasnov (18):
> af_vsock: update functions for connectible socket
> af_vsock: separate wait data loop
> af_vsock: separate receive data loop
> af_vsock: implement SEQPACKET receive loop
> af_vsock: implement send logic for SEQPACKET
> af_vsock: rest of SEQPACKET support
> af_vsock: update comments for stream sockets
> virtio/vsock: set packet's type in virtio_transport_send_pkt_info()
> virtio/vsock: simplify credit update function API
> virtio/vsock: defines and constants for SEQPACKET
> virtio/vsock: dequeue callback for SOCK_SEQPACKET
> virtio/vsock: add SEQPACKET receive logic
> virtio/vsock: rest of SOCK_SEQPACKET support
> virtio/vsock: enable SEQPACKET for transport
> vhost/vsock: enable SEQPACKET for transport
> vsock/loopback: enable SEQPACKET for transport
> vsock_test: add SOCK_SEQPACKET tests
> virtio/vsock: update trace event for SEQPACKET
>
> drivers/vhost/vsock.c | 44 +-
> include/linux/virtio_vsock.h | 9 +
> include/net/af_vsock.h | 7 +
> .../events/vsock_virtio_transport_common.h | 5 +-
> include/uapi/linux/virtio_vsock.h | 9 +
> net/vmw_vsock/af_vsock.c | 465 +++++++++++------
> net/vmw_vsock/virtio_transport.c | 25 +
> net/vmw_vsock/virtio_transport_common.c | 133 ++++-
> net/vmw_vsock/vsock_loopback.c | 11 +
> tools/testing/vsock/util.c | 32 +-
> tools/testing/vsock/util.h | 3 +
> tools/testing/vsock/vsock_test.c | 116 ++++
> 12 files changed, 672 insertions(+), 187 deletions(-)
>
> v9 -> v10:
> General changelog:
> - patch for write serialization removed from patchset
> - commit messages rephrased
> - RFC tag removed
>
> Per patch changelog:
> see every patch after '---' line.
>
> v8 -> v9:
> General changelog:
> - see per patch change log.
>
> Per patch changelog:
> see every patch after '---' line.
>
> v7 -> v8:
> General changelog:
> - whole idea is simplified: channel now considered reliable,
> so SEQ_BEGIN, SEQ_END, 'msg_len' and 'msg_id' were removed.
> Only thing that is used to mark end of message is bit in
> 'flags' field of packet header: VIRTIO_VSOCK_SEQ_EOR. Packet
> with such bit set to 1 means, that this is last packet of
> message.
>
> - POSIX MSG_EOR support is removed, as there is no exact
> description how it works.
>
> - all changes to 'include/uapi/linux/virtio_vsock.h' moved
> to dedicated patch, as these changes linked with patch to
> spec.
>
> - patch 'virtio/vsock: SEQPACKET feature bit support' now merged
> to 'virtio/vsock: setup SEQPACKET ops for transport'.
>
> - patch 'vhost/vsock: SEQPACKET feature bit support' now merged
> to 'vhost/vsock: setup SEQPACKET ops for transport'.
>
> Per patch changelog:
> see every patch after '---' line.
>
> v6 -> v7:
> General changelog:
> - virtio transport callback for message length now removed
> from transport. Length of record is returned by dequeue
> callback.
>
> - function which tries to get message length now returns 0
> when rx queue is empty. Also length of current message in
> progress is set to 0, when message processed or error
> happens.
>
> - patches for virtio feature bit moved after patches with
> transport ops.
>
> Per patch changelog:
> see every patch after '---' line.
>
> v5 -> v6:
> General changelog:
> - virtio transport specific callbacks which send SEQ_BEGIN or
> SEQ_END now hidden inside virtio transport. Only enqueue,
> dequeue and record length callbacks are provided by transport.
>
> - virtio feature bit for SEQPACKET socket support introduced:
> VIRTIO_VSOCK_F_SEQPACKET.
>
> - 'msg_cnt' field in 'struct virtio_vsock_seq_hdr' renamed to
> 'msg_id' and used as id.
>
> Per patch changelog:
> - 'af_vsock: separate wait data loop':
> 1) Commit message updated.
> 2) 'prepare_to_wait()' moved inside while loop(thanks to
> Jorgen Hansen).
> Marked 'Reviewed-by' with 1), but as 2) I removed R-b.
>
> - 'af_vsock: separate receive data loop': commit message
> updated.
> Marked 'Reviewed-by' with that fix.
>
> - 'af_vsock: implement SEQPACKET receive loop': style fixes.
>
> - 'af_vsock: rest of SEQPACKET support':
> 1) 'module_put()' added when transport callback check failed.
> 2) Now only 'seqpacket_allow()' callback called to check
> support of SEQPACKET by transport.
>
> - 'af_vsock: update comments for stream sockets': commit message
> updated.
> Marked 'Reviewed-by' with that fix.
>
> - 'virtio/vsock: set packet's type in send':
> 1) Commit message updated.
> 2) Parameter 'type' from 'virtio_transport_send_credit_update()'
> also removed in this patch instead of in next.
>
> - 'virtio/vsock: dequeue callback for SOCK_SEQPACKET': SEQPACKET
> related state wrapped to special struct.
>
> - 'virtio/vsock: update trace event for SEQPACKET': format strings
> now not broken by new lines.
>
> v4 -> v5:
> - patches reorganized:
> 1) Setting of packet's type in 'virtio_transport_send_pkt_info()'
> is moved to separate patch.
> 2) Simplifying of 'virtio_transport_send_credit_update()' is
> moved to separate patch and before main virtio/vsock patches.
> - style problem fixed
> - in 'af_vsock: separate receive data loop' extra 'release_sock()'
> removed
> - added trace event fields for SEQPACKET
> - in 'af_vsock: separate wait data loop':
> 1) 'vsock_wait_data()' removed 'goto out;'
> 2) Comment for invalid data amount is changed.
> - in 'af_vsock: rest of SEQPACKET support', 'new_transport' pointer
> check is moved after 'try_module_get()'
> - in 'af_vsock: update comments for stream sockets', 'connect-oriented'
> replaced with 'connection-oriented'
> - in 'loopback/vsock: setup SEQPACKET ops for transport',
> 'loopback/vsock' replaced with 'vsock/loopback'
>
> v3 -> v4:
> - SEQPACKET specific metadata moved from packet header to payload
> and called 'virtio_vsock_seq_hdr'
> - record integrity check:
> 1) SEQ_END operation was added, which marks end of record.
> 2) Both SEQ_BEGIN and SEQ_END carries counter which is incremented
> on every marker send.
> - af_vsock.c: socket operations for STREAM and SEQPACKET call same
> functions instead of having own "gates" differs only by names:
> 'vsock_seqpacket/stream_getsockopt()' now replaced with
> 'vsock_connectible_getsockopt()'.
> - af_vsock.c: 'seqpacket_dequeue' callback returns error and flag that
> record ready. There is no need to return number of copied bytes,
> because case when record received successfully is checked at virtio
> transport layer, when SEQ_END is processed. Also user doesn't need
> number of copied bytes, because 'recv()' from SEQPACKET could return
> error, length of users's buffer or length of whole record(both are
> known in af_vsock.c).
> - af_vsock.c: both wait loops in af_vsock.c(for data and space) moved
> to separate functions because now both called from several places.
> - af_vsock.c: 'vsock_assign_transport()' checks that 'new_transport'
> pointer is not NULL and returns 'ESOCKTNOSUPPORT' instead of 'ENODEV'
> if failed to use transport.
> - tools/testing/vsock/vsock_test.c: rename tests
>
> v2 -> v3:
> - patches reorganized: split for prepare and implementation patches
> - local variables are declared in "Reverse Christmas tree" manner
> - virtio_transport_common.c: valid leXX_to_cpu() for vsock header
> fields access
> - af_vsock.c: 'vsock_connectible_*sockopt()' added as shared code
> between stream and seqpacket sockets.
> - af_vsock.c: loops in '__vsock_*_recvmsg()' refactored.
> - af_vsock.c: 'vsock_wait_data()' refactored.
>
> v1 -> v2:
> - patches reordered: af_vsock.c related changes now before virtio vsock
> - patches reorganized: more small patches, where +/- are not mixed
> - tests for SOCK_SEQPACKET added
> - all commit messages updated
> - af_vsock.c: 'vsock_pre_recv_check()' inlined to
> 'vsock_connectible_recvmsg()'
> - af_vsock.c: 'vsock_assign_transport()' returns ENODEV if transport
> was not found
> - virtio_transport_common.c: transport callback for seqpacket dequeue
> - virtio_transport_common.c: simplified
> 'virtio_transport_recv_connected()'
> - virtio_transport_common.c: send reset on socket and packet type
> mismatch.
>
>Signed-off-by: Arseny Krasnov <[email protected]>
>
>--
>2.25.1
>
On 21.05.2021 10:55, Stefano Garzarella wrote:
> Hi Arseny,
>
> On Thu, May 20, 2021 at 10:13:53PM +0300, Arseny Krasnov wrote:
>> This patchset implements support of SOCK_SEQPACKET for virtio
>> transport.
> I'll carefully review and test this series next Monday, in the mean time
> I think we should have at least an agreement about the changes that
> regards virtio-spec before merge this series, to avoid any compatibility
> issues.
>
> Do you plan to send a new version of the specification changes?
>
> Thanks,
> Stefano
Hello, sorry for long answer. I'm on vacation now, but i plan to send
it in next several days, because with current implementation it is short
Thank You
>
>> As SOCK_SEQPACKET guarantees to save record boundaries, so to
>> do it, new bit for field 'flags' was added: SEQ_EOR. This bit is
>> set to 1 in last RW packet of message.
>> Now as packets of one socket are not reordered neither on vsock
>> nor on vhost transport layers, such bit allows to restore original
>> message on receiver's side. If user's buffer is smaller than message
>> length, when all out of size data is dropped.
>> Maximum length of datagram is not limited as in stream socket,
>> because same credit logic is used. Difference with stream socket is
>> that user is not woken up until whole record is received or error
>> occurred. Implementation also supports 'MSG_TRUNC' flags.
>> Tests also implemented.
>>
>> Thanks to [email protected] for encouragements and initial design
>> recommendations.
>>
>> Arseny Krasnov (18):
>> af_vsock: update functions for connectible socket
>> af_vsock: separate wait data loop
>> af_vsock: separate receive data loop
>> af_vsock: implement SEQPACKET receive loop
>> af_vsock: implement send logic for SEQPACKET
>> af_vsock: rest of SEQPACKET support
>> af_vsock: update comments for stream sockets
>> virtio/vsock: set packet's type in virtio_transport_send_pkt_info()
>> virtio/vsock: simplify credit update function API
>> virtio/vsock: defines and constants for SEQPACKET
>> virtio/vsock: dequeue callback for SOCK_SEQPACKET
>> virtio/vsock: add SEQPACKET receive logic
>> virtio/vsock: rest of SOCK_SEQPACKET support
>> virtio/vsock: enable SEQPACKET for transport
>> vhost/vsock: enable SEQPACKET for transport
>> vsock/loopback: enable SEQPACKET for transport
>> vsock_test: add SOCK_SEQPACKET tests
>> virtio/vsock: update trace event for SEQPACKET
>>
>> drivers/vhost/vsock.c | 44 +-
>> include/linux/virtio_vsock.h | 9 +
>> include/net/af_vsock.h | 7 +
>> .../events/vsock_virtio_transport_common.h | 5 +-
>> include/uapi/linux/virtio_vsock.h | 9 +
>> net/vmw_vsock/af_vsock.c | 465 +++++++++++------
>> net/vmw_vsock/virtio_transport.c | 25 +
>> net/vmw_vsock/virtio_transport_common.c | 133 ++++-
>> net/vmw_vsock/vsock_loopback.c | 11 +
>> tools/testing/vsock/util.c | 32 +-
>> tools/testing/vsock/util.h | 3 +
>> tools/testing/vsock/vsock_test.c | 116 ++++
>> 12 files changed, 672 insertions(+), 187 deletions(-)
>>
>> v9 -> v10:
>> General changelog:
>> - patch for write serialization removed from patchset
>> - commit messages rephrased
>> - RFC tag removed
>>
>> Per patch changelog:
>> see every patch after '---' line.
>>
>> v8 -> v9:
>> General changelog:
>> - see per patch change log.
>>
>> Per patch changelog:
>> see every patch after '---' line.
>>
>> v7 -> v8:
>> General changelog:
>> - whole idea is simplified: channel now considered reliable,
>> so SEQ_BEGIN, SEQ_END, 'msg_len' and 'msg_id' were removed.
>> Only thing that is used to mark end of message is bit in
>> 'flags' field of packet header: VIRTIO_VSOCK_SEQ_EOR. Packet
>> with such bit set to 1 means, that this is last packet of
>> message.
>>
>> - POSIX MSG_EOR support is removed, as there is no exact
>> description how it works.
>>
>> - all changes to 'include/uapi/linux/virtio_vsock.h' moved
>> to dedicated patch, as these changes linked with patch to
>> spec.
>>
>> - patch 'virtio/vsock: SEQPACKET feature bit support' now merged
>> to 'virtio/vsock: setup SEQPACKET ops for transport'.
>>
>> - patch 'vhost/vsock: SEQPACKET feature bit support' now merged
>> to 'vhost/vsock: setup SEQPACKET ops for transport'.
>>
>> Per patch changelog:
>> see every patch after '---' line.
>>
>> v6 -> v7:
>> General changelog:
>> - virtio transport callback for message length now removed
>> from transport. Length of record is returned by dequeue
>> callback.
>>
>> - function which tries to get message length now returns 0
>> when rx queue is empty. Also length of current message in
>> progress is set to 0, when message processed or error
>> happens.
>>
>> - patches for virtio feature bit moved after patches with
>> transport ops.
>>
>> Per patch changelog:
>> see every patch after '---' line.
>>
>> v5 -> v6:
>> General changelog:
>> - virtio transport specific callbacks which send SEQ_BEGIN or
>> SEQ_END now hidden inside virtio transport. Only enqueue,
>> dequeue and record length callbacks are provided by transport.
>>
>> - virtio feature bit for SEQPACKET socket support introduced:
>> VIRTIO_VSOCK_F_SEQPACKET.
>>
>> - 'msg_cnt' field in 'struct virtio_vsock_seq_hdr' renamed to
>> 'msg_id' and used as id.
>>
>> Per patch changelog:
>> - 'af_vsock: separate wait data loop':
>> 1) Commit message updated.
>> 2) 'prepare_to_wait()' moved inside while loop(thanks to
>> Jorgen Hansen).
>> Marked 'Reviewed-by' with 1), but as 2) I removed R-b.
>>
>> - 'af_vsock: separate receive data loop': commit message
>> updated.
>> Marked 'Reviewed-by' with that fix.
>>
>> - 'af_vsock: implement SEQPACKET receive loop': style fixes.
>>
>> - 'af_vsock: rest of SEQPACKET support':
>> 1) 'module_put()' added when transport callback check failed.
>> 2) Now only 'seqpacket_allow()' callback called to check
>> support of SEQPACKET by transport.
>>
>> - 'af_vsock: update comments for stream sockets': commit message
>> updated.
>> Marked 'Reviewed-by' with that fix.
>>
>> - 'virtio/vsock: set packet's type in send':
>> 1) Commit message updated.
>> 2) Parameter 'type' from 'virtio_transport_send_credit_update()'
>> also removed in this patch instead of in next.
>>
>> - 'virtio/vsock: dequeue callback for SOCK_SEQPACKET': SEQPACKET
>> related state wrapped to special struct.
>>
>> - 'virtio/vsock: update trace event for SEQPACKET': format strings
>> now not broken by new lines.
>>
>> v4 -> v5:
>> - patches reorganized:
>> 1) Setting of packet's type in 'virtio_transport_send_pkt_info()'
>> is moved to separate patch.
>> 2) Simplifying of 'virtio_transport_send_credit_update()' is
>> moved to separate patch and before main virtio/vsock patches.
>> - style problem fixed
>> - in 'af_vsock: separate receive data loop' extra 'release_sock()'
>> removed
>> - added trace event fields for SEQPACKET
>> - in 'af_vsock: separate wait data loop':
>> 1) 'vsock_wait_data()' removed 'goto out;'
>> 2) Comment for invalid data amount is changed.
>> - in 'af_vsock: rest of SEQPACKET support', 'new_transport' pointer
>> check is moved after 'try_module_get()'
>> - in 'af_vsock: update comments for stream sockets', 'connect-oriented'
>> replaced with 'connection-oriented'
>> - in 'loopback/vsock: setup SEQPACKET ops for transport',
>> 'loopback/vsock' replaced with 'vsock/loopback'
>>
>> v3 -> v4:
>> - SEQPACKET specific metadata moved from packet header to payload
>> and called 'virtio_vsock_seq_hdr'
>> - record integrity check:
>> 1) SEQ_END operation was added, which marks end of record.
>> 2) Both SEQ_BEGIN and SEQ_END carries counter which is incremented
>> on every marker send.
>> - af_vsock.c: socket operations for STREAM and SEQPACKET call same
>> functions instead of having own "gates" differs only by names:
>> 'vsock_seqpacket/stream_getsockopt()' now replaced with
>> 'vsock_connectible_getsockopt()'.
>> - af_vsock.c: 'seqpacket_dequeue' callback returns error and flag that
>> record ready. There is no need to return number of copied bytes,
>> because case when record received successfully is checked at virtio
>> transport layer, when SEQ_END is processed. Also user doesn't need
>> number of copied bytes, because 'recv()' from SEQPACKET could return
>> error, length of users's buffer or length of whole record(both are
>> known in af_vsock.c).
>> - af_vsock.c: both wait loops in af_vsock.c(for data and space) moved
>> to separate functions because now both called from several places.
>> - af_vsock.c: 'vsock_assign_transport()' checks that 'new_transport'
>> pointer is not NULL and returns 'ESOCKTNOSUPPORT' instead of 'ENODEV'
>> if failed to use transport.
>> - tools/testing/vsock/vsock_test.c: rename tests
>>
>> v2 -> v3:
>> - patches reorganized: split for prepare and implementation patches
>> - local variables are declared in "Reverse Christmas tree" manner
>> - virtio_transport_common.c: valid leXX_to_cpu() for vsock header
>> fields access
>> - af_vsock.c: 'vsock_connectible_*sockopt()' added as shared code
>> between stream and seqpacket sockets.
>> - af_vsock.c: loops in '__vsock_*_recvmsg()' refactored.
>> - af_vsock.c: 'vsock_wait_data()' refactored.
>>
>> v1 -> v2:
>> - patches reordered: af_vsock.c related changes now before virtio vsock
>> - patches reorganized: more small patches, where +/- are not mixed
>> - tests for SOCK_SEQPACKET added
>> - all commit messages updated
>> - af_vsock.c: 'vsock_pre_recv_check()' inlined to
>> 'vsock_connectible_recvmsg()'
>> - af_vsock.c: 'vsock_assign_transport()' returns ENODEV if transport
>> was not found
>> - virtio_transport_common.c: transport callback for seqpacket dequeue
>> - virtio_transport_common.c: simplified
>> 'virtio_transport_recv_connected()'
>> - virtio_transport_common.c: send reset on socket and packet type
>> mismatch.
>>
>> Signed-off-by: Arseny Krasnov <[email protected]>
>>
>> --
>> 2.25.1
>>
>
On 23.05.2021 15:14, Arseny Krasnov wrote:
> On 21.05.2021 10:55, Stefano Garzarella wrote:
>> Hi Arseny,
>>
>> On Thu, May 20, 2021 at 10:13:53PM +0300, Arseny Krasnov wrote:
>>> This patchset implements support of SOCK_SEQPACKET for virtio
>>> transport.
>> I'll carefully review and test this series next Monday, in the mean time
>> I think we should have at least an agreement about the changes that
>> regards virtio-spec before merge this series, to avoid any compatibility
>> issues.
>>
>> Do you plan to send a new version of the specification changes?
>>
>> Thanks,
>> Stefano
> Hello, sorry for long answer. I'm on vacation now, but i plan to send
>
> it in next several days, because with current implementation it is short
>
>
> Thank You
Hello, here is spec patch:
https://lists.oasis-open.org/archives/virtio-comment/202105/msg00017.html
Let's discuss it
Thank You
>>> As SOCK_SEQPACKET guarantees to save record boundaries, so to
>>> do it, new bit for field 'flags' was added: SEQ_EOR. This bit is
>>> set to 1 in last RW packet of message.
>>> Now as packets of one socket are not reordered neither on vsock
>>> nor on vhost transport layers, such bit allows to restore original
>>> message on receiver's side. If user's buffer is smaller than message
>>> length, when all out of size data is dropped.
>>> Maximum length of datagram is not limited as in stream socket,
>>> because same credit logic is used. Difference with stream socket is
>>> that user is not woken up until whole record is received or error
>>> occurred. Implementation also supports 'MSG_TRUNC' flags.
>>> Tests also implemented.
>>>
>>> Thanks to [email protected] for encouragements and initial design
>>> recommendations.
>>>
>>> Arseny Krasnov (18):
>>> af_vsock: update functions for connectible socket
>>> af_vsock: separate wait data loop
>>> af_vsock: separate receive data loop
>>> af_vsock: implement SEQPACKET receive loop
>>> af_vsock: implement send logic for SEQPACKET
>>> af_vsock: rest of SEQPACKET support
>>> af_vsock: update comments for stream sockets
>>> virtio/vsock: set packet's type in virtio_transport_send_pkt_info()
>>> virtio/vsock: simplify credit update function API
>>> virtio/vsock: defines and constants for SEQPACKET
>>> virtio/vsock: dequeue callback for SOCK_SEQPACKET
>>> virtio/vsock: add SEQPACKET receive logic
>>> virtio/vsock: rest of SOCK_SEQPACKET support
>>> virtio/vsock: enable SEQPACKET for transport
>>> vhost/vsock: enable SEQPACKET for transport
>>> vsock/loopback: enable SEQPACKET for transport
>>> vsock_test: add SOCK_SEQPACKET tests
>>> virtio/vsock: update trace event for SEQPACKET
>>>
>>> drivers/vhost/vsock.c | 44 +-
>>> include/linux/virtio_vsock.h | 9 +
>>> include/net/af_vsock.h | 7 +
>>> .../events/vsock_virtio_transport_common.h | 5 +-
>>> include/uapi/linux/virtio_vsock.h | 9 +
>>> net/vmw_vsock/af_vsock.c | 465 +++++++++++------
>>> net/vmw_vsock/virtio_transport.c | 25 +
>>> net/vmw_vsock/virtio_transport_common.c | 133 ++++-
>>> net/vmw_vsock/vsock_loopback.c | 11 +
>>> tools/testing/vsock/util.c | 32 +-
>>> tools/testing/vsock/util.h | 3 +
>>> tools/testing/vsock/vsock_test.c | 116 ++++
>>> 12 files changed, 672 insertions(+), 187 deletions(-)
>>>
>>> v9 -> v10:
>>> General changelog:
>>> - patch for write serialization removed from patchset
>>> - commit messages rephrased
>>> - RFC tag removed
>>>
>>> Per patch changelog:
>>> see every patch after '---' line.
>>>
>>> v8 -> v9:
>>> General changelog:
>>> - see per patch change log.
>>>
>>> Per patch changelog:
>>> see every patch after '---' line.
>>>
>>> v7 -> v8:
>>> General changelog:
>>> - whole idea is simplified: channel now considered reliable,
>>> so SEQ_BEGIN, SEQ_END, 'msg_len' and 'msg_id' were removed.
>>> Only thing that is used to mark end of message is bit in
>>> 'flags' field of packet header: VIRTIO_VSOCK_SEQ_EOR. Packet
>>> with such bit set to 1 means, that this is last packet of
>>> message.
>>>
>>> - POSIX MSG_EOR support is removed, as there is no exact
>>> description how it works.
>>>
>>> - all changes to 'include/uapi/linux/virtio_vsock.h' moved
>>> to dedicated patch, as these changes linked with patch to
>>> spec.
>>>
>>> - patch 'virtio/vsock: SEQPACKET feature bit support' now merged
>>> to 'virtio/vsock: setup SEQPACKET ops for transport'.
>>>
>>> - patch 'vhost/vsock: SEQPACKET feature bit support' now merged
>>> to 'vhost/vsock: setup SEQPACKET ops for transport'.
>>>
>>> Per patch changelog:
>>> see every patch after '---' line.
>>>
>>> v6 -> v7:
>>> General changelog:
>>> - virtio transport callback for message length now removed
>>> from transport. Length of record is returned by dequeue
>>> callback.
>>>
>>> - function which tries to get message length now returns 0
>>> when rx queue is empty. Also length of current message in
>>> progress is set to 0, when message processed or error
>>> happens.
>>>
>>> - patches for virtio feature bit moved after patches with
>>> transport ops.
>>>
>>> Per patch changelog:
>>> see every patch after '---' line.
>>>
>>> v5 -> v6:
>>> General changelog:
>>> - virtio transport specific callbacks which send SEQ_BEGIN or
>>> SEQ_END now hidden inside virtio transport. Only enqueue,
>>> dequeue and record length callbacks are provided by transport.
>>>
>>> - virtio feature bit for SEQPACKET socket support introduced:
>>> VIRTIO_VSOCK_F_SEQPACKET.
>>>
>>> - 'msg_cnt' field in 'struct virtio_vsock_seq_hdr' renamed to
>>> 'msg_id' and used as id.
>>>
>>> Per patch changelog:
>>> - 'af_vsock: separate wait data loop':
>>> 1) Commit message updated.
>>> 2) 'prepare_to_wait()' moved inside while loop(thanks to
>>> Jorgen Hansen).
>>> Marked 'Reviewed-by' with 1), but as 2) I removed R-b.
>>>
>>> - 'af_vsock: separate receive data loop': commit message
>>> updated.
>>> Marked 'Reviewed-by' with that fix.
>>>
>>> - 'af_vsock: implement SEQPACKET receive loop': style fixes.
>>>
>>> - 'af_vsock: rest of SEQPACKET support':
>>> 1) 'module_put()' added when transport callback check failed.
>>> 2) Now only 'seqpacket_allow()' callback called to check
>>> support of SEQPACKET by transport.
>>>
>>> - 'af_vsock: update comments for stream sockets': commit message
>>> updated.
>>> Marked 'Reviewed-by' with that fix.
>>>
>>> - 'virtio/vsock: set packet's type in send':
>>> 1) Commit message updated.
>>> 2) Parameter 'type' from 'virtio_transport_send_credit_update()'
>>> also removed in this patch instead of in next.
>>>
>>> - 'virtio/vsock: dequeue callback for SOCK_SEQPACKET': SEQPACKET
>>> related state wrapped to special struct.
>>>
>>> - 'virtio/vsock: update trace event for SEQPACKET': format strings
>>> now not broken by new lines.
>>>
>>> v4 -> v5:
>>> - patches reorganized:
>>> 1) Setting of packet's type in 'virtio_transport_send_pkt_info()'
>>> is moved to separate patch.
>>> 2) Simplifying of 'virtio_transport_send_credit_update()' is
>>> moved to separate patch and before main virtio/vsock patches.
>>> - style problem fixed
>>> - in 'af_vsock: separate receive data loop' extra 'release_sock()'
>>> removed
>>> - added trace event fields for SEQPACKET
>>> - in 'af_vsock: separate wait data loop':
>>> 1) 'vsock_wait_data()' removed 'goto out;'
>>> 2) Comment for invalid data amount is changed.
>>> - in 'af_vsock: rest of SEQPACKET support', 'new_transport' pointer
>>> check is moved after 'try_module_get()'
>>> - in 'af_vsock: update comments for stream sockets', 'connect-oriented'
>>> replaced with 'connection-oriented'
>>> - in 'loopback/vsock: setup SEQPACKET ops for transport',
>>> 'loopback/vsock' replaced with 'vsock/loopback'
>>>
>>> v3 -> v4:
>>> - SEQPACKET specific metadata moved from packet header to payload
>>> and called 'virtio_vsock_seq_hdr'
>>> - record integrity check:
>>> 1) SEQ_END operation was added, which marks end of record.
>>> 2) Both SEQ_BEGIN and SEQ_END carries counter which is incremented
>>> on every marker send.
>>> - af_vsock.c: socket operations for STREAM and SEQPACKET call same
>>> functions instead of having own "gates" differs only by names:
>>> 'vsock_seqpacket/stream_getsockopt()' now replaced with
>>> 'vsock_connectible_getsockopt()'.
>>> - af_vsock.c: 'seqpacket_dequeue' callback returns error and flag that
>>> record ready. There is no need to return number of copied bytes,
>>> because case when record received successfully is checked at virtio
>>> transport layer, when SEQ_END is processed. Also user doesn't need
>>> number of copied bytes, because 'recv()' from SEQPACKET could return
>>> error, length of users's buffer or length of whole record(both are
>>> known in af_vsock.c).
>>> - af_vsock.c: both wait loops in af_vsock.c(for data and space) moved
>>> to separate functions because now both called from several places.
>>> - af_vsock.c: 'vsock_assign_transport()' checks that 'new_transport'
>>> pointer is not NULL and returns 'ESOCKTNOSUPPORT' instead of 'ENODEV'
>>> if failed to use transport.
>>> - tools/testing/vsock/vsock_test.c: rename tests
>>>
>>> v2 -> v3:
>>> - patches reorganized: split for prepare and implementation patches
>>> - local variables are declared in "Reverse Christmas tree" manner
>>> - virtio_transport_common.c: valid leXX_to_cpu() for vsock header
>>> fields access
>>> - af_vsock.c: 'vsock_connectible_*sockopt()' added as shared code
>>> between stream and seqpacket sockets.
>>> - af_vsock.c: loops in '__vsock_*_recvmsg()' refactored.
>>> - af_vsock.c: 'vsock_wait_data()' refactored.
>>>
>>> v1 -> v2:
>>> - patches reordered: af_vsock.c related changes now before virtio vsock
>>> - patches reorganized: more small patches, where +/- are not mixed
>>> - tests for SOCK_SEQPACKET added
>>> - all commit messages updated
>>> - af_vsock.c: 'vsock_pre_recv_check()' inlined to
>>> 'vsock_connectible_recvmsg()'
>>> - af_vsock.c: 'vsock_assign_transport()' returns ENODEV if transport
>>> was not found
>>> - virtio_transport_common.c: transport callback for seqpacket dequeue
>>> - virtio_transport_common.c: simplified
>>> 'virtio_transport_recv_connected()'
>>> - virtio_transport_common.c: send reset on socket and packet type
>>> mismatch.
>>>
>>> Signed-off-by: Arseny Krasnov <[email protected]>
>>>
>>> --
>>> 2.25.1
>>>
On Tue, May 25, 2021 at 11:22:09AM +0300, Arseny Krasnov wrote:
>
>On 23.05.2021 15:14, Arseny Krasnov wrote:
>> On 21.05.2021 10:55, Stefano Garzarella wrote:
>>> Hi Arseny,
>>>
>>> On Thu, May 20, 2021 at 10:13:53PM +0300, Arseny Krasnov wrote:
>>>> This patchset implements support of SOCK_SEQPACKET for virtio
>>>> transport.
>>> I'll carefully review and test this series next Monday, in the mean time
>>> I think we should have at least an agreement about the changes that
>>> regards virtio-spec before merge this series, to avoid any compatibility
>>> issues.
>>>
>>> Do you plan to send a new version of the specification changes?
>>>
>>> Thanks,
>>> Stefano
>> Hello, sorry for long answer. I'm on vacation now, but i plan to send
>>
>> it in next several days, because with current implementation it is short
>>
>>
>> Thank You
>
>Hello, here is spec patch:
>
>https://lists.oasis-open.org/archives/virtio-comment/202105/msg00017.html
>
>Let's discuss it
Yep, sure.
About this series I think is better to split in two series since it
became very long. Patchwork [1] also complains here [2].
You can send a first series with patches from 1 to 7. These patches are
reviewed by me and can go regardless of the discussion of the VIRTIO
specifications.
Maybe you can also add the patch with the test to this first series.
Please specify in the cover letter that the implementation for virtio
devices is under development and will be sent later.
When it will be merged in the net-next tree, you can post the second
part with the rest of the series that implements SEQPACKET for virtio
devices, possibly after we received an agreement for the specifications.
Please use the "net-next" tag and take a look at
Documentation/networking/netdev-FAQ.rst about netdev development.
Anyway, in the next days (hopefully tomorrow) I'll review the rest of
the series related to virtio devices and spec.
Thanks,
Stefano
[1]
https://patchwork.kernel.org/project/netdevbpf/list/?series=486011&state=*
[2]
https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/
On 25.05.2021 17:52, Stefano Garzarella wrote:
> On Tue, May 25, 2021 at 11:22:09AM +0300, Arseny Krasnov wrote:
>> On 23.05.2021 15:14, Arseny Krasnov wrote:
>>> On 21.05.2021 10:55, Stefano Garzarella wrote:
>>>> Hi Arseny,
>>>>
>>>> On Thu, May 20, 2021 at 10:13:53PM +0300, Arseny Krasnov wrote:
>>>>> This patchset implements support of SOCK_SEQPACKET for virtio
>>>>> transport.
>>>> I'll carefully review and test this series next Monday, in the mean time
>>>> I think we should have at least an agreement about the changes that
>>>> regards virtio-spec before merge this series, to avoid any compatibility
>>>> issues.
>>>>
>>>> Do you plan to send a new version of the specification changes?
>>>>
>>>> Thanks,
>>>> Stefano
>>> Hello, sorry for long answer. I'm on vacation now, but i plan to send
>>>
>>> it in next several days, because with current implementation it is short
>>>
>>>
>>> Thank You
>> Hello, here is spec patch:
>>
>> https://lists.oasis-open.org/archives/virtio-comment/202105/msg00017.html
>>
>> Let's discuss it
> Yep, sure.
>
> About this series I think is better to split in two series since it
> became very long. Patchwork [1] also complains here [2].
>
> You can send a first series with patches from 1 to 7. These patches are
> reviewed by me and can go regardless of the discussion of the VIRTIO
> specifications.
Ok, i'll send it on next week.
> Maybe you can also add the patch with the test to this first series.
>
> Please specify in the cover letter that the implementation for virtio
> devices is under development and will be sent later.
>
>
> When it will be merged in the net-next tree, you can post the second
> part with the rest of the series that implements SEQPACKET for virtio
> devices, possibly after we received an agreement for the specifications.
>
> Please use the "net-next" tag and take a look at
> Documentation/networking/netdev-FAQ.rst about netdev development.
Ok
>
>
> Anyway, in the next days (hopefully tomorrow) I'll review the rest of
> the series related to virtio devices and spec.
>
> Thanks,
> Stefano
>
> [1]
> https://patchwork.kernel.org/project/netdevbpf/list/?series=486011&state=*
>
> [2]
> https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/
>
>
On Thu, May 20, 2021 at 10:17:58PM +0300, Arseny Krasnov wrote:
>Callback fetches RW packets from rx queue of socket until whole record
>is copied(if user's buffer is full, user is not woken up). This is done
>to not stall sender, because if we wake up user and it leaves syscall,
>nobody will send credit update for rest of record, and sender will wait
>for next enter of read syscall at receiver's side. So if user buffer is
>full, we just send credit update and drop data.
>
>Signed-off-by: Arseny Krasnov <[email protected]>
>---
> v9 -> v10:
> 1) Number of dequeued bytes incremented even in case when
> user's buffer is full.
> 2) Use 'msg_data_left()' instead of direct access to 'msg_hdr'.
> 3) Rename variable 'err' to 'dequeued_len', in case of error
> it has negative value.
>
> include/linux/virtio_vsock.h | 5 ++
> net/vmw_vsock/virtio_transport_common.c | 65 +++++++++++++++++++++++++
> 2 files changed, 70 insertions(+)
>
>diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
>index dc636b727179..02acf6e9ae04 100644
>--- a/include/linux/virtio_vsock.h
>+++ b/include/linux/virtio_vsock.h
>@@ -80,6 +80,11 @@ virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
> struct msghdr *msg,
> size_t len, int flags);
>
>+ssize_t
>+virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
>+ struct msghdr *msg,
>+ int flags,
>+ bool *msg_ready);
> s64 virtio_transport_stream_has_data(struct vsock_sock *vsk);
> s64 virtio_transport_stream_has_space(struct vsock_sock *vsk);
>
>diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
>index ad0d34d41444..61349b2ea7fe 100644
>--- a/net/vmw_vsock/virtio_transport_common.c
>+++ b/net/vmw_vsock/virtio_transport_common.c
>@@ -393,6 +393,59 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
> return err;
> }
>
>+static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
>+ struct msghdr *msg,
>+ int flags,
>+ bool *msg_ready)
>+{
>+ struct virtio_vsock_sock *vvs = vsk->trans;
>+ struct virtio_vsock_pkt *pkt;
>+ int dequeued_len = 0;
>+ size_t user_buf_len = msg_data_left(msg);
>+
>+ *msg_ready = false;
>+ spin_lock_bh(&vvs->rx_lock);
>+
>+ while (!*msg_ready && !list_empty(&vvs->rx_queue) && dequeued_len >= 0) {
I'
>+ size_t bytes_to_copy;
>+ size_t pkt_len;
>+
>+ pkt = list_first_entry(&vvs->rx_queue, struct virtio_vsock_pkt, list);
>+ pkt_len = (size_t)le32_to_cpu(pkt->hdr.len);
>+ bytes_to_copy = min(user_buf_len, pkt_len);
>+
>+ if (bytes_to_copy) {
>+ /* sk_lock is held by caller so no one else can dequeue.
>+ * Unlock rx_lock since memcpy_to_msg() may sleep.
>+ */
>+ spin_unlock_bh(&vvs->rx_lock);
>+
>+ if (memcpy_to_msg(msg, pkt->buf, bytes_to_copy))
>+ dequeued_len = -EINVAL;
I think here is better to return the error returned by memcpy_to_msg(),
as we do in the other place where we use memcpy_to_msg().
I mean something like this:
err = memcpy_to_msgmsg, pkt->buf, bytes_to_copy);
if (err)
dequeued_len = err;
>+ else
>+ user_buf_len -= bytes_to_copy;
>+
>+ spin_lock_bh(&vvs->rx_lock);
>+ }
>+
Maybe here we can simply break the cycle if we have an error:
if (dequeued_len < 0)
break;
Or we can refactor a bit, simplifying the while() condition and also the
code in this way (not tested):
while (!*msg_ready && !list_empty(&vvs->rx_queue)) {
...
if (bytes_to_copy) {
int err;
/* ...
*/
spin_unlock_bh(&vvs->rx_lock);
err = memcpy_to_msgmsg, pkt->buf, bytes_to_copy);
if (err) {
dequeued_len = err;
goto out;
}
spin_lock_bh(&vvs->rx_lock);
user_buf_len -= bytes_to_copy;
}
dequeued_len += pkt_len;
if (le32_to_cpu(pkt->hdr.flags) & VIRTIO_VSOCK_SEQ_EOR)
*msg_ready = true;
virtio_transport_dec_rx_pkt(vvs, pkt);
list_del(&pkt->list);
virtio_transport_free_pkt(pkt);
}
out:
spin_unlock_bh(&vvs->rx_lock);
virtio_transport_send_credit_update(vsk);
return dequeued_len;
}
On Thu, May 20, 2021 at 10:18:37PM +0300, Arseny Krasnov wrote:
>Small updates to make SOCK_SEQPACKET work:
>1) Send SHUTDOWN on socket close for SEQPACKET type.
>2) Set SEQPACKET packet type during send.
>3) Set 'VIRTIO_VSOCK_SEQ_EOR' bit in flags for last
> packet of message.
>
>Signed-off-by: Arseny Krasnov <[email protected]>
>---
> v9 -> v10:
> 1) Use 'msg_data_left()' instead of direct access to 'msg_hdr'.
> 2) Commit message updated.
> 3) Add check for socket type when setting SEQ_EOR bit.
>
> include/linux/virtio_vsock.h | 4 ++++
> net/vmw_vsock/virtio_transport_common.c | 18 ++++++++++++++++--
> 2 files changed, 20 insertions(+), 2 deletions(-)
>
>diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
>index 02acf6e9ae04..7360ab7ea0af 100644
>--- a/include/linux/virtio_vsock.h
>+++ b/include/linux/virtio_vsock.h
>@@ -80,6 +80,10 @@ virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
> struct msghdr *msg,
> size_t len, int flags);
>
>+int
>+virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk,
>+ struct msghdr *msg,
>+ size_t len);
> ssize_t
> 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 a6f8b0f39775..f7a3281b3eab 100644
>--- a/net/vmw_vsock/virtio_transport_common.c
>+++ b/net/vmw_vsock/virtio_transport_common.c
>@@ -74,6 +74,11 @@ virtio_transport_alloc_pkt(struct virtio_vsock_pkt_info *info,
> err = memcpy_from_msg(pkt->buf, info->msg, len);
> if (err)
> goto out;
>+
>+ if (msg_data_left(info->msg) == 0 &&
>+ info->type == VIRTIO_VSOCK_TYPE_SEQPACKET)
>+ pkt->hdr.flags = cpu_to_le32(info->flags |
>+ VIRTIO_VSOCK_SEQ_EOR);
`pkt->hdr.flags |= cpu_to_le32(VIRTIO_VSOCK_SEQ_EOR)` should be enough,
no?
Stefano
> }
>
> trace_virtio_transport_alloc_pkt(src_cid, src_port,
>@@ -187,7 +192,7 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
> struct virtio_vsock_pkt *pkt;
> u32 pkt_len = info->pkt_len;
>
>- info->type = VIRTIO_VSOCK_TYPE_STREAM;
>+ info->type = virtio_transport_get_type(sk_vsock(vsk));
>
> t_ops = virtio_transport_get_ops(vsk);
> if (unlikely(!t_ops))
>@@ -478,6 +483,15 @@ virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
> }
> EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_dequeue);
>
>+int
>+virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk,
>+ struct msghdr *msg,
>+ size_t len)
>+{
>+ return virtio_transport_stream_enqueue(vsk, msg, len);
>+}
>+EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_enqueue);
>+
> int
> virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
> struct msghdr *msg,
>@@ -912,7 +926,7 @@ void virtio_transport_release(struct vsock_sock *vsk)
> struct sock *sk = &vsk->sk;
> bool remove_sock = true;
>
>- if (sk->sk_type == SOCK_STREAM)
>+ if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)
> remove_sock = virtio_transport_close(vsk);
>
> if (remove_sock) {
>--
>2.25.1
>
On 03.06.2021 17:45, Stefano Garzarella wrote:
> On Thu, May 20, 2021 at 10:17:58PM +0300, Arseny Krasnov wrote:
>> Callback fetches RW packets from rx queue of socket until whole record
>> is copied(if user's buffer is full, user is not woken up). This is done
>> to not stall sender, because if we wake up user and it leaves syscall,
>> nobody will send credit update for rest of record, and sender will wait
>> for next enter of read syscall at receiver's side. So if user buffer is
>> full, we just send credit update and drop data.
>>
>> Signed-off-by: Arseny Krasnov <[email protected]>
>> ---
>> v9 -> v10:
>> 1) Number of dequeued bytes incremented even in case when
>> user's buffer is full.
>> 2) Use 'msg_data_left()' instead of direct access to 'msg_hdr'.
>> 3) Rename variable 'err' to 'dequeued_len', in case of error
>> it has negative value.
>>
>> include/linux/virtio_vsock.h | 5 ++
>> net/vmw_vsock/virtio_transport_common.c | 65 +++++++++++++++++++++++++
>> 2 files changed, 70 insertions(+)
>>
>> diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
>> index dc636b727179..02acf6e9ae04 100644
>> --- a/include/linux/virtio_vsock.h
>> +++ b/include/linux/virtio_vsock.h
>> @@ -80,6 +80,11 @@ virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
>> struct msghdr *msg,
>> size_t len, int flags);
>>
>> +ssize_t
>> +virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
>> + struct msghdr *msg,
>> + int flags,
>> + bool *msg_ready);
>> s64 virtio_transport_stream_has_data(struct vsock_sock *vsk);
>> s64 virtio_transport_stream_has_space(struct vsock_sock *vsk);
>>
>> diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
>> index ad0d34d41444..61349b2ea7fe 100644
>> --- a/net/vmw_vsock/virtio_transport_common.c
>> +++ b/net/vmw_vsock/virtio_transport_common.c
>> @@ -393,6 +393,59 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
>> return err;
>> }
>>
>> +static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
>> + struct msghdr *msg,
>> + int flags,
>> + bool *msg_ready)
>> +{
>> + struct virtio_vsock_sock *vvs = vsk->trans;
>> + struct virtio_vsock_pkt *pkt;
>> + int dequeued_len = 0;
>> + size_t user_buf_len = msg_data_left(msg);
>> +
>> + *msg_ready = false;
>> + spin_lock_bh(&vvs->rx_lock);
>> +
>> + while (!*msg_ready && !list_empty(&vvs->rx_queue) && dequeued_len >= 0) {
> I'
>
>> + size_t bytes_to_copy;
>> + size_t pkt_len;
>> +
>> + pkt = list_first_entry(&vvs->rx_queue, struct virtio_vsock_pkt, list);
>> + pkt_len = (size_t)le32_to_cpu(pkt->hdr.len);
>> + bytes_to_copy = min(user_buf_len, pkt_len);
>> +
>> + if (bytes_to_copy) {
>> + /* sk_lock is held by caller so no one else can dequeue.
>> + * Unlock rx_lock since memcpy_to_msg() may sleep.
>> + */
>> + spin_unlock_bh(&vvs->rx_lock);
>> +
>> + if (memcpy_to_msg(msg, pkt->buf, bytes_to_copy))
>> + dequeued_len = -EINVAL;
> I think here is better to return the error returned by memcpy_to_msg(),
> as we do in the other place where we use memcpy_to_msg().
>
> I mean something like this:
> err = memcpy_to_msgmsg, pkt->buf, bytes_to_copy);
> if (err)
> dequeued_len = err;
Ack
>> + else
>> + user_buf_len -= bytes_to_copy;
>> +
>> + spin_lock_bh(&vvs->rx_lock);
>> + }
>> +
> Maybe here we can simply break the cycle if we have an error:
> if (dequeued_len < 0)
> break;
>
> Or we can refactor a bit, simplifying the while() condition and also the
> code in this way (not tested):
>
> while (!*msg_ready && !list_empty(&vvs->rx_queue)) {
> ...
>
> if (bytes_to_copy) {
> int err;
>
> /* ...
> */
> spin_unlock_bh(&vvs->rx_lock);
> err = memcpy_to_msgmsg, pkt->buf, bytes_to_copy);
> if (err) {
> dequeued_len = err;
> goto out;
> }
> spin_lock_bh(&vvs->rx_lock);
>
> user_buf_len -= bytes_to_copy;
> }
>
> dequeued_len += pkt_len;
>
> if (le32_to_cpu(pkt->hdr.flags) & VIRTIO_VSOCK_SEQ_EOR)
> *msg_ready = true;
>
> virtio_transport_dec_rx_pkt(vvs, pkt);
> list_del(&pkt->list);
> virtio_transport_free_pkt(pkt);
> }
>
> out:
> spin_unlock_bh(&vvs->rx_lock);
>
> virtio_transport_send_credit_update(vsk);
>
> return dequeued_len;
> }
I think we can't do 'goto out' or break, because in case of error, we still need
to free packet. It is possible to do something like this:
virtio_transport_dec_rx_pkt(vvs, pkt);
list_del(&pkt->list);
virtio_transport_free_pkt(pkt);
if (dequeued_len < 0)
break;
>
>
On Fri, Jun 04, 2021 at 04:12:23PM +0300, Arseny Krasnov wrote:
>
>On 03.06.2021 17:45, Stefano Garzarella wrote:
>> On Thu, May 20, 2021 at 10:17:58PM +0300, Arseny Krasnov wrote:
>>> Callback fetches RW packets from rx queue of socket until whole record
>>> is copied(if user's buffer is full, user is not woken up). This is done
>>> to not stall sender, because if we wake up user and it leaves syscall,
>>> nobody will send credit update for rest of record, and sender will wait
>>> for next enter of read syscall at receiver's side. So if user buffer is
>>> full, we just send credit update and drop data.
>>>
>>> Signed-off-by: Arseny Krasnov <[email protected]>
>>> ---
>>> v9 -> v10:
>>> 1) Number of dequeued bytes incremented even in case when
>>> user's buffer is full.
>>> 2) Use 'msg_data_left()' instead of direct access to 'msg_hdr'.
>>> 3) Rename variable 'err' to 'dequeued_len', in case of error
>>> it has negative value.
>>>
>>> include/linux/virtio_vsock.h | 5 ++
>>> net/vmw_vsock/virtio_transport_common.c | 65 +++++++++++++++++++++++++
>>> 2 files changed, 70 insertions(+)
>>>
>>> diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
>>> index dc636b727179..02acf6e9ae04 100644
>>> --- a/include/linux/virtio_vsock.h
>>> +++ b/include/linux/virtio_vsock.h
>>> @@ -80,6 +80,11 @@ virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
>>> struct msghdr *msg,
>>> size_t len, int flags);
>>>
>>> +ssize_t
>>> +virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
>>> + struct msghdr *msg,
>>> + int flags,
>>> + bool *msg_ready);
>>> s64 virtio_transport_stream_has_data(struct vsock_sock *vsk);
>>> s64 virtio_transport_stream_has_space(struct vsock_sock *vsk);
>>>
>>> diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
>>> index ad0d34d41444..61349b2ea7fe 100644
>>> --- a/net/vmw_vsock/virtio_transport_common.c
>>> +++ b/net/vmw_vsock/virtio_transport_common.c
>>> @@ -393,6 +393,59 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
>>> return err;
>>> }
>>>
>>> +static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
>>> + struct msghdr *msg,
>>> + int flags,
>>> + bool *msg_ready)
>>> +{
>>> + struct virtio_vsock_sock *vvs = vsk->trans;
>>> + struct virtio_vsock_pkt *pkt;
>>> + int dequeued_len = 0;
>>> + size_t user_buf_len = msg_data_left(msg);
>>> +
>>> + *msg_ready = false;
>>> + spin_lock_bh(&vvs->rx_lock);
>>> +
>>> + while (!*msg_ready && !list_empty(&vvs->rx_queue) && dequeued_len >= 0) {
>> I'
>>
>>> + size_t bytes_to_copy;
>>> + size_t pkt_len;
>>> +
>>> + pkt = list_first_entry(&vvs->rx_queue, struct virtio_vsock_pkt, list);
>>> + pkt_len = (size_t)le32_to_cpu(pkt->hdr.len);
>>> + bytes_to_copy = min(user_buf_len, pkt_len);
>>> +
>>> + if (bytes_to_copy) {
>>> + /* sk_lock is held by caller so no one else can dequeue.
>>> + * Unlock rx_lock since memcpy_to_msg() may sleep.
>>> + */
>>> + spin_unlock_bh(&vvs->rx_lock);
>>> +
>>> + if (memcpy_to_msg(msg, pkt->buf, bytes_to_copy))
>>> + dequeued_len = -EINVAL;
>> I think here is better to return the error returned by memcpy_to_msg(),
>> as we do in the other place where we use memcpy_to_msg().
>>
>> I mean something like this:
>> err = memcpy_to_msgmsg, pkt->buf, bytes_to_copy);
>> if (err)
>> dequeued_len = err;
>Ack
>>> + else
>>> + user_buf_len -= bytes_to_copy;
>>> +
>>> + spin_lock_bh(&vvs->rx_lock);
>>> + }
>>> +
>> Maybe here we can simply break the cycle if we have an error:
>> if (dequeued_len < 0)
>> break;
>>
>> Or we can refactor a bit, simplifying the while() condition and also the
>> code in this way (not tested):
>>
>> while (!*msg_ready && !list_empty(&vvs->rx_queue)) {
>> ...
>>
>> if (bytes_to_copy) {
>> int err;
>>
>> /* ...
>> */
>> spin_unlock_bh(&vvs->rx_lock);
>> err = memcpy_to_msgmsg, pkt->buf, bytes_to_copy);
>> if (err) {
>> dequeued_len = err;
>> goto out;
>> }
>> spin_lock_bh(&vvs->rx_lock);
>>
>> user_buf_len -= bytes_to_copy;
>> }
>>
>> dequeued_len += pkt_len;
>>
>> if (le32_to_cpu(pkt->hdr.flags) & VIRTIO_VSOCK_SEQ_EOR)
>> *msg_ready = true;
>>
>> virtio_transport_dec_rx_pkt(vvs, pkt);
>> list_del(&pkt->list);
>> virtio_transport_free_pkt(pkt);
>> }
>>
>> out:
>> spin_unlock_bh(&vvs->rx_lock);
>>
>> virtio_transport_send_credit_update(vsk);
>>
>> return dequeued_len;
>> }
>
>I think we can't do 'goto out' or break, because in case of error, we still need
>to free packet.
Didn't we have code that remove packets from a previous message?
I don't see it anymore.
For example if we have 10 packets queued for a message (the 10th packet
has the EOR flag) and the memcpy_to_msg() fails on the 2nd packet, with
you proposal we are freeing only the first 2 packets, the rest is there
and should be freed when reading the next message, but I don't see that
code.
The same can happen if the recvmsg syscall is interrupted. In that case
we report that nothing was copied, but we freed the first N packets, so
they are lost but the other packets are still in the queue.
Please check also the patch where we implemented
__vsock_seqpacket_recvmsg().
I thinks we should free packets only when we are sure we copied them to
the user space.
> It is possible to do something like this:
>
> virtio_transport_dec_rx_pkt(vvs, pkt);
> list_del(&pkt->list);
> virtio_transport_free_pkt(pkt);
>
> if (dequeued_len < 0)
> break;
>
>>
>>
>
On 04.06.2021 18:03, Stefano Garzarella wrote:
> On Fri, Jun 04, 2021 at 04:12:23PM +0300, Arseny Krasnov wrote:
>> On 03.06.2021 17:45, Stefano Garzarella wrote:
>>> On Thu, May 20, 2021 at 10:17:58PM +0300, Arseny Krasnov wrote:
>>>> Callback fetches RW packets from rx queue of socket until whole record
>>>> is copied(if user's buffer is full, user is not woken up). This is done
>>>> to not stall sender, because if we wake up user and it leaves syscall,
>>>> nobody will send credit update for rest of record, and sender will wait
>>>> for next enter of read syscall at receiver's side. So if user buffer is
>>>> full, we just send credit update and drop data.
>>>>
>>>> Signed-off-by: Arseny Krasnov <[email protected]>
>>>> ---
>>>> v9 -> v10:
>>>> 1) Number of dequeued bytes incremented even in case when
>>>> user's buffer is full.
>>>> 2) Use 'msg_data_left()' instead of direct access to 'msg_hdr'.
>>>> 3) Rename variable 'err' to 'dequeued_len', in case of error
>>>> it has negative value.
>>>>
>>>> include/linux/virtio_vsock.h | 5 ++
>>>> net/vmw_vsock/virtio_transport_common.c | 65 +++++++++++++++++++++++++
>>>> 2 files changed, 70 insertions(+)
>>>>
>>>> diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
>>>> index dc636b727179..02acf6e9ae04 100644
>>>> --- a/include/linux/virtio_vsock.h
>>>> +++ b/include/linux/virtio_vsock.h
>>>> @@ -80,6 +80,11 @@ virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
>>>> struct msghdr *msg,
>>>> size_t len, int flags);
>>>>
>>>> +ssize_t
>>>> +virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
>>>> + struct msghdr *msg,
>>>> + int flags,
>>>> + bool *msg_ready);
>>>> s64 virtio_transport_stream_has_data(struct vsock_sock *vsk);
>>>> s64 virtio_transport_stream_has_space(struct vsock_sock *vsk);
>>>>
>>>> diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
>>>> index ad0d34d41444..61349b2ea7fe 100644
>>>> --- a/net/vmw_vsock/virtio_transport_common.c
>>>> +++ b/net/vmw_vsock/virtio_transport_common.c
>>>> @@ -393,6 +393,59 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
>>>> return err;
>>>> }
>>>>
>>>> +static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
>>>> + struct msghdr *msg,
>>>> + int flags,
>>>> + bool *msg_ready)
>>>> +{
>>>> + struct virtio_vsock_sock *vvs = vsk->trans;
>>>> + struct virtio_vsock_pkt *pkt;
>>>> + int dequeued_len = 0;
>>>> + size_t user_buf_len = msg_data_left(msg);
>>>> +
>>>> + *msg_ready = false;
>>>> + spin_lock_bh(&vvs->rx_lock);
>>>> +
>>>> + while (!*msg_ready && !list_empty(&vvs->rx_queue) && dequeued_len >= 0) {
>>> I'
>>>
>>>> + size_t bytes_to_copy;
>>>> + size_t pkt_len;
>>>> +
>>>> + pkt = list_first_entry(&vvs->rx_queue, struct virtio_vsock_pkt, list);
>>>> + pkt_len = (size_t)le32_to_cpu(pkt->hdr.len);
>>>> + bytes_to_copy = min(user_buf_len, pkt_len);
>>>> +
>>>> + if (bytes_to_copy) {
>>>> + /* sk_lock is held by caller so no one else can dequeue.
>>>> + * Unlock rx_lock since memcpy_to_msg() may sleep.
>>>> + */
>>>> + spin_unlock_bh(&vvs->rx_lock);
>>>> +
>>>> + if (memcpy_to_msg(msg, pkt->buf, bytes_to_copy))
>>>> + dequeued_len = -EINVAL;
>>> I think here is better to return the error returned by memcpy_to_msg(),
>>> as we do in the other place where we use memcpy_to_msg().
>>>
>>> I mean something like this:
>>> err = memcpy_to_msgmsg, pkt->buf, bytes_to_copy);
>>> if (err)
>>> dequeued_len = err;
>> Ack
>>>> + else
>>>> + user_buf_len -= bytes_to_copy;
>>>> +
>>>> + spin_lock_bh(&vvs->rx_lock);
>>>> + }
>>>> +
>>> Maybe here we can simply break the cycle if we have an error:
>>> if (dequeued_len < 0)
>>> break;
>>>
>>> Or we can refactor a bit, simplifying the while() condition and also the
>>> code in this way (not tested):
>>>
>>> while (!*msg_ready && !list_empty(&vvs->rx_queue)) {
>>> ...
>>>
>>> if (bytes_to_copy) {
>>> int err;
>>>
>>> /* ...
>>> */
>>> spin_unlock_bh(&vvs->rx_lock);
>>> err = memcpy_to_msgmsg, pkt->buf, bytes_to_copy);
>>> if (err) {
>>> dequeued_len = err;
>>> goto out;
>>> }
>>> spin_lock_bh(&vvs->rx_lock);
>>>
>>> user_buf_len -= bytes_to_copy;
>>> }
>>>
>>> dequeued_len += pkt_len;
>>>
>>> if (le32_to_cpu(pkt->hdr.flags) & VIRTIO_VSOCK_SEQ_EOR)
>>> *msg_ready = true;
>>>
>>> virtio_transport_dec_rx_pkt(vvs, pkt);
>>> list_del(&pkt->list);
>>> virtio_transport_free_pkt(pkt);
>>> }
>>>
>>> out:
>>> spin_unlock_bh(&vvs->rx_lock);
>>>
>>> virtio_transport_send_credit_update(vsk);
>>>
>>> return dequeued_len;
>>> }
>> I think we can't do 'goto out' or break, because in case of error, we still need
>> to free packet.
> Didn't we have code that remove packets from a previous message?
> I don't see it anymore.
>
> For example if we have 10 packets queued for a message (the 10th packet
> has the EOR flag) and the memcpy_to_msg() fails on the 2nd packet, with
> you proposal we are freeing only the first 2 packets, the rest is there
> and should be freed when reading the next message, but I don't see that
> code.
>
> The same can happen if the recvmsg syscall is interrupted. In that case
> we report that nothing was copied, but we freed the first N packets, so
> they are lost but the other packets are still in the queue.
>
> Please check also the patch where we implemented
> __vsock_seqpacket_recvmsg().
>
> I thinks we should free packets only when we are sure we copied them to
> the user space.
Hm, yes, this is problem. To solve it i can restore previous approach
with seqbegin/seqend. In that case i can detect unfinished record and
drop it's packets. Seems seqbegin will be a bit like VIRTIO_VSOCK_SEQ_EOR in flags
field of header(e.g. VIRTIO_VSOCK_SEQ_BEGIN). Message id and length are unneeded,
as channel considedered lossless. What do You think?
Thank You
>
>> It is possible to do something like this:
>>
>> virtio_transport_dec_rx_pkt(vvs, pkt);
>> list_del(&pkt->list);
>> virtio_transport_free_pkt(pkt);
>>
>> if (dequeued_len < 0)
>> break;
>>
>>>
>
On Fri, Jun 04, 2021 at 09:03:26PM +0300, Arseny Krasnov wrote:
>
>On 04.06.2021 18:03, Stefano Garzarella wrote:
>> On Fri, Jun 04, 2021 at 04:12:23PM +0300, Arseny Krasnov wrote:
>>> On 03.06.2021 17:45, Stefano Garzarella wrote:
>>>> On Thu, May 20, 2021 at 10:17:58PM +0300, Arseny Krasnov wrote:
>>>>> Callback fetches RW packets from rx queue of socket until whole record
>>>>> is copied(if user's buffer is full, user is not woken up). This is done
>>>>> to not stall sender, because if we wake up user and it leaves syscall,
>>>>> nobody will send credit update for rest of record, and sender will wait
>>>>> for next enter of read syscall at receiver's side. So if user buffer is
>>>>> full, we just send credit update and drop data.
>>>>>
>>>>> Signed-off-by: Arseny Krasnov <[email protected]>
>>>>> ---
>>>>> v9 -> v10:
>>>>> 1) Number of dequeued bytes incremented even in case when
>>>>> user's buffer is full.
>>>>> 2) Use 'msg_data_left()' instead of direct access to 'msg_hdr'.
>>>>> 3) Rename variable 'err' to 'dequeued_len', in case of error
>>>>> it has negative value.
>>>>>
>>>>> include/linux/virtio_vsock.h | 5 ++
>>>>> net/vmw_vsock/virtio_transport_common.c | 65 +++++++++++++++++++++++++
>>>>> 2 files changed, 70 insertions(+)
>>>>>
>>>>> diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
>>>>> index dc636b727179..02acf6e9ae04 100644
>>>>> --- a/include/linux/virtio_vsock.h
>>>>> +++ b/include/linux/virtio_vsock.h
>>>>> @@ -80,6 +80,11 @@ virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
>>>>> struct msghdr *msg,
>>>>> size_t len, int flags);
>>>>>
>>>>> +ssize_t
>>>>> +virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
>>>>> + struct msghdr *msg,
>>>>> + int flags,
>>>>> + bool *msg_ready);
>>>>> s64 virtio_transport_stream_has_data(struct vsock_sock *vsk);
>>>>> s64 virtio_transport_stream_has_space(struct vsock_sock *vsk);
>>>>>
>>>>> diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
>>>>> index ad0d34d41444..61349b2ea7fe 100644
>>>>> --- a/net/vmw_vsock/virtio_transport_common.c
>>>>> +++ b/net/vmw_vsock/virtio_transport_common.c
>>>>> @@ -393,6 +393,59 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
>>>>> return err;
>>>>> }
>>>>>
>>>>> +static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
>>>>> + struct msghdr *msg,
>>>>> + int flags,
>>>>> + bool *msg_ready)
>>>>> +{
>>>>> + struct virtio_vsock_sock *vvs = vsk->trans;
>>>>> + struct virtio_vsock_pkt *pkt;
>>>>> + int dequeued_len = 0;
>>>>> + size_t user_buf_len = msg_data_left(msg);
>>>>> +
>>>>> + *msg_ready = false;
>>>>> + spin_lock_bh(&vvs->rx_lock);
>>>>> +
>>>>> + while (!*msg_ready && !list_empty(&vvs->rx_queue) && dequeued_len >= 0) {
>>>> I'
>>>>
>>>>> + size_t bytes_to_copy;
>>>>> + size_t pkt_len;
>>>>> +
>>>>> + pkt = list_first_entry(&vvs->rx_queue, struct virtio_vsock_pkt, list);
>>>>> + pkt_len = (size_t)le32_to_cpu(pkt->hdr.len);
>>>>> + bytes_to_copy = min(user_buf_len, pkt_len);
>>>>> +
>>>>> + if (bytes_to_copy) {
>>>>> + /* sk_lock is held by caller so no one else can dequeue.
>>>>> + * Unlock rx_lock since memcpy_to_msg() may sleep.
>>>>> + */
>>>>> + spin_unlock_bh(&vvs->rx_lock);
>>>>> +
>>>>> + if (memcpy_to_msg(msg, pkt->buf, bytes_to_copy))
>>>>> + dequeued_len = -EINVAL;
>>>> I think here is better to return the error returned by memcpy_to_msg(),
>>>> as we do in the other place where we use memcpy_to_msg().
>>>>
>>>> I mean something like this:
>>>> err = memcpy_to_msgmsg, pkt->buf, bytes_to_copy);
>>>> if (err)
>>>> dequeued_len = err;
>>> Ack
>>>>> + else
>>>>> + user_buf_len -= bytes_to_copy;
>>>>> +
>>>>> + spin_lock_bh(&vvs->rx_lock);
>>>>> + }
>>>>> +
>>>> Maybe here we can simply break the cycle if we have an error:
>>>> if (dequeued_len < 0)
>>>> break;
>>>>
>>>> Or we can refactor a bit, simplifying the while() condition and also the
>>>> code in this way (not tested):
>>>>
>>>> while (!*msg_ready && !list_empty(&vvs->rx_queue)) {
>>>> ...
>>>>
>>>> if (bytes_to_copy) {
>>>> int err;
>>>>
>>>> /* ...
>>>> */
>>>> spin_unlock_bh(&vvs->rx_lock);
>>>> err = memcpy_to_msgmsg, pkt->buf, bytes_to_copy);
>>>> if (err) {
>>>> dequeued_len = err;
>>>> goto out;
>>>> }
>>>> spin_lock_bh(&vvs->rx_lock);
>>>>
>>>> user_buf_len -= bytes_to_copy;
>>>> }
>>>>
>>>> dequeued_len += pkt_len;
>>>>
>>>> if (le32_to_cpu(pkt->hdr.flags) & VIRTIO_VSOCK_SEQ_EOR)
>>>> *msg_ready = true;
>>>>
>>>> virtio_transport_dec_rx_pkt(vvs, pkt);
>>>> list_del(&pkt->list);
>>>> virtio_transport_free_pkt(pkt);
>>>> }
>>>>
>>>> out:
>>>> spin_unlock_bh(&vvs->rx_lock);
>>>>
>>>> virtio_transport_send_credit_update(vsk);
>>>>
>>>> return dequeued_len;
>>>> }
>>> I think we can't do 'goto out' or break, because in case of error,
>>> we still need
>>> to free packet.
>> Didn't we have code that remove packets from a previous message?
>> I don't see it anymore.
>>
>> For example if we have 10 packets queued for a message (the 10th
>> packet
>> has the EOR flag) and the memcpy_to_msg() fails on the 2nd packet, with
>> you proposal we are freeing only the first 2 packets, the rest is there
>> and should be freed when reading the next message, but I don't see that
>> code.
>>
>> The same can happen if the recvmsg syscall is interrupted. In that case
>> we report that nothing was copied, but we freed the first N packets, so
>> they are lost but the other packets are still in the queue.
>>
>> Please check also the patch where we implemented
>> __vsock_seqpacket_recvmsg().
>>
>> I thinks we should free packets only when we are sure we copied them to
>> the user space.
>
>Hm, yes, this is problem. To solve it i can restore previous approach
>with seqbegin/seqend. In that case i can detect unfinished record and
>drop it's packets. Seems seqbegin will be a bit like
>VIRTIO_VSOCK_SEQ_EOR in flags
>field of header(e.g. VIRTIO_VSOCK_SEQ_BEGIN). Message id and length are
>unneeded,
>as channel considedered lossless. What do You think?
>
I think VIRTIO_VSOCK_SEQ_BEGIN is redundant, using only EOR should be
fine.
When we receive EOR we know that this is the last packet on this message
and the next packet will be the first of a new message.
What we should do is check that we have all the fragments of a packet
and return them all together, otherwise we have to say we have nothing.
For example as we process packets from the vitqueue and queue them in
the rx_queue we could use a counter of how many EORs are in the
rx_queue, which we decrease in virtio_transport_seqpacket_do_dequeue()
when we copied all the fragments.
If the counter is 0, we don't remove anything from the queue and
virtio_transport_seqpacket_do_dequeue() returns 0.
So .seqpacket_dequeue should return 0 if there is not at least one
complete message, or return the entire message. A partial message should
never return.
What do you think?
Maybe we should start using skbuffs for seqpackets as well, but that
might take some time, so that might be okay for now.
Thanks,
Stefano
On 07.06.2021 14:04, Stefano Garzarella wrote:
> On Fri, Jun 04, 2021 at 09:03:26PM +0300, Arseny Krasnov wrote:
>> On 04.06.2021 18:03, Stefano Garzarella wrote:
>>> On Fri, Jun 04, 2021 at 04:12:23PM +0300, Arseny Krasnov wrote:
>>>> On 03.06.2021 17:45, Stefano Garzarella wrote:
>>>>> On Thu, May 20, 2021 at 10:17:58PM +0300, Arseny Krasnov wrote:
>>>>>> Callback fetches RW packets from rx queue of socket until whole record
>>>>>> is copied(if user's buffer is full, user is not woken up). This is done
>>>>>> to not stall sender, because if we wake up user and it leaves syscall,
>>>>>> nobody will send credit update for rest of record, and sender will wait
>>>>>> for next enter of read syscall at receiver's side. So if user buffer is
>>>>>> full, we just send credit update and drop data.
>>>>>>
>>>>>> Signed-off-by: Arseny Krasnov <[email protected]>
>>>>>> ---
>>>>>> v9 -> v10:
>>>>>> 1) Number of dequeued bytes incremented even in case when
>>>>>> user's buffer is full.
>>>>>> 2) Use 'msg_data_left()' instead of direct access to 'msg_hdr'.
>>>>>> 3) Rename variable 'err' to 'dequeued_len', in case of error
>>>>>> it has negative value.
>>>>>>
>>>>>> include/linux/virtio_vsock.h | 5 ++
>>>>>> net/vmw_vsock/virtio_transport_common.c | 65 +++++++++++++++++++++++++
>>>>>> 2 files changed, 70 insertions(+)
>>>>>>
>>>>>> diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
>>>>>> index dc636b727179..02acf6e9ae04 100644
>>>>>> --- a/include/linux/virtio_vsock.h
>>>>>> +++ b/include/linux/virtio_vsock.h
>>>>>> @@ -80,6 +80,11 @@ virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
>>>>>> struct msghdr *msg,
>>>>>> size_t len, int flags);
>>>>>>
>>>>>> +ssize_t
>>>>>> +virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
>>>>>> + struct msghdr *msg,
>>>>>> + int flags,
>>>>>> + bool *msg_ready);
>>>>>> s64 virtio_transport_stream_has_data(struct vsock_sock *vsk);
>>>>>> s64 virtio_transport_stream_has_space(struct vsock_sock *vsk);
>>>>>>
>>>>>> diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
>>>>>> index ad0d34d41444..61349b2ea7fe 100644
>>>>>> --- a/net/vmw_vsock/virtio_transport_common.c
>>>>>> +++ b/net/vmw_vsock/virtio_transport_common.c
>>>>>> @@ -393,6 +393,59 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
>>>>>> return err;
>>>>>> }
>>>>>>
>>>>>> +static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
>>>>>> + struct msghdr *msg,
>>>>>> + int flags,
>>>>>> + bool *msg_ready)
>>>>>> +{
>>>>>> + struct virtio_vsock_sock *vvs = vsk->trans;
>>>>>> + struct virtio_vsock_pkt *pkt;
>>>>>> + int dequeued_len = 0;
>>>>>> + size_t user_buf_len = msg_data_left(msg);
>>>>>> +
>>>>>> + *msg_ready = false;
>>>>>> + spin_lock_bh(&vvs->rx_lock);
>>>>>> +
>>>>>> + while (!*msg_ready && !list_empty(&vvs->rx_queue) && dequeued_len >= 0) {
>>>>> I'
>>>>>
>>>>>> + size_t bytes_to_copy;
>>>>>> + size_t pkt_len;
>>>>>> +
>>>>>> + pkt = list_first_entry(&vvs->rx_queue, struct virtio_vsock_pkt, list);
>>>>>> + pkt_len = (size_t)le32_to_cpu(pkt->hdr.len);
>>>>>> + bytes_to_copy = min(user_buf_len, pkt_len);
>>>>>> +
>>>>>> + if (bytes_to_copy) {
>>>>>> + /* sk_lock is held by caller so no one else can dequeue.
>>>>>> + * Unlock rx_lock since memcpy_to_msg() may sleep.
>>>>>> + */
>>>>>> + spin_unlock_bh(&vvs->rx_lock);
>>>>>> +
>>>>>> + if (memcpy_to_msg(msg, pkt->buf, bytes_to_copy))
>>>>>> + dequeued_len = -EINVAL;
>>>>> I think here is better to return the error returned by memcpy_to_msg(),
>>>>> as we do in the other place where we use memcpy_to_msg().
>>>>>
>>>>> I mean something like this:
>>>>> err = memcpy_to_msgmsg, pkt->buf, bytes_to_copy);
>>>>> if (err)
>>>>> dequeued_len = err;
>>>> Ack
>>>>>> + else
>>>>>> + user_buf_len -= bytes_to_copy;
>>>>>> +
>>>>>> + spin_lock_bh(&vvs->rx_lock);
>>>>>> + }
>>>>>> +
>>>>> Maybe here we can simply break the cycle if we have an error:
>>>>> if (dequeued_len < 0)
>>>>> break;
>>>>>
>>>>> Or we can refactor a bit, simplifying the while() condition and also the
>>>>> code in this way (not tested):
>>>>>
>>>>> while (!*msg_ready && !list_empty(&vvs->rx_queue)) {
>>>>> ...
>>>>>
>>>>> if (bytes_to_copy) {
>>>>> int err;
>>>>>
>>>>> /* ...
>>>>> */
>>>>> spin_unlock_bh(&vvs->rx_lock);
>>>>> err = memcpy_to_msgmsg, pkt->buf, bytes_to_copy);
>>>>> if (err) {
>>>>> dequeued_len = err;
>>>>> goto out;
>>>>> }
>>>>> spin_lock_bh(&vvs->rx_lock);
>>>>>
>>>>> user_buf_len -= bytes_to_copy;
>>>>> }
>>>>>
>>>>> dequeued_len += pkt_len;
>>>>>
>>>>> if (le32_to_cpu(pkt->hdr.flags) & VIRTIO_VSOCK_SEQ_EOR)
>>>>> *msg_ready = true;
>>>>>
>>>>> virtio_transport_dec_rx_pkt(vvs, pkt);
>>>>> list_del(&pkt->list);
>>>>> virtio_transport_free_pkt(pkt);
>>>>> }
>>>>>
>>>>> out:
>>>>> spin_unlock_bh(&vvs->rx_lock);
>>>>>
>>>>> virtio_transport_send_credit_update(vsk);
>>>>>
>>>>> return dequeued_len;
>>>>> }
>>>> I think we can't do 'goto out' or break, because in case of error,
>>>> we still need
>>>> to free packet.
>>> Didn't we have code that remove packets from a previous message?
>>> I don't see it anymore.
>>>
>>> For example if we have 10 packets queued for a message (the 10th
>>> packet
>>> has the EOR flag) and the memcpy_to_msg() fails on the 2nd packet, with
>>> you proposal we are freeing only the first 2 packets, the rest is there
>>> and should be freed when reading the next message, but I don't see that
>>> code.
>>>
>>> The same can happen if the recvmsg syscall is interrupted. In that case
>>> we report that nothing was copied, but we freed the first N packets, so
>>> they are lost but the other packets are still in the queue.
>>>
>>> Please check also the patch where we implemented
>>> __vsock_seqpacket_recvmsg().
>>>
>>> I thinks we should free packets only when we are sure we copied them to
>>> the user space.
>> Hm, yes, this is problem. To solve it i can restore previous approach
>> with seqbegin/seqend. In that case i can detect unfinished record and
>> drop it's packets. Seems seqbegin will be a bit like
>> VIRTIO_VSOCK_SEQ_EOR in flags
>> field of header(e.g. VIRTIO_VSOCK_SEQ_BEGIN). Message id and length are
>> unneeded,
>> as channel considedered lossless. What do You think?
>>
> I think VIRTIO_VSOCK_SEQ_BEGIN is redundant, using only EOR should be
> fine.
>
> When we receive EOR we know that this is the last packet on this message
> and the next packet will be the first of a new message.
>
> What we should do is check that we have all the fragments of a packet
> and return them all together, otherwise we have to say we have nothing.
>
> For example as we process packets from the vitqueue and queue them in
> the rx_queue we could use a counter of how many EORs are in the
> rx_queue, which we decrease in virtio_transport_seqpacket_do_dequeue()
> when we copied all the fragments.
>
> If the counter is 0, we don't remove anything from the queue and
> virtio_transport_seqpacket_do_dequeue() returns 0.
>
> So .seqpacket_dequeue should return 0 if there is not at least one
> complete message, or return the entire message. A partial message should
> never return.
>
> What do you think?
I like it, i've implemented this approach in some early pre v1 versions.
But in this case, credit update logic will be changed - in current implementation
(both seqpacket and stream) credit update reply is sent when data is copied
to user's buffer(e.g. we copy data somewhere, free packet and ready to process
new packet). But if we don't touch user's buffer and keeping incoming packet in rx queue
until whole record is ready, when to send credit update?
Thank You
>
>
> Maybe we should start using skbuffs for seqpackets as well, but that
> might take some time, so that might be okay for now.
>
> Thanks,
> Stefano
>
>
On Mon, Jun 07, 2021 at 04:18:38PM +0300, Arseny Krasnov wrote:
>
>On 07.06.2021 14:04, Stefano Garzarella wrote:
>> On Fri, Jun 04, 2021 at 09:03:26PM +0300, Arseny Krasnov wrote:
>>> On 04.06.2021 18:03, Stefano Garzarella wrote:
>>>> On Fri, Jun 04, 2021 at 04:12:23PM +0300, Arseny Krasnov wrote:
>>>>> On 03.06.2021 17:45, Stefano Garzarella wrote:
>>>>>> On Thu, May 20, 2021 at 10:17:58PM +0300, Arseny Krasnov wrote:
>>>>>>> Callback fetches RW packets from rx queue of socket until whole record
>>>>>>> is copied(if user's buffer is full, user is not woken up). This is done
>>>>>>> to not stall sender, because if we wake up user and it leaves syscall,
>>>>>>> nobody will send credit update for rest of record, and sender will wait
>>>>>>> for next enter of read syscall at receiver's side. So if user buffer is
>>>>>>> full, we just send credit update and drop data.
>>>>>>>
>>>>>>> Signed-off-by: Arseny Krasnov <[email protected]>
>>>>>>> ---
>>>>>>> v9 -> v10:
>>>>>>> 1) Number of dequeued bytes incremented even in case when
>>>>>>> user's buffer is full.
>>>>>>> 2) Use 'msg_data_left()' instead of direct access to 'msg_hdr'.
>>>>>>> 3) Rename variable 'err' to 'dequeued_len', in case of error
>>>>>>> it has negative value.
>>>>>>>
>>>>>>> include/linux/virtio_vsock.h | 5 ++
>>>>>>> net/vmw_vsock/virtio_transport_common.c | 65 +++++++++++++++++++++++++
>>>>>>> 2 files changed, 70 insertions(+)
>>>>>>>
>>>>>>> diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
>>>>>>> index dc636b727179..02acf6e9ae04 100644
>>>>>>> --- a/include/linux/virtio_vsock.h
>>>>>>> +++ b/include/linux/virtio_vsock.h
>>>>>>> @@ -80,6 +80,11 @@ virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
>>>>>>> struct msghdr *msg,
>>>>>>> size_t len, int flags);
>>>>>>>
>>>>>>> +ssize_t
>>>>>>> +virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
>>>>>>> + struct msghdr *msg,
>>>>>>> + int flags,
>>>>>>> + bool *msg_ready);
>>>>>>> s64 virtio_transport_stream_has_data(struct vsock_sock *vsk);
>>>>>>> s64 virtio_transport_stream_has_space(struct vsock_sock *vsk);
>>>>>>>
>>>>>>> diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
>>>>>>> index ad0d34d41444..61349b2ea7fe 100644
>>>>>>> --- a/net/vmw_vsock/virtio_transport_common.c
>>>>>>> +++ b/net/vmw_vsock/virtio_transport_common.c
>>>>>>> @@ -393,6 +393,59 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
>>>>>>> return err;
>>>>>>> }
>>>>>>>
>>>>>>> +static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
>>>>>>> + struct msghdr *msg,
>>>>>>> + int flags,
>>>>>>> + bool *msg_ready)
>>>>>>> +{
>>>>>>> + struct virtio_vsock_sock *vvs = vsk->trans;
>>>>>>> + struct virtio_vsock_pkt *pkt;
>>>>>>> + int dequeued_len = 0;
>>>>>>> + size_t user_buf_len = msg_data_left(msg);
>>>>>>> +
>>>>>>> + *msg_ready = false;
>>>>>>> + spin_lock_bh(&vvs->rx_lock);
>>>>>>> +
>>>>>>> + while (!*msg_ready && !list_empty(&vvs->rx_queue) && dequeued_len >= 0) {
>>>>>> I'
>>>>>>
>>>>>>> + size_t bytes_to_copy;
>>>>>>> + size_t pkt_len;
>>>>>>> +
>>>>>>> + pkt = list_first_entry(&vvs->rx_queue, struct virtio_vsock_pkt, list);
>>>>>>> + pkt_len = (size_t)le32_to_cpu(pkt->hdr.len);
>>>>>>> + bytes_to_copy = min(user_buf_len, pkt_len);
>>>>>>> +
>>>>>>> + if (bytes_to_copy) {
>>>>>>> + /* sk_lock is held by caller so no one else can dequeue.
>>>>>>> + * Unlock rx_lock since memcpy_to_msg() may sleep.
>>>>>>> + */
>>>>>>> + spin_unlock_bh(&vvs->rx_lock);
>>>>>>> +
>>>>>>> + if (memcpy_to_msg(msg, pkt->buf, bytes_to_copy))
>>>>>>> + dequeued_len = -EINVAL;
>>>>>> I think here is better to return the error returned by memcpy_to_msg(),
>>>>>> as we do in the other place where we use memcpy_to_msg().
>>>>>>
>>>>>> I mean something like this:
>>>>>> err = memcpy_to_msgmsg, pkt->buf, bytes_to_copy);
>>>>>> if (err)
>>>>>> dequeued_len = err;
>>>>> Ack
>>>>>>> + else
>>>>>>> + user_buf_len -= bytes_to_copy;
>>>>>>> +
>>>>>>> + spin_lock_bh(&vvs->rx_lock);
>>>>>>> + }
>>>>>>> +
>>>>>> Maybe here we can simply break the cycle if we have an error:
>>>>>> if (dequeued_len < 0)
>>>>>> break;
>>>>>>
>>>>>> Or we can refactor a bit, simplifying the while() condition and also the
>>>>>> code in this way (not tested):
>>>>>>
>>>>>> while (!*msg_ready && !list_empty(&vvs->rx_queue)) {
>>>>>> ...
>>>>>>
>>>>>> if (bytes_to_copy) {
>>>>>> int err;
>>>>>>
>>>>>> /* ...
>>>>>> */
>>>>>> spin_unlock_bh(&vvs->rx_lock);
>>>>>> err = memcpy_to_msgmsg, pkt->buf, bytes_to_copy);
>>>>>> if (err) {
>>>>>> dequeued_len = err;
>>>>>> goto out;
>>>>>> }
>>>>>> spin_lock_bh(&vvs->rx_lock);
>>>>>>
>>>>>> user_buf_len -= bytes_to_copy;
>>>>>> }
>>>>>>
>>>>>> dequeued_len += pkt_len;
>>>>>>
>>>>>> if (le32_to_cpu(pkt->hdr.flags) & VIRTIO_VSOCK_SEQ_EOR)
>>>>>> *msg_ready = true;
>>>>>>
>>>>>> virtio_transport_dec_rx_pkt(vvs, pkt);
>>>>>> list_del(&pkt->list);
>>>>>> virtio_transport_free_pkt(pkt);
>>>>>> }
>>>>>>
>>>>>> out:
>>>>>> spin_unlock_bh(&vvs->rx_lock);
>>>>>>
>>>>>> virtio_transport_send_credit_update(vsk);
>>>>>>
>>>>>> return dequeued_len;
>>>>>> }
>>>>> I think we can't do 'goto out' or break, because in case of error,
>>>>> we still need
>>>>> to free packet.
>>>> Didn't we have code that remove packets from a previous message?
>>>> I don't see it anymore.
>>>>
>>>> For example if we have 10 packets queued for a message (the 10th
>>>> packet
>>>> has the EOR flag) and the memcpy_to_msg() fails on the 2nd packet, with
>>>> you proposal we are freeing only the first 2 packets, the rest is there
>>>> and should be freed when reading the next message, but I don't see that
>>>> code.
>>>>
>>>> The same can happen if the recvmsg syscall is interrupted. In that case
>>>> we report that nothing was copied, but we freed the first N packets, so
>>>> they are lost but the other packets are still in the queue.
>>>>
>>>> Please check also the patch where we implemented
>>>> __vsock_seqpacket_recvmsg().
>>>>
>>>> I thinks we should free packets only when we are sure we copied them to
>>>> the user space.
>>> Hm, yes, this is problem. To solve it i can restore previous approach
>>> with seqbegin/seqend. In that case i can detect unfinished record and
>>> drop it's packets. Seems seqbegin will be a bit like
>>> VIRTIO_VSOCK_SEQ_EOR in flags
>>> field of header(e.g. VIRTIO_VSOCK_SEQ_BEGIN). Message id and length are
>>> unneeded,
>>> as channel considedered lossless. What do You think?
>>>
>> I think VIRTIO_VSOCK_SEQ_BEGIN is redundant, using only EOR should be
>> fine.
>>
>> When we receive EOR we know that this is the last packet on this message
>> and the next packet will be the first of a new message.
>>
>> What we should do is check that we have all the fragments of a packet
>> and return them all together, otherwise we have to say we have nothing.
>>
>> For example as we process packets from the vitqueue and queue them in
>> the rx_queue we could use a counter of how many EORs are in the
>> rx_queue, which we decrease in virtio_transport_seqpacket_do_dequeue()
>> when we copied all the fragments.
>>
>> If the counter is 0, we don't remove anything from the queue and
>> virtio_transport_seqpacket_do_dequeue() returns 0.
>>
>> So .seqpacket_dequeue should return 0 if there is not at least one
>> complete message, or return the entire message. A partial message should
>> never return.
>>
>> What do you think?
>
>I like it, i've implemented this approach in some early pre v1 versions.
>
>But in this case, credit update logic will be changed - in current implementation
>
>(both seqpacket and stream) credit update reply is sent when data is copied
>
>to user's buffer(e.g. we copy data somewhere, free packet and ready to process
>
>new packet). But if we don't touch user's buffer and keeping incoming packet in rx queue
>
>until whole record is ready, when to send credit update?
I think the best approach could be to send credit updates when we remove
them from the rx_queue.
Stefano
On 08.06.2021 11:23, Stefano Garzarella wrote:
> On Mon, Jun 07, 2021 at 04:18:38PM +0300, Arseny Krasnov wrote:
>> On 07.06.2021 14:04, Stefano Garzarella wrote:
>>> On Fri, Jun 04, 2021 at 09:03:26PM +0300, Arseny Krasnov wrote:
>>>> On 04.06.2021 18:03, Stefano Garzarella wrote:
>>>>> On Fri, Jun 04, 2021 at 04:12:23PM +0300, Arseny Krasnov wrote:
>>>>>> On 03.06.2021 17:45, Stefano Garzarella wrote:
>>>>>>> On Thu, May 20, 2021 at 10:17:58PM +0300, Arseny Krasnov wrote:
>>>>>>>> Callback fetches RW packets from rx queue of socket until whole record
>>>>>>>> is copied(if user's buffer is full, user is not woken up). This is done
>>>>>>>> to not stall sender, because if we wake up user and it leaves syscall,
>>>>>>>> nobody will send credit update for rest of record, and sender will wait
>>>>>>>> for next enter of read syscall at receiver's side. So if user buffer is
>>>>>>>> full, we just send credit update and drop data.
>>>>>>>>
>>>>>>>> Signed-off-by: Arseny Krasnov <[email protected]>
>>>>>>>> ---
>>>>>>>> v9 -> v10:
>>>>>>>> 1) Number of dequeued bytes incremented even in case when
>>>>>>>> user's buffer is full.
>>>>>>>> 2) Use 'msg_data_left()' instead of direct access to 'msg_hdr'.
>>>>>>>> 3) Rename variable 'err' to 'dequeued_len', in case of error
>>>>>>>> it has negative value.
>>>>>>>>
>>>>>>>> include/linux/virtio_vsock.h | 5 ++
>>>>>>>> net/vmw_vsock/virtio_transport_common.c | 65 +++++++++++++++++++++++++
>>>>>>>> 2 files changed, 70 insertions(+)
>>>>>>>>
>>>>>>>> diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
>>>>>>>> index dc636b727179..02acf6e9ae04 100644
>>>>>>>> --- a/include/linux/virtio_vsock.h
>>>>>>>> +++ b/include/linux/virtio_vsock.h
>>>>>>>> @@ -80,6 +80,11 @@ virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
>>>>>>>> struct msghdr *msg,
>>>>>>>> size_t len, int flags);
>>>>>>>>
>>>>>>>> +ssize_t
>>>>>>>> +virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
>>>>>>>> + struct msghdr *msg,
>>>>>>>> + int flags,
>>>>>>>> + bool *msg_ready);
>>>>>>>> s64 virtio_transport_stream_has_data(struct vsock_sock *vsk);
>>>>>>>> s64 virtio_transport_stream_has_space(struct vsock_sock *vsk);
>>>>>>>>
>>>>>>>> diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
>>>>>>>> index ad0d34d41444..61349b2ea7fe 100644
>>>>>>>> --- a/net/vmw_vsock/virtio_transport_common.c
>>>>>>>> +++ b/net/vmw_vsock/virtio_transport_common.c
>>>>>>>> @@ -393,6 +393,59 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
>>>>>>>> return err;
>>>>>>>> }
>>>>>>>>
>>>>>>>> +static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
>>>>>>>> + struct msghdr *msg,
>>>>>>>> + int flags,
>>>>>>>> + bool *msg_ready)
>>>>>>>> +{
>>>>>>>> + struct virtio_vsock_sock *vvs = vsk->trans;
>>>>>>>> + struct virtio_vsock_pkt *pkt;
>>>>>>>> + int dequeued_len = 0;
>>>>>>>> + size_t user_buf_len = msg_data_left(msg);
>>>>>>>> +
>>>>>>>> + *msg_ready = false;
>>>>>>>> + spin_lock_bh(&vvs->rx_lock);
>>>>>>>> +
>>>>>>>> + while (!*msg_ready && !list_empty(&vvs->rx_queue) && dequeued_len >= 0) {
>>>>>>> I'
>>>>>>>
>>>>>>>> + size_t bytes_to_copy;
>>>>>>>> + size_t pkt_len;
>>>>>>>> +
>>>>>>>> + pkt = list_first_entry(&vvs->rx_queue, struct virtio_vsock_pkt, list);
>>>>>>>> + pkt_len = (size_t)le32_to_cpu(pkt->hdr.len);
>>>>>>>> + bytes_to_copy = min(user_buf_len, pkt_len);
>>>>>>>> +
>>>>>>>> + if (bytes_to_copy) {
>>>>>>>> + /* sk_lock is held by caller so no one else can dequeue.
>>>>>>>> + * Unlock rx_lock since memcpy_to_msg() may sleep.
>>>>>>>> + */
>>>>>>>> + spin_unlock_bh(&vvs->rx_lock);
>>>>>>>> +
>>>>>>>> + if (memcpy_to_msg(msg, pkt->buf, bytes_to_copy))
>>>>>>>> + dequeued_len = -EINVAL;
>>>>>>> I think here is better to return the error returned by memcpy_to_msg(),
>>>>>>> as we do in the other place where we use memcpy_to_msg().
>>>>>>>
>>>>>>> I mean something like this:
>>>>>>> err = memcpy_to_msgmsg, pkt->buf, bytes_to_copy);
>>>>>>> if (err)
>>>>>>> dequeued_len = err;
>>>>>> Ack
>>>>>>>> + else
>>>>>>>> + user_buf_len -= bytes_to_copy;
>>>>>>>> +
>>>>>>>> + spin_lock_bh(&vvs->rx_lock);
>>>>>>>> + }
>>>>>>>> +
>>>>>>> Maybe here we can simply break the cycle if we have an error:
>>>>>>> if (dequeued_len < 0)
>>>>>>> break;
>>>>>>>
>>>>>>> Or we can refactor a bit, simplifying the while() condition and also the
>>>>>>> code in this way (not tested):
>>>>>>>
>>>>>>> while (!*msg_ready && !list_empty(&vvs->rx_queue)) {
>>>>>>> ...
>>>>>>>
>>>>>>> if (bytes_to_copy) {
>>>>>>> int err;
>>>>>>>
>>>>>>> /* ...
>>>>>>> */
>>>>>>> spin_unlock_bh(&vvs->rx_lock);
>>>>>>> err = memcpy_to_msgmsg, pkt->buf, bytes_to_copy);
>>>>>>> if (err) {
>>>>>>> dequeued_len = err;
>>>>>>> goto out;
>>>>>>> }
>>>>>>> spin_lock_bh(&vvs->rx_lock);
>>>>>>>
>>>>>>> user_buf_len -= bytes_to_copy;
>>>>>>> }
>>>>>>>
>>>>>>> dequeued_len += pkt_len;
>>>>>>>
>>>>>>> if (le32_to_cpu(pkt->hdr.flags) & VIRTIO_VSOCK_SEQ_EOR)
>>>>>>> *msg_ready = true;
>>>>>>>
>>>>>>> virtio_transport_dec_rx_pkt(vvs, pkt);
>>>>>>> list_del(&pkt->list);
>>>>>>> virtio_transport_free_pkt(pkt);
>>>>>>> }
>>>>>>>
>>>>>>> out:
>>>>>>> spin_unlock_bh(&vvs->rx_lock);
>>>>>>>
>>>>>>> virtio_transport_send_credit_update(vsk);
>>>>>>>
>>>>>>> return dequeued_len;
>>>>>>> }
>>>>>> I think we can't do 'goto out' or break, because in case of error,
>>>>>> we still need
>>>>>> to free packet.
>>>>> Didn't we have code that remove packets from a previous message?
>>>>> I don't see it anymore.
>>>>>
>>>>> For example if we have 10 packets queued for a message (the 10th
>>>>> packet
>>>>> has the EOR flag) and the memcpy_to_msg() fails on the 2nd packet, with
>>>>> you proposal we are freeing only the first 2 packets, the rest is there
>>>>> and should be freed when reading the next message, but I don't see that
>>>>> code.
>>>>>
>>>>> The same can happen if the recvmsg syscall is interrupted. In that case
>>>>> we report that nothing was copied, but we freed the first N packets, so
>>>>> they are lost but the other packets are still in the queue.
>>>>>
>>>>> Please check also the patch where we implemented
>>>>> __vsock_seqpacket_recvmsg().
>>>>>
>>>>> I thinks we should free packets only when we are sure we copied them to
>>>>> the user space.
>>>> Hm, yes, this is problem. To solve it i can restore previous approach
>>>> with seqbegin/seqend. In that case i can detect unfinished record and
>>>> drop it's packets. Seems seqbegin will be a bit like
>>>> VIRTIO_VSOCK_SEQ_EOR in flags
>>>> field of header(e.g. VIRTIO_VSOCK_SEQ_BEGIN). Message id and length are
>>>> unneeded,
>>>> as channel considedered lossless. What do You think?
>>>>
>>> I think VIRTIO_VSOCK_SEQ_BEGIN is redundant, using only EOR should be
>>> fine.
>>>
>>> When we receive EOR we know that this is the last packet on this message
>>> and the next packet will be the first of a new message.
>>>
>>> What we should do is check that we have all the fragments of a packet
>>> and return them all together, otherwise we have to say we have nothing.
>>>
>>> For example as we process packets from the vitqueue and queue them in
>>> the rx_queue we could use a counter of how many EORs are in the
>>> rx_queue, which we decrease in virtio_transport_seqpacket_do_dequeue()
>>> when we copied all the fragments.
>>>
>>> If the counter is 0, we don't remove anything from the queue and
>>> virtio_transport_seqpacket_do_dequeue() returns 0.
>>>
>>> So .seqpacket_dequeue should return 0 if there is not at least one
>>> complete message, or return the entire message. A partial message should
>>> never return.
>>>
>>> What do you think?
>> I like it, i've implemented this approach in some early pre v1 versions.
>>
>> But in this case, credit update logic will be changed - in current implementation
>>
>> (both seqpacket and stream) credit update reply is sent when data is copied
>>
>> to user's buffer(e.g. we copy data somewhere, free packet and ready to process
>>
>> new packet). But if we don't touch user's buffer and keeping incoming packet in rx queue
>>
>> until whole record is ready, when to send credit update?
> I think the best approach could be to send credit updates when we remove
> them from the rx_queue.
In that case, it will be impossible to send message bigger than size of rx buffer
(e.g. credit allowed size), because packet will be queued without credit update
reply until credit allowed reach 0.
Thank You
>
> Stefano
>
>
On Tue, Jun 08, 2021 at 12:40:39PM +0300, Arseny Krasnov wrote:
>
>On 08.06.2021 11:23, Stefano Garzarella wrote:
>> On Mon, Jun 07, 2021 at 04:18:38PM +0300, Arseny Krasnov wrote:
>>> On 07.06.2021 14:04, Stefano Garzarella wrote:
>>>> On Fri, Jun 04, 2021 at 09:03:26PM +0300, Arseny Krasnov wrote:
>>>>> On 04.06.2021 18:03, Stefano Garzarella wrote:
>>>>>> On Fri, Jun 04, 2021 at 04:12:23PM +0300, Arseny Krasnov wrote:
>>>>>>> On 03.06.2021 17:45, Stefano Garzarella wrote:
>>>>>>>> On Thu, May 20, 2021 at 10:17:58PM +0300, Arseny Krasnov wrote:
>>>>>>>>> Callback fetches RW packets from rx queue of socket until whole record
>>>>>>>>> is copied(if user's buffer is full, user is not woken up). This is done
>>>>>>>>> to not stall sender, because if we wake up user and it leaves syscall,
>>>>>>>>> nobody will send credit update for rest of record, and sender will wait
>>>>>>>>> for next enter of read syscall at receiver's side. So if user buffer is
>>>>>>>>> full, we just send credit update and drop data.
>>>>>>>>>
>>>>>>>>> Signed-off-by: Arseny Krasnov <[email protected]>
>>>>>>>>> ---
>>>>>>>>> v9 -> v10:
>>>>>>>>> 1) Number of dequeued bytes incremented even in case when
>>>>>>>>> user's buffer is full.
>>>>>>>>> 2) Use 'msg_data_left()' instead of direct access to 'msg_hdr'.
>>>>>>>>> 3) Rename variable 'err' to 'dequeued_len', in case of error
>>>>>>>>> it has negative value.
>>>>>>>>>
>>>>>>>>> include/linux/virtio_vsock.h | 5 ++
>>>>>>>>> net/vmw_vsock/virtio_transport_common.c | 65 +++++++++++++++++++++++++
>>>>>>>>> 2 files changed, 70 insertions(+)
>>>>>>>>>
>>>>>>>>> diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
>>>>>>>>> index dc636b727179..02acf6e9ae04 100644
>>>>>>>>> --- a/include/linux/virtio_vsock.h
>>>>>>>>> +++ b/include/linux/virtio_vsock.h
>>>>>>>>> @@ -80,6 +80,11 @@ virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
>>>>>>>>> struct msghdr *msg,
>>>>>>>>> size_t len, int flags);
>>>>>>>>>
>>>>>>>>> +ssize_t
>>>>>>>>> +virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
>>>>>>>>> + struct msghdr *msg,
>>>>>>>>> + int flags,
>>>>>>>>> + bool *msg_ready);
>>>>>>>>> s64 virtio_transport_stream_has_data(struct vsock_sock *vsk);
>>>>>>>>> s64 virtio_transport_stream_has_space(struct vsock_sock *vsk);
>>>>>>>>>
>>>>>>>>> diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
>>>>>>>>> index ad0d34d41444..61349b2ea7fe 100644
>>>>>>>>> --- a/net/vmw_vsock/virtio_transport_common.c
>>>>>>>>> +++ b/net/vmw_vsock/virtio_transport_common.c
>>>>>>>>> @@ -393,6 +393,59 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
>>>>>>>>> return err;
>>>>>>>>> }
>>>>>>>>>
>>>>>>>>> +static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
>>>>>>>>> + struct msghdr *msg,
>>>>>>>>> + int flags,
>>>>>>>>> + bool *msg_ready)
>>>>>>>>> +{
>>>>>>>>> + struct virtio_vsock_sock *vvs = vsk->trans;
>>>>>>>>> + struct virtio_vsock_pkt *pkt;
>>>>>>>>> + int dequeued_len = 0;
>>>>>>>>> + size_t user_buf_len = msg_data_left(msg);
>>>>>>>>> +
>>>>>>>>> + *msg_ready = false;
>>>>>>>>> + spin_lock_bh(&vvs->rx_lock);
>>>>>>>>> +
>>>>>>>>> + while (!*msg_ready && !list_empty(&vvs->rx_queue) && dequeued_len >= 0) {
>>>>>>>> I'
>>>>>>>>
>>>>>>>>> + size_t bytes_to_copy;
>>>>>>>>> + size_t pkt_len;
>>>>>>>>> +
>>>>>>>>> + pkt = list_first_entry(&vvs->rx_queue, struct virtio_vsock_pkt, list);
>>>>>>>>> + pkt_len = (size_t)le32_to_cpu(pkt->hdr.len);
>>>>>>>>> + bytes_to_copy = min(user_buf_len, pkt_len);
>>>>>>>>> +
>>>>>>>>> + if (bytes_to_copy) {
>>>>>>>>> + /* sk_lock is held by caller so no one else can dequeue.
>>>>>>>>> + * Unlock rx_lock since memcpy_to_msg() may sleep.
>>>>>>>>> + */
>>>>>>>>> + spin_unlock_bh(&vvs->rx_lock);
>>>>>>>>> +
>>>>>>>>> + if (memcpy_to_msg(msg, pkt->buf, bytes_to_copy))
>>>>>>>>> + dequeued_len = -EINVAL;
>>>>>>>> I think here is better to return the error returned by memcpy_to_msg(),
>>>>>>>> as we do in the other place where we use memcpy_to_msg().
>>>>>>>>
>>>>>>>> I mean something like this:
>>>>>>>> err = memcpy_to_msgmsg, pkt->buf, bytes_to_copy);
>>>>>>>> if (err)
>>>>>>>> dequeued_len = err;
>>>>>>> Ack
>>>>>>>>> + else
>>>>>>>>> + user_buf_len -= bytes_to_copy;
>>>>>>>>> +
>>>>>>>>> + spin_lock_bh(&vvs->rx_lock);
>>>>>>>>> + }
>>>>>>>>> +
>>>>>>>> Maybe here we can simply break the cycle if we have an error:
>>>>>>>> if (dequeued_len < 0)
>>>>>>>> break;
>>>>>>>>
>>>>>>>> Or we can refactor a bit, simplifying the while() condition and also the
>>>>>>>> code in this way (not tested):
>>>>>>>>
>>>>>>>> while (!*msg_ready && !list_empty(&vvs->rx_queue)) {
>>>>>>>> ...
>>>>>>>>
>>>>>>>> if (bytes_to_copy) {
>>>>>>>> int err;
>>>>>>>>
>>>>>>>> /* ...
>>>>>>>> */
>>>>>>>> spin_unlock_bh(&vvs->rx_lock);
>>>>>>>> err = memcpy_to_msgmsg, pkt->buf, bytes_to_copy);
>>>>>>>> if (err) {
>>>>>>>> dequeued_len = err;
>>>>>>>> goto out;
>>>>>>>> }
>>>>>>>> spin_lock_bh(&vvs->rx_lock);
>>>>>>>>
>>>>>>>> user_buf_len -= bytes_to_copy;
>>>>>>>> }
>>>>>>>>
>>>>>>>> dequeued_len += pkt_len;
>>>>>>>>
>>>>>>>> if (le32_to_cpu(pkt->hdr.flags) & VIRTIO_VSOCK_SEQ_EOR)
>>>>>>>> *msg_ready = true;
>>>>>>>>
>>>>>>>> virtio_transport_dec_rx_pkt(vvs, pkt);
>>>>>>>> list_del(&pkt->list);
>>>>>>>> virtio_transport_free_pkt(pkt);
>>>>>>>> }
>>>>>>>>
>>>>>>>> out:
>>>>>>>> spin_unlock_bh(&vvs->rx_lock);
>>>>>>>>
>>>>>>>> virtio_transport_send_credit_update(vsk);
>>>>>>>>
>>>>>>>> return dequeued_len;
>>>>>>>> }
>>>>>>> I think we can't do 'goto out' or break, because in case of error,
>>>>>>> we still need
>>>>>>> to free packet.
>>>>>> Didn't we have code that remove packets from a previous message?
>>>>>> I don't see it anymore.
>>>>>>
>>>>>> For example if we have 10 packets queued for a message (the 10th
>>>>>> packet
>>>>>> has the EOR flag) and the memcpy_to_msg() fails on the 2nd packet, with
>>>>>> you proposal we are freeing only the first 2 packets, the rest is there
>>>>>> and should be freed when reading the next message, but I don't see that
>>>>>> code.
>>>>>>
>>>>>> The same can happen if the recvmsg syscall is interrupted. In that case
>>>>>> we report that nothing was copied, but we freed the first N packets, so
>>>>>> they are lost but the other packets are still in the queue.
>>>>>>
>>>>>> Please check also the patch where we implemented
>>>>>> __vsock_seqpacket_recvmsg().
>>>>>>
>>>>>> I thinks we should free packets only when we are sure we copied them to
>>>>>> the user space.
>>>>> Hm, yes, this is problem. To solve it i can restore previous approach
>>>>> with seqbegin/seqend. In that case i can detect unfinished record and
>>>>> drop it's packets. Seems seqbegin will be a bit like
>>>>> VIRTIO_VSOCK_SEQ_EOR in flags
>>>>> field of header(e.g. VIRTIO_VSOCK_SEQ_BEGIN). Message id and length are
>>>>> unneeded,
>>>>> as channel considedered lossless. What do You think?
>>>>>
>>>> I think VIRTIO_VSOCK_SEQ_BEGIN is redundant, using only EOR should be
>>>> fine.
>>>>
>>>> When we receive EOR we know that this is the last packet on this message
>>>> and the next packet will be the first of a new message.
>>>>
>>>> What we should do is check that we have all the fragments of a packet
>>>> and return them all together, otherwise we have to say we have nothing.
>>>>
>>>> For example as we process packets from the vitqueue and queue them in
>>>> the rx_queue we could use a counter of how many EORs are in the
>>>> rx_queue, which we decrease in virtio_transport_seqpacket_do_dequeue()
>>>> when we copied all the fragments.
>>>>
>>>> If the counter is 0, we don't remove anything from the queue and
>>>> virtio_transport_seqpacket_do_dequeue() returns 0.
>>>>
>>>> So .seqpacket_dequeue should return 0 if there is not at least one
>>>> complete message, or return the entire message. A partial message should
>>>> never return.
>>>>
>>>> What do you think?
>>> I like it, i've implemented this approach in some early pre v1 versions.
>>>
>>> But in this case, credit update logic will be changed - in current implementation
>>>
>>> (both seqpacket and stream) credit update reply is sent when data is copied
>>>
>>> to user's buffer(e.g. we copy data somewhere, free packet and ready to process
>>>
>>> new packet). But if we don't touch user's buffer and keeping incoming packet in rx queue
>>>
>>> until whole record is ready, when to send credit update?
>> I think the best approach could be to send credit updates when we remove
>> them from the rx_queue.
>
>In that case, it will be impossible to send message bigger than size of rx buffer
>
>(e.g. credit allowed size), because packet will be queued without credit update
>
>reply until credit allowed reach 0.
>
Yep, but I think it is a reasonable limit for a datagram socket.
Maybe we can add a check on the TX side, since we know this value and
return an error to the user.
Thanks,
Stefano
On 08.06.2021 13:19, Stefano Garzarella wrote:
> On Tue, Jun 08, 2021 at 12:40:39PM +0300, Arseny Krasnov wrote:
>> On 08.06.2021 11:23, Stefano Garzarella wrote:
>>> On Mon, Jun 07, 2021 at 04:18:38PM +0300, Arseny Krasnov wrote:
>>>> On 07.06.2021 14:04, Stefano Garzarella wrote:
>>>>> On Fri, Jun 04, 2021 at 09:03:26PM +0300, Arseny Krasnov wrote:
>>>>>> On 04.06.2021 18:03, Stefano Garzarella wrote:
>>>>>>> On Fri, Jun 04, 2021 at 04:12:23PM +0300, Arseny Krasnov wrote:
>>>>>>>> On 03.06.2021 17:45, Stefano Garzarella wrote:
>>>>>>>>> On Thu, May 20, 2021 at 10:17:58PM +0300, Arseny Krasnov wrote:
>>>>>>>>>> Callback fetches RW packets from rx queue of socket until whole record
>>>>>>>>>> is copied(if user's buffer is full, user is not woken up). This is done
>>>>>>>>>> to not stall sender, because if we wake up user and it leaves syscall,
>>>>>>>>>> nobody will send credit update for rest of record, and sender will wait
>>>>>>>>>> for next enter of read syscall at receiver's side. So if user buffer is
>>>>>>>>>> full, we just send credit update and drop data.
>>>>>>>>>>
>>>>>>>>>> Signed-off-by: Arseny Krasnov <[email protected]>
>>>>>>>>>> ---
>>>>>>>>>> v9 -> v10:
>>>>>>>>>> 1) Number of dequeued bytes incremented even in case when
>>>>>>>>>> user's buffer is full.
>>>>>>>>>> 2) Use 'msg_data_left()' instead of direct access to 'msg_hdr'.
>>>>>>>>>> 3) Rename variable 'err' to 'dequeued_len', in case of error
>>>>>>>>>> it has negative value.
>>>>>>>>>>
>>>>>>>>>> include/linux/virtio_vsock.h | 5 ++
>>>>>>>>>> net/vmw_vsock/virtio_transport_common.c | 65 +++++++++++++++++++++++++
>>>>>>>>>> 2 files changed, 70 insertions(+)
>>>>>>>>>>
>>>>>>>>>> diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
>>>>>>>>>> index dc636b727179..02acf6e9ae04 100644
>>>>>>>>>> --- a/include/linux/virtio_vsock.h
>>>>>>>>>> +++ b/include/linux/virtio_vsock.h
>>>>>>>>>> @@ -80,6 +80,11 @@ virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
>>>>>>>>>> struct msghdr *msg,
>>>>>>>>>> size_t len, int flags);
>>>>>>>>>>
>>>>>>>>>> +ssize_t
>>>>>>>>>> +virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
>>>>>>>>>> + struct msghdr *msg,
>>>>>>>>>> + int flags,
>>>>>>>>>> + bool *msg_ready);
>>>>>>>>>> s64 virtio_transport_stream_has_data(struct vsock_sock *vsk);
>>>>>>>>>> s64 virtio_transport_stream_has_space(struct vsock_sock *vsk);
>>>>>>>>>>
>>>>>>>>>> diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
>>>>>>>>>> index ad0d34d41444..61349b2ea7fe 100644
>>>>>>>>>> --- a/net/vmw_vsock/virtio_transport_common.c
>>>>>>>>>> +++ b/net/vmw_vsock/virtio_transport_common.c
>>>>>>>>>> @@ -393,6 +393,59 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
>>>>>>>>>> return err;
>>>>>>>>>> }
>>>>>>>>>>
>>>>>>>>>> +static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
>>>>>>>>>> + struct msghdr *msg,
>>>>>>>>>> + int flags,
>>>>>>>>>> + bool *msg_ready)
>>>>>>>>>> +{
>>>>>>>>>> + struct virtio_vsock_sock *vvs = vsk->trans;
>>>>>>>>>> + struct virtio_vsock_pkt *pkt;
>>>>>>>>>> + int dequeued_len = 0;
>>>>>>>>>> + size_t user_buf_len = msg_data_left(msg);
>>>>>>>>>> +
>>>>>>>>>> + *msg_ready = false;
>>>>>>>>>> + spin_lock_bh(&vvs->rx_lock);
>>>>>>>>>> +
>>>>>>>>>> + while (!*msg_ready && !list_empty(&vvs->rx_queue) && dequeued_len >= 0) {
>>>>>>>>> I'
>>>>>>>>>
>>>>>>>>>> + size_t bytes_to_copy;
>>>>>>>>>> + size_t pkt_len;
>>>>>>>>>> +
>>>>>>>>>> + pkt = list_first_entry(&vvs->rx_queue, struct virtio_vsock_pkt, list);
>>>>>>>>>> + pkt_len = (size_t)le32_to_cpu(pkt->hdr.len);
>>>>>>>>>> + bytes_to_copy = min(user_buf_len, pkt_len);
>>>>>>>>>> +
>>>>>>>>>> + if (bytes_to_copy) {
>>>>>>>>>> + /* sk_lock is held by caller so no one else can dequeue.
>>>>>>>>>> + * Unlock rx_lock since memcpy_to_msg() may sleep.
>>>>>>>>>> + */
>>>>>>>>>> + spin_unlock_bh(&vvs->rx_lock);
>>>>>>>>>> +
>>>>>>>>>> + if (memcpy_to_msg(msg, pkt->buf, bytes_to_copy))
>>>>>>>>>> + dequeued_len = -EINVAL;
>>>>>>>>> I think here is better to return the error returned by memcpy_to_msg(),
>>>>>>>>> as we do in the other place where we use memcpy_to_msg().
>>>>>>>>>
>>>>>>>>> I mean something like this:
>>>>>>>>> err = memcpy_to_msgmsg, pkt->buf, bytes_to_copy);
>>>>>>>>> if (err)
>>>>>>>>> dequeued_len = err;
>>>>>>>> Ack
>>>>>>>>>> + else
>>>>>>>>>> + user_buf_len -= bytes_to_copy;
>>>>>>>>>> +
>>>>>>>>>> + spin_lock_bh(&vvs->rx_lock);
>>>>>>>>>> + }
>>>>>>>>>> +
>>>>>>>>> Maybe here we can simply break the cycle if we have an error:
>>>>>>>>> if (dequeued_len < 0)
>>>>>>>>> break;
>>>>>>>>>
>>>>>>>>> Or we can refactor a bit, simplifying the while() condition and also the
>>>>>>>>> code in this way (not tested):
>>>>>>>>>
>>>>>>>>> while (!*msg_ready && !list_empty(&vvs->rx_queue)) {
>>>>>>>>> ...
>>>>>>>>>
>>>>>>>>> if (bytes_to_copy) {
>>>>>>>>> int err;
>>>>>>>>>
>>>>>>>>> /* ...
>>>>>>>>> */
>>>>>>>>> spin_unlock_bh(&vvs->rx_lock);
>>>>>>>>> err = memcpy_to_msgmsg, pkt->buf, bytes_to_copy);
>>>>>>>>> if (err) {
>>>>>>>>> dequeued_len = err;
>>>>>>>>> goto out;
>>>>>>>>> }
>>>>>>>>> spin_lock_bh(&vvs->rx_lock);
>>>>>>>>>
>>>>>>>>> user_buf_len -= bytes_to_copy;
>>>>>>>>> }
>>>>>>>>>
>>>>>>>>> dequeued_len += pkt_len;
>>>>>>>>>
>>>>>>>>> if (le32_to_cpu(pkt->hdr.flags) & VIRTIO_VSOCK_SEQ_EOR)
>>>>>>>>> *msg_ready = true;
>>>>>>>>>
>>>>>>>>> virtio_transport_dec_rx_pkt(vvs, pkt);
>>>>>>>>> list_del(&pkt->list);
>>>>>>>>> virtio_transport_free_pkt(pkt);
>>>>>>>>> }
>>>>>>>>>
>>>>>>>>> out:
>>>>>>>>> spin_unlock_bh(&vvs->rx_lock);
>>>>>>>>>
>>>>>>>>> virtio_transport_send_credit_update(vsk);
>>>>>>>>>
>>>>>>>>> return dequeued_len;
>>>>>>>>> }
>>>>>>>> I think we can't do 'goto out' or break, because in case of error,
>>>>>>>> we still need
>>>>>>>> to free packet.
>>>>>>> Didn't we have code that remove packets from a previous message?
>>>>>>> I don't see it anymore.
>>>>>>>
>>>>>>> For example if we have 10 packets queued for a message (the 10th
>>>>>>> packet
>>>>>>> has the EOR flag) and the memcpy_to_msg() fails on the 2nd packet, with
>>>>>>> you proposal we are freeing only the first 2 packets, the rest is there
>>>>>>> and should be freed when reading the next message, but I don't see that
>>>>>>> code.
>>>>>>>
>>>>>>> The same can happen if the recvmsg syscall is interrupted. In that case
>>>>>>> we report that nothing was copied, but we freed the first N packets, so
>>>>>>> they are lost but the other packets are still in the queue.
>>>>>>>
>>>>>>> Please check also the patch where we implemented
>>>>>>> __vsock_seqpacket_recvmsg().
>>>>>>>
>>>>>>> I thinks we should free packets only when we are sure we copied them to
>>>>>>> the user space.
>>>>>> Hm, yes, this is problem. To solve it i can restore previous approach
>>>>>> with seqbegin/seqend. In that case i can detect unfinished record and
>>>>>> drop it's packets. Seems seqbegin will be a bit like
>>>>>> VIRTIO_VSOCK_SEQ_EOR in flags
>>>>>> field of header(e.g. VIRTIO_VSOCK_SEQ_BEGIN). Message id and length are
>>>>>> unneeded,
>>>>>> as channel considedered lossless. What do You think?
>>>>>>
>>>>> I think VIRTIO_VSOCK_SEQ_BEGIN is redundant, using only EOR should be
>>>>> fine.
>>>>>
>>>>> When we receive EOR we know that this is the last packet on this message
>>>>> and the next packet will be the first of a new message.
>>>>>
>>>>> What we should do is check that we have all the fragments of a packet
>>>>> and return them all together, otherwise we have to say we have nothing.
>>>>>
>>>>> For example as we process packets from the vitqueue and queue them in
>>>>> the rx_queue we could use a counter of how many EORs are in the
>>>>> rx_queue, which we decrease in virtio_transport_seqpacket_do_dequeue()
>>>>> when we copied all the fragments.
>>>>>
>>>>> If the counter is 0, we don't remove anything from the queue and
>>>>> virtio_transport_seqpacket_do_dequeue() returns 0.
>>>>>
>>>>> So .seqpacket_dequeue should return 0 if there is not at least one
>>>>> complete message, or return the entire message. A partial message should
>>>>> never return.
>>>>>
>>>>> What do you think?
>>>> I like it, i've implemented this approach in some early pre v1 versions.
>>>>
>>>> But in this case, credit update logic will be changed - in current implementation
>>>>
>>>> (both seqpacket and stream) credit update reply is sent when data is copied
>>>>
>>>> to user's buffer(e.g. we copy data somewhere, free packet and ready to process
>>>>
>>>> new packet). But if we don't touch user's buffer and keeping incoming packet in rx queue
>>>>
>>>> until whole record is ready, when to send credit update?
>>> I think the best approach could be to send credit updates when we remove
>>> them from the rx_queue.
>> In that case, it will be impossible to send message bigger than size of rx buffer
>>
>> (e.g. credit allowed size), because packet will be queued without credit update
>>
>> reply until credit allowed reach 0.
>>
> Yep, but I think it is a reasonable limit for a datagram socket.
>
> Maybe we can add a check on the TX side, since we know this value and
> return an error to the user.
E.g., to before sending message? using SEQPACKET socket,
i need to call setsockopt with SO_VM_SOCKETS_BUFFER_MAX_SIZE/
SO_VM_SOCKETS_BUFFER_SIZE params to setup maximum message size,
if user tries to send message bigger than it, return -EMSGSIZE ?
Thank You
>
> Thanks,
> Stefano
>
>
On Tue, Jun 08, 2021 at 01:24:58PM +0300, Arseny Krasnov wrote:
>
>On 08.06.2021 13:19, Stefano Garzarella wrote:
>> On Tue, Jun 08, 2021 at 12:40:39PM +0300, Arseny Krasnov wrote:
>>> On 08.06.2021 11:23, Stefano Garzarella wrote:
>>>> On Mon, Jun 07, 2021 at 04:18:38PM +0300, Arseny Krasnov wrote:
>>>>> On 07.06.2021 14:04, Stefano Garzarella wrote:
>>>>>> On Fri, Jun 04, 2021 at 09:03:26PM +0300, Arseny Krasnov wrote:
>>>>>>> On 04.06.2021 18:03, Stefano Garzarella wrote:
>>>>>>>> On Fri, Jun 04, 2021 at 04:12:23PM +0300, Arseny Krasnov wrote:
>>>>>>>>> On 03.06.2021 17:45, Stefano Garzarella wrote:
>>>>>>>>>> On Thu, May 20, 2021 at 10:17:58PM +0300, Arseny Krasnov wrote:
>>>>>>>>>>> Callback fetches RW packets from rx queue of socket until whole record
>>>>>>>>>>> is copied(if user's buffer is full, user is not woken up). This is done
>>>>>>>>>>> to not stall sender, because if we wake up user and it leaves syscall,
>>>>>>>>>>> nobody will send credit update for rest of record, and sender will wait
>>>>>>>>>>> for next enter of read syscall at receiver's side. So if user buffer is
>>>>>>>>>>> full, we just send credit update and drop data.
>>>>>>>>>>>
>>>>>>>>>>> Signed-off-by: Arseny Krasnov <[email protected]>
>>>>>>>>>>> ---
>>>>>>>>>>> v9 -> v10:
>>>>>>>>>>> 1) Number of dequeued bytes incremented even in case when
>>>>>>>>>>> user's buffer is full.
>>>>>>>>>>> 2) Use 'msg_data_left()' instead of direct access to 'msg_hdr'.
>>>>>>>>>>> 3) Rename variable 'err' to 'dequeued_len', in case of error
>>>>>>>>>>> it has negative value.
>>>>>>>>>>>
>>>>>>>>>>> include/linux/virtio_vsock.h | 5 ++
>>>>>>>>>>> net/vmw_vsock/virtio_transport_common.c | 65 +++++++++++++++++++++++++
>>>>>>>>>>> 2 files changed, 70 insertions(+)
>>>>>>>>>>>
>>>>>>>>>>> diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
>>>>>>>>>>> index dc636b727179..02acf6e9ae04 100644
>>>>>>>>>>> --- a/include/linux/virtio_vsock.h
>>>>>>>>>>> +++ b/include/linux/virtio_vsock.h
>>>>>>>>>>> @@ -80,6 +80,11 @@ virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
>>>>>>>>>>> struct msghdr *msg,
>>>>>>>>>>> size_t len, int flags);
>>>>>>>>>>>
>>>>>>>>>>> +ssize_t
>>>>>>>>>>> +virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
>>>>>>>>>>> + struct msghdr *msg,
>>>>>>>>>>> + int flags,
>>>>>>>>>>> + bool *msg_ready);
>>>>>>>>>>> s64 virtio_transport_stream_has_data(struct vsock_sock *vsk);
>>>>>>>>>>> s64 virtio_transport_stream_has_space(struct vsock_sock *vsk);
>>>>>>>>>>>
>>>>>>>>>>> diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
>>>>>>>>>>> index ad0d34d41444..61349b2ea7fe 100644
>>>>>>>>>>> --- a/net/vmw_vsock/virtio_transport_common.c
>>>>>>>>>>> +++ b/net/vmw_vsock/virtio_transport_common.c
>>>>>>>>>>> @@ -393,6 +393,59 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
>>>>>>>>>>> return err;
>>>>>>>>>>> }
>>>>>>>>>>>
>>>>>>>>>>> +static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
>>>>>>>>>>> + struct msghdr *msg,
>>>>>>>>>>> + int flags,
>>>>>>>>>>> + bool *msg_ready)
>>>>>>>>>>> +{
>>>>>>>>>>> + struct virtio_vsock_sock *vvs = vsk->trans;
>>>>>>>>>>> + struct virtio_vsock_pkt *pkt;
>>>>>>>>>>> + int dequeued_len = 0;
>>>>>>>>>>> + size_t user_buf_len = msg_data_left(msg);
>>>>>>>>>>> +
>>>>>>>>>>> + *msg_ready = false;
>>>>>>>>>>> + spin_lock_bh(&vvs->rx_lock);
>>>>>>>>>>> +
>>>>>>>>>>> + while (!*msg_ready && !list_empty(&vvs->rx_queue) && dequeued_len >= 0) {
>>>>>>>>>> I'
>>>>>>>>>>
>>>>>>>>>>> + size_t bytes_to_copy;
>>>>>>>>>>> + size_t pkt_len;
>>>>>>>>>>> +
>>>>>>>>>>> + pkt = list_first_entry(&vvs->rx_queue, struct virtio_vsock_pkt, list);
>>>>>>>>>>> + pkt_len = (size_t)le32_to_cpu(pkt->hdr.len);
>>>>>>>>>>> + bytes_to_copy = min(user_buf_len, pkt_len);
>>>>>>>>>>> +
>>>>>>>>>>> + if (bytes_to_copy) {
>>>>>>>>>>> + /* sk_lock is held by caller so no one else can dequeue.
>>>>>>>>>>> + * Unlock rx_lock since memcpy_to_msg() may sleep.
>>>>>>>>>>> + */
>>>>>>>>>>> + spin_unlock_bh(&vvs->rx_lock);
>>>>>>>>>>> +
>>>>>>>>>>> + if (memcpy_to_msg(msg, pkt->buf, bytes_to_copy))
>>>>>>>>>>> + dequeued_len = -EINVAL;
>>>>>>>>>> I think here is better to return the error returned by memcpy_to_msg(),
>>>>>>>>>> as we do in the other place where we use memcpy_to_msg().
>>>>>>>>>>
>>>>>>>>>> I mean something like this:
>>>>>>>>>> err = memcpy_to_msgmsg, pkt->buf, bytes_to_copy);
>>>>>>>>>> if (err)
>>>>>>>>>> dequeued_len = err;
>>>>>>>>> Ack
>>>>>>>>>>> + else
>>>>>>>>>>> + user_buf_len -= bytes_to_copy;
>>>>>>>>>>> +
>>>>>>>>>>> + spin_lock_bh(&vvs->rx_lock);
>>>>>>>>>>> + }
>>>>>>>>>>> +
>>>>>>>>>> Maybe here we can simply break the cycle if we have an error:
>>>>>>>>>> if (dequeued_len < 0)
>>>>>>>>>> break;
>>>>>>>>>>
>>>>>>>>>> Or we can refactor a bit, simplifying the while() condition and also the
>>>>>>>>>> code in this way (not tested):
>>>>>>>>>>
>>>>>>>>>> while (!*msg_ready && !list_empty(&vvs->rx_queue)) {
>>>>>>>>>> ...
>>>>>>>>>>
>>>>>>>>>> if (bytes_to_copy) {
>>>>>>>>>> int err;
>>>>>>>>>>
>>>>>>>>>> /* ...
>>>>>>>>>> */
>>>>>>>>>> spin_unlock_bh(&vvs->rx_lock);
>>>>>>>>>> err = memcpy_to_msgmsg, pkt->buf, bytes_to_copy);
>>>>>>>>>> if (err) {
>>>>>>>>>> dequeued_len = err;
>>>>>>>>>> goto out;
>>>>>>>>>> }
>>>>>>>>>> spin_lock_bh(&vvs->rx_lock);
>>>>>>>>>>
>>>>>>>>>> user_buf_len -= bytes_to_copy;
>>>>>>>>>> }
>>>>>>>>>>
>>>>>>>>>> dequeued_len += pkt_len;
>>>>>>>>>>
>>>>>>>>>> if (le32_to_cpu(pkt->hdr.flags) & VIRTIO_VSOCK_SEQ_EOR)
>>>>>>>>>> *msg_ready = true;
>>>>>>>>>>
>>>>>>>>>> virtio_transport_dec_rx_pkt(vvs, pkt);
>>>>>>>>>> list_del(&pkt->list);
>>>>>>>>>> virtio_transport_free_pkt(pkt);
>>>>>>>>>> }
>>>>>>>>>>
>>>>>>>>>> out:
>>>>>>>>>> spin_unlock_bh(&vvs->rx_lock);
>>>>>>>>>>
>>>>>>>>>> virtio_transport_send_credit_update(vsk);
>>>>>>>>>>
>>>>>>>>>> return dequeued_len;
>>>>>>>>>> }
>>>>>>>>> I think we can't do 'goto out' or break, because in case of error,
>>>>>>>>> we still need
>>>>>>>>> to free packet.
>>>>>>>> Didn't we have code that remove packets from a previous message?
>>>>>>>> I don't see it anymore.
>>>>>>>>
>>>>>>>> For example if we have 10 packets queued for a message (the 10th
>>>>>>>> packet
>>>>>>>> has the EOR flag) and the memcpy_to_msg() fails on the 2nd packet, with
>>>>>>>> you proposal we are freeing only the first 2 packets, the rest is there
>>>>>>>> and should be freed when reading the next message, but I don't see that
>>>>>>>> code.
>>>>>>>>
>>>>>>>> The same can happen if the recvmsg syscall is interrupted. In that case
>>>>>>>> we report that nothing was copied, but we freed the first N packets, so
>>>>>>>> they are lost but the other packets are still in the queue.
>>>>>>>>
>>>>>>>> Please check also the patch where we implemented
>>>>>>>> __vsock_seqpacket_recvmsg().
>>>>>>>>
>>>>>>>> I thinks we should free packets only when we are sure we copied them to
>>>>>>>> the user space.
>>>>>>> Hm, yes, this is problem. To solve it i can restore previous approach
>>>>>>> with seqbegin/seqend. In that case i can detect unfinished record and
>>>>>>> drop it's packets. Seems seqbegin will be a bit like
>>>>>>> VIRTIO_VSOCK_SEQ_EOR in flags
>>>>>>> field of header(e.g. VIRTIO_VSOCK_SEQ_BEGIN). Message id and length are
>>>>>>> unneeded,
>>>>>>> as channel considedered lossless. What do You think?
>>>>>>>
>>>>>> I think VIRTIO_VSOCK_SEQ_BEGIN is redundant, using only EOR should be
>>>>>> fine.
>>>>>>
>>>>>> When we receive EOR we know that this is the last packet on this message
>>>>>> and the next packet will be the first of a new message.
>>>>>>
>>>>>> What we should do is check that we have all the fragments of a packet
>>>>>> and return them all together, otherwise we have to say we have nothing.
>>>>>>
>>>>>> For example as we process packets from the vitqueue and queue them in
>>>>>> the rx_queue we could use a counter of how many EORs are in the
>>>>>> rx_queue, which we decrease in virtio_transport_seqpacket_do_dequeue()
>>>>>> when we copied all the fragments.
>>>>>>
>>>>>> If the counter is 0, we don't remove anything from the queue and
>>>>>> virtio_transport_seqpacket_do_dequeue() returns 0.
>>>>>>
>>>>>> So .seqpacket_dequeue should return 0 if there is not at least one
>>>>>> complete message, or return the entire message. A partial message should
>>>>>> never return.
>>>>>>
>>>>>> What do you think?
>>>>> I like it, i've implemented this approach in some early pre v1 versions.
>>>>>
>>>>> But in this case, credit update logic will be changed - in current implementation
>>>>>
>>>>> (both seqpacket and stream) credit update reply is sent when data is copied
>>>>>
>>>>> to user's buffer(e.g. we copy data somewhere, free packet and ready to process
>>>>>
>>>>> new packet). But if we don't touch user's buffer and keeping incoming packet in rx queue
>>>>>
>>>>> until whole record is ready, when to send credit update?
>>>> I think the best approach could be to send credit updates when we remove
>>>> them from the rx_queue.
>>> In that case, it will be impossible to send message bigger than size of rx buffer
>>>
>>> (e.g. credit allowed size), because packet will be queued without credit update
>>>
>>> reply until credit allowed reach 0.
>>>
>> Yep, but I think it is a reasonable limit for a datagram socket.
>>
>> Maybe we can add a check on the TX side, since we know this value and
>> return an error to the user.
>
>E.g., to before sending message? using SEQPACKET socket,
>
>i need to call setsockopt with SO_VM_SOCKETS_BUFFER_MAX_SIZE/
>
>SO_VM_SOCKETS_BUFFER_SIZE params to setup maximum message size,
>
>if user tries to send message bigger than it, return -EMSGSIZE ?
>
Yep, I mean the receiver side must set it (IIRC default is 256K).
In the transmitter side we can check it using `vvs->peer_buf_alloc` and
return the error.
Stefano