2021-03-23 13:13:43

by Arseny Krasnov

[permalink] [raw]
Subject: [RFC PATCH v7 00/22] virtio/vsock: introduce SOCK_SEQPACKET support

This patchset implements support of SOCK_SEQPACKET for virtio
transport.
As SOCK_SEQPACKET guarantees to save record boundaries, so to
do it, two new packet operations were added: first for start of record
and second to mark end of record(SEQ_BEGIN and SEQ_END later). Also,
both operations carries metadata - to maintain boundaries and payload
integrity. Metadata is introduced by adding special header with two
fields - message id and message length:

struct virtio_vsock_seq_hdr {
__le32 msg_id;
__le32 msg_len;
} __attribute__((packed));

This header is transmitted as payload of SEQ_BEGIN and SEQ_END
packets(buffer of second virtio descriptor in chain) in the same way as
data transmitted in RW packets. Payload was chosen as buffer for this
header to avoid touching first virtio buffer which carries header of
packet, because someone could check that size of this buffer is equal
to size of packet header. To send record, packet with start marker is
sent first(it's header carries length of record and id),then all data
is sent as usual 'RW' packets and finally SEQ_END is sent(it carries
id of message, which is equal to id of SEQ_BEGIN), also after sending
SEQ_END id is incremented. On receiver's side,size of record is known
from packet with start record marker. To check that no packets were
dropped by transport, 'msg_id's of two sequential SEQ_BEGIN and SEQ_END
are checked to be equal and length of data between two markers is
compared to then length in SEQ_BEGIN header.
Now as packets of one socket are not reordered neither on
vsock nor on vhost transport layers, such markers allows to restore
original record on receiver's side. If user's buffer is smaller that
record 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_EOR' and 'MSG_TRUNC' flags.
Tests also implemented.

Thanks to [email protected] for encouragements and initial design
recommendations.

Arseny Krasnov (22):
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: separate wait space 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: dequeue callback for SOCK_SEQPACKET
virtio/vsock: fetch length for SEQPACKET record
virtio/vsock: add SEQPACKET receive logic
virtio/vsock: rest of SOCK_SEQPACKET support
virtio/vsock: SEQPACKET support feature bit
virtio/vsock: setup SEQPACKET ops for transport
vhost/vsock: setup SEQPACKET ops for transport
vsock/loopback: setup SEQPACKET ops for transport
vhost/vsock: SEQPACKET feature bit support
virtio/vsock: SEQPACKET feature bit support
vsock_test: add SOCK_SEQPACKET tests
virtio/vsock: update trace event for SEQPACKET

drivers/vhost/vsock.c | 21 +-
include/linux/virtio_vsock.h | 21 +
include/net/af_vsock.h | 9 +
.../events/vsock_virtio_transport_common.h | 48 +-
include/uapi/linux/virtio_vsock.h | 19 +
net/vmw_vsock/af_vsock.c | 581 +++++++++++------
net/vmw_vsock/virtio_transport.c | 17 +
net/vmw_vsock/virtio_transport_common.c | 379 +++++++++--
net/vmw_vsock/vsock_loopback.c | 12 +
tools/testing/vsock/util.c | 32 +-
tools/testing/vsock/util.h | 3 +
tools/testing/vsock/vsock_test.c | 126 ++++
12 files changed, 1015 insertions(+), 253 deletions(-)

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


2021-03-23 13:13:44

by Arseny Krasnov

[permalink] [raw]
Subject: [RFC PATCH v7 06/22] af_vsock: implement send logic for SEQPACKET

This adds some logic to current stream enqueue function for SEQPACKET
support:
1) Use 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]>
---
v6 -> v7:
'seqpacket_enqueue' callback interface changed, 'flags' argument was
removed, because it was 'msg_flags' field of 'msg' argument which is
already exists.

include/net/af_vsock.h | 2 ++
net/vmw_vsock/af_vsock.c | 21 +++++++++++++++------
2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h
index 7232f6c42a36..b7063820d0bc 100644
--- a/include/net/af_vsock.h
+++ b/include/net/af_vsock.h
@@ -138,6 +138,8 @@ struct vsock_transport {
/* SEQ_PACKET. */
int (*seqpacket_dequeue)(struct vsock_sock *vsk, struct msghdr *msg,
int flags, bool *msg_ready, size_t *record_len);
+ 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 617ffe42693d..a98e5daa06b7 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -1830,9 +1830,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;
@@ -1844,12 +1848,17 @@ static int vsock_connectible_sendmsg(struct socket *sock, struct msghdr *msg,
vsk, written, &send_data);
if (err < 0)
goto out_err;
-
}

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

2021-03-23 13:13:49

by Arseny Krasnov

[permalink] [raw]
Subject: [RFC PATCH v7 07/22] af_vsock: rest of SEQPACKET support

This does rest of SOCK_SEQPACKET support:
1) Adds socket ops for SEQPACKET type.
2) Allows to create socket with SEQPACKET type.

Signed-off-by: Arseny Krasnov <[email protected]>
Reviewed-by: Stefano Garzarella <[email protected]>
---
include/net/af_vsock.h | 1 +
net/vmw_vsock/af_vsock.c | 36 +++++++++++++++++++++++++++++++++++-
2 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h
index b7063820d0bc..6c88ba0df588 100644
--- a/include/net/af_vsock.h
+++ b/include/net/af_vsock.h
@@ -140,6 +140,7 @@ struct vsock_transport {
int flags, bool *msg_ready, size_t *record_len);
int (*seqpacket_enqueue)(struct vsock_sock *vsk, struct msghdr *msg,
size_t len);
+ bool (*seqpacket_allow)(void);

/* 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 a98e5daa06b7..802afe253781 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -452,6 +452,7 @@ int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk)
new_transport = transport_dgram;
break;
case SOCK_STREAM:
+ case SOCK_SEQPACKET:
if (vsock_use_local_transport(remote_cid))
new_transport = transport_local;
else if (remote_cid <= VMADDR_CID_HOST || !transport_h2g ||
@@ -484,6 +485,14 @@ int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk)
if (!new_transport || !try_module_get(new_transport->module))
return -ENODEV;

+ if (sk->sk_type == SOCK_SEQPACKET) {
+ if (!new_transport->seqpacket_allow ||
+ !new_transport->seqpacket_allow()) {
+ module_put(new_transport->module);
+ return -ESOCKTNOSUPPORT;
+ }
+ }
+
ret = new_transport->init(vsk, psk);
if (ret) {
module_put(new_transport->module);
@@ -684,6 +693,7 @@ static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr)

switch (sk->sk_socket->type) {
case SOCK_STREAM:
+ case SOCK_SEQPACKET:
spin_lock_bh(&vsock_table_lock);
retval = __vsock_bind_connectible(vsk, addr);
spin_unlock_bh(&vsock_table_lock);
@@ -769,7 +779,7 @@ static struct sock *__vsock_create(struct net *net,

static bool sock_type_connectible(u16 type)
{
- return type == SOCK_STREAM;
+ return (type == SOCK_STREAM) || (type == SOCK_SEQPACKET);
}

static void __vsock_release(struct sock *sk, int level)
@@ -2174,6 +2184,27 @@ static const struct proto_ops vsock_stream_ops = {
.sendpage = sock_no_sendpage,
};

+static const struct proto_ops vsock_seqpacket_ops = {
+ .family = PF_VSOCK,
+ .owner = THIS_MODULE,
+ .release = vsock_release,
+ .bind = vsock_bind,
+ .connect = vsock_connect,
+ .socketpair = sock_no_socketpair,
+ .accept = vsock_accept,
+ .getname = vsock_getname,
+ .poll = vsock_poll,
+ .ioctl = sock_no_ioctl,
+ .listen = vsock_listen,
+ .shutdown = vsock_shutdown,
+ .setsockopt = vsock_connectible_setsockopt,
+ .getsockopt = vsock_connectible_getsockopt,
+ .sendmsg = vsock_connectible_sendmsg,
+ .recvmsg = vsock_connectible_recvmsg,
+ .mmap = sock_no_mmap,
+ .sendpage = sock_no_sendpage,
+};
+
static int vsock_create(struct net *net, struct socket *sock,
int protocol, int kern)
{
@@ -2194,6 +2225,9 @@ static int vsock_create(struct net *net, struct socket *sock,
case SOCK_STREAM:
sock->ops = &vsock_stream_ops;
break;
+ case SOCK_SEQPACKET:
+ sock->ops = &vsock_seqpacket_ops;
+ break;
default:
return -ESOCKTNOSUPPORT;
}
--
2.25.1

2021-03-23 13:13:51

by Arseny Krasnov

[permalink] [raw]
Subject: [RFC PATCH v7 05/22] af_vsock: separate wait space loop

This moves loop that waits for space on send to separate function,
because it will be used for SEQ_BEGIN/SEQ_END sending before and
after data transmission. Waiting for SEQ_BEGIN/SEQ_END is needed
because such packets carries SEQPACKET header that couldn't be
fragmented by credit mechanism, so to avoid it, sender waits until
enough space will be ready.

Signed-off-by: Arseny Krasnov <[email protected]>
---
include/net/af_vsock.h | 2 +
net/vmw_vsock/af_vsock.c | 99 +++++++++++++++++++++++++---------------
2 files changed, 63 insertions(+), 38 deletions(-)

diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h
index 74ac8a4c4168..7232f6c42a36 100644
--- a/include/net/af_vsock.h
+++ b/include/net/af_vsock.h
@@ -204,6 +204,8 @@ void vsock_remove_sock(struct vsock_sock *vsk);
void vsock_for_each_connected_socket(void (*fn)(struct sock *sk));
int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk);
bool vsock_find_cid(unsigned int cid);
+int vsock_wait_space(struct sock *sk, size_t space, int flags,
+ struct vsock_transport_send_notify_data *send_data);

/**** TAP ****/

diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
index fa0c37f97330..617ffe42693d 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -1692,6 +1692,65 @@ static int vsock_connectible_getsockopt(struct socket *sock,
return 0;
}

+int vsock_wait_space(struct sock *sk, size_t space, int flags,
+ struct vsock_transport_send_notify_data *send_data)
+{
+ const struct vsock_transport *transport;
+ struct vsock_sock *vsk;
+ long timeout;
+ int err;
+
+ DEFINE_WAIT_FUNC(wait, woken_wake_function);
+
+ vsk = vsock_sk(sk);
+ transport = vsk->transport;
+ timeout = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
+ err = 0;
+
+ add_wait_queue(sk_sleep(sk), &wait);
+
+ while (vsock_stream_has_space(vsk) < space &&
+ sk->sk_err == 0 &&
+ !(sk->sk_shutdown & SEND_SHUTDOWN) &&
+ !(vsk->peer_shutdown & RCV_SHUTDOWN)) {
+
+ /* Don't wait for non-blocking sockets. */
+ if (timeout == 0) {
+ err = -EAGAIN;
+ goto out_err;
+ }
+
+ if (send_data) {
+ err = transport->notify_send_pre_block(vsk, send_data);
+ if (err < 0)
+ goto out_err;
+ }
+
+ release_sock(sk);
+ timeout = wait_woken(&wait, TASK_INTERRUPTIBLE, timeout);
+ lock_sock(sk);
+ if (signal_pending(current)) {
+ err = sock_intr_errno(timeout);
+ goto out_err;
+ } else if (timeout == 0) {
+ err = -EAGAIN;
+ goto out_err;
+ }
+ }
+
+ if (sk->sk_err) {
+ err = -sk->sk_err;
+ } else if ((sk->sk_shutdown & SEND_SHUTDOWN) ||
+ (vsk->peer_shutdown & RCV_SHUTDOWN)) {
+ err = -EPIPE;
+ }
+
+out_err:
+ remove_wait_queue(sk_sleep(sk), &wait);
+ return err;
+}
+EXPORT_SYMBOL_GPL(vsock_wait_space);
+
static int vsock_connectible_sendmsg(struct socket *sock, struct msghdr *msg,
size_t len)
{
@@ -1699,10 +1758,8 @@ static int vsock_connectible_sendmsg(struct socket *sock, struct msghdr *msg,
struct vsock_sock *vsk;
const struct vsock_transport *transport;
ssize_t total_written;
- long timeout;
int err;
struct vsock_transport_send_notify_data send_data;
- DEFINE_WAIT_FUNC(wait, woken_wake_function);

sk = sock->sk;
vsk = vsock_sk(sk);
@@ -1740,9 +1797,6 @@ static int vsock_connectible_sendmsg(struct socket *sock, struct msghdr *msg,
goto out;
}

- /* Wait for room in the produce queue to enqueue our user's data. */
- timeout = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
-
err = transport->notify_send_init(vsk, &send_data);
if (err < 0)
goto out;
@@ -1750,39 +1804,8 @@ static int vsock_connectible_sendmsg(struct socket *sock, struct msghdr *msg,
while (total_written < len) {
ssize_t written;

- add_wait_queue(sk_sleep(sk), &wait);
- while (vsock_stream_has_space(vsk) == 0 &&
- sk->sk_err == 0 &&
- !(sk->sk_shutdown & SEND_SHUTDOWN) &&
- !(vsk->peer_shutdown & RCV_SHUTDOWN)) {
-
- /* Don't wait for non-blocking sockets. */
- if (timeout == 0) {
- err = -EAGAIN;
- remove_wait_queue(sk_sleep(sk), &wait);
- goto out_err;
- }
-
- err = transport->notify_send_pre_block(vsk, &send_data);
- if (err < 0) {
- remove_wait_queue(sk_sleep(sk), &wait);
- goto out_err;
- }
-
- release_sock(sk);
- timeout = wait_woken(&wait, TASK_INTERRUPTIBLE, timeout);
- lock_sock(sk);
- if (signal_pending(current)) {
- err = sock_intr_errno(timeout);
- remove_wait_queue(sk_sleep(sk), &wait);
- goto out_err;
- } else if (timeout == 0) {
- err = -EAGAIN;
- remove_wait_queue(sk_sleep(sk), &wait);
- goto out_err;
- }
- }
- remove_wait_queue(sk_sleep(sk), &wait);
+ if (vsock_wait_space(sk, 1, msg->msg_flags, &send_data))
+ goto out_err;

/* These checks occur both as part of and after the loop
* conditional since we need to check before and after
--
2.25.1

2021-03-23 13:13:54

by Arseny Krasnov

[permalink] [raw]
Subject: [RFC PATCH v7 02/22] af_vsock: separate wait data loop

This moves wait loop for data to dedicated function, because later it
will be used by SEQPACKET data receive loop. While moving the code
around, let's update an old comment.

Signed-off-by: Arseny Krasnov <[email protected]>
Reviewed-by: Stefano Garzarella <[email protected]>
---
net/vmw_vsock/af_vsock.c | 156 +++++++++++++++++++++------------------
1 file changed, 84 insertions(+), 72 deletions(-)

diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
index 656370e11707..421c0303b26f 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -1832,6 +1832,69 @@ static int vsock_connectible_sendmsg(struct socket *sock, struct msghdr *msg,
return err;
}

+static int vsock_wait_data(struct sock *sk, struct wait_queue_entry *wait,
+ long timeout,
+ struct vsock_transport_recv_notify_data *recv_data,
+ size_t target)
+{
+ const struct vsock_transport *transport;
+ struct vsock_sock *vsk;
+ s64 data;
+ int err;
+
+ vsk = vsock_sk(sk);
+ err = 0;
+ transport = vsk->transport;
+
+ while ((data = vsock_stream_has_data(vsk)) == 0) {
+ prepare_to_wait(sk_sleep(sk), wait, TASK_INTERRUPTIBLE);
+
+ if (sk->sk_err != 0 ||
+ (sk->sk_shutdown & RCV_SHUTDOWN) ||
+ (vsk->peer_shutdown & SEND_SHUTDOWN)) {
+ break;
+ }
+
+ /* Don't wait for non-blocking sockets. */
+ if (timeout == 0) {
+ err = -EAGAIN;
+ break;
+ }
+
+ if (recv_data) {
+ err = transport->notify_recv_pre_block(vsk, target, recv_data);
+ if (err < 0)
+ break;
+ }
+
+ release_sock(sk);
+ timeout = schedule_timeout(timeout);
+ lock_sock(sk);
+
+ if (signal_pending(current)) {
+ err = sock_intr_errno(timeout);
+ break;
+ } else if (timeout == 0) {
+ err = -EAGAIN;
+ break;
+ }
+ }
+
+ finish_wait(sk_sleep(sk), wait);
+
+ if (err)
+ return err;
+
+ /* Internal transport error when checking for available
+ * data. XXX This should be changed to a connection
+ * reset in a later change.
+ */
+ if (data < 0)
+ return -ENOMEM;
+
+ return data;
+}
+
static int
vsock_connectible_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
int flags)
@@ -1911,85 +1974,34 @@ vsock_connectible_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,


while (1) {
- s64 ready;
+ ssize_t read;

- prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
- ready = vsock_stream_has_data(vsk);
+ err = vsock_wait_data(sk, &wait, timeout, &recv_data, target);
+ if (err <= 0)
+ break;

- if (ready == 0) {
- if (sk->sk_err != 0 ||
- (sk->sk_shutdown & RCV_SHUTDOWN) ||
- (vsk->peer_shutdown & SEND_SHUTDOWN)) {
- finish_wait(sk_sleep(sk), &wait);
- break;
- }
- /* Don't wait for non-blocking sockets. */
- if (timeout == 0) {
- err = -EAGAIN;
- finish_wait(sk_sleep(sk), &wait);
- break;
- }
-
- err = transport->notify_recv_pre_block(
- vsk, target, &recv_data);
- if (err < 0) {
- finish_wait(sk_sleep(sk), &wait);
- break;
- }
- release_sock(sk);
- timeout = schedule_timeout(timeout);
- lock_sock(sk);
-
- if (signal_pending(current)) {
- err = sock_intr_errno(timeout);
- finish_wait(sk_sleep(sk), &wait);
- break;
- } else if (timeout == 0) {
- err = -EAGAIN;
- finish_wait(sk_sleep(sk), &wait);
- break;
- }
- } else {
- ssize_t read;
-
- finish_wait(sk_sleep(sk), &wait);
-
- if (ready < 0) {
- /* Invalid queue pair content. XXX This should
- * be changed to a connection reset in a later
- * change.
- */
-
- err = -ENOMEM;
- goto out;
- }
-
- err = transport->notify_recv_pre_dequeue(
- vsk, target, &recv_data);
- if (err < 0)
- break;
+ err = transport->notify_recv_pre_dequeue(vsk, target,
+ &recv_data);
+ if (err < 0)
+ break;

- read = transport->stream_dequeue(
- vsk, msg,
- len - copied, flags);
- if (read < 0) {
- err = -ENOMEM;
- break;
- }
+ read = transport->stream_dequeue(vsk, msg, len - copied, flags);
+ if (read < 0) {
+ err = -ENOMEM;
+ break;
+ }

- copied += read;
+ copied += read;

- err = transport->notify_recv_post_dequeue(
- vsk, target, read,
- !(flags & MSG_PEEK), &recv_data);
- if (err < 0)
- goto out;
+ err = transport->notify_recv_post_dequeue(vsk, target, read,
+ !(flags & MSG_PEEK), &recv_data);
+ if (err < 0)
+ goto out;

- if (read >= target || flags & MSG_PEEK)
- break;
+ if (read >= target || flags & MSG_PEEK)
+ break;

- target -= read;
- }
+ target -= read;
}

if (sk->sk_err)
--
2.25.1

2021-03-23 13:13:54

by Arseny Krasnov

[permalink] [raw]
Subject: [RFC PATCH v7 08/22] af_vsock: update comments for stream sockets

This replaces 'stream' to 'connection oriented' in comments as
SEQPACKET is also connection oriented.

Signed-off-by: Arseny Krasnov <[email protected]>
Reviewed-by: Stefano Garzarella <[email protected]>
---
net/vmw_vsock/af_vsock.c | 31 +++++++++++++++++--------------
1 file changed, 17 insertions(+), 14 deletions(-)

diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
index 802afe253781..6a7652c75d3d 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -415,8 +415,8 @@ static void vsock_deassign_transport(struct vsock_sock *vsk)

/* Assign a transport to a socket and call the .init transport callback.
*
- * Note: for stream socket this must be called when vsk->remote_addr is set
- * (e.g. during the connect() or when a connection request on a listener
+ * Note: for connection oriented socket this must be called when vsk->remote_addr
+ * is set (e.g. during the connect() or when a connection request on a listener
* socket is received).
* The vsk->remote_addr is used to decide which transport to use:
* - remote CID == VMADDR_CID_LOCAL or g2h->local_cid or VMADDR_CID_HOST if
@@ -470,10 +470,10 @@ int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk)
return 0;

/* transport->release() must be called with sock lock acquired.
- * This path can only be taken during vsock_stream_connect(),
- * where we have already held the sock lock.
- * In the other cases, this function is called on a new socket
- * which is not assigned to any transport.
+ * This path can only be taken during vsock_connect(), where we
+ * have already held the sock lock. In the other cases, this
+ * function is called on a new socket which is not assigned to
+ * any transport.
*/
vsk->transport->release(vsk);
vsock_deassign_transport(vsk);
@@ -658,9 +658,10 @@ static int __vsock_bind_connectible(struct vsock_sock *vsk,

vsock_addr_init(&vsk->local_addr, new_addr.svm_cid, new_addr.svm_port);

- /* Remove stream sockets from the unbound list and add them to the hash
- * table for easy lookup by its address. The unbound list is simply an
- * extra entry at the end of the hash table, a trick used by AF_UNIX.
+ /* Remove connection oriented sockets from the unbound list and add them
+ * to the hash table for easy lookup by its address. The unbound list
+ * is simply an extra entry at the end of the hash table, a trick used
+ * by AF_UNIX.
*/
__vsock_remove_bound(vsk);
__vsock_insert_bound(vsock_bound_sockets(&vsk->local_addr), vsk);
@@ -951,10 +952,10 @@ static int vsock_shutdown(struct socket *sock, int mode)
if ((mode & ~SHUTDOWN_MASK) || !mode)
return -EINVAL;

- /* If this is a STREAM socket and it is not connected then bail out
- * immediately. If it is a DGRAM socket then we must first kick the
- * socket so that it wakes up from any sleeping calls, for example
- * recv(), and then afterwards return the error.
+ /* If this is a connection oriented socket and it is not connected then
+ * bail out immediately. If it is a DGRAM socket then we must first
+ * kick the socket so that it wakes up from any sleeping calls, for
+ * example recv(), and then afterwards return the error.
*/

sk = sock->sk;
@@ -1783,7 +1784,9 @@ static int vsock_connectible_sendmsg(struct socket *sock, struct msghdr *msg,

transport = vsk->transport;

- /* Callers should not provide a destination with stream sockets. */
+ /* Callers should not provide a destination with connection oriented
+ * sockets.
+ */
if (msg->msg_namelen) {
err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
goto out;
--
2.25.1

2021-03-23 13:15:31

by Arseny Krasnov

[permalink] [raw]
Subject: [RFC PATCH v7 10/22] virtio/vsock: simplify credit update function API

This function is static and 'hdr' arg was always NULL.

Signed-off-by: Arseny Krasnov <[email protected]>
Reviewed-by: Stefano Garzarella <[email protected]>
---
net/vmw_vsock/virtio_transport_common.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
index f69993d67f89..833104b71a1c 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -271,8 +271,7 @@ 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,
- struct virtio_vsock_hdr *hdr)
+static int virtio_transport_send_credit_update(struct vsock_sock *vsk)
{
struct virtio_vsock_pkt_info info = {
.op = VIRTIO_VSOCK_OP_CREDIT_UPDATE,
@@ -384,7 +383,7 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
* with different values.
*/
if (free_space < VIRTIO_VSOCK_MAX_PKT_BUF_SIZE)
- virtio_transport_send_credit_update(vsk, NULL);
+ virtio_transport_send_credit_update(vsk);

return total;

@@ -493,7 +492,7 @@ void virtio_transport_notify_buffer_size(struct vsock_sock *vsk, u64 *val)

vvs->buf_alloc = *val;

- virtio_transport_send_credit_update(vsk, NULL);
+ virtio_transport_send_credit_update(vsk);
}
EXPORT_SYMBOL_GPL(virtio_transport_notify_buffer_size);

--
2.25.1

2021-03-23 13:15:39

by Arseny Krasnov

[permalink] [raw]
Subject: [RFC PATCH v7 11/22] virtio/vsock: dequeue callback for SOCK_SEQPACKET

This adds transport callback and it's logic for SEQPACKET dequeue.
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. If during copy SEQ_BEGIN
was found(and not all data was copied), copying is restarted by reset
user's iov iterator(previous unfinished data is dropped).

Signed-off-by: Arseny Krasnov <[email protected]>
---
v6 -> v7:
1) 'struct virtio_vsock_seqpacket_state' now renamed to
'struct virtio_vsock_seq_state'.
2) Field 'seqpacket_state' of 'struct virtio_vsock_sock' now
renamed to 'seq_state'.
3) Current message length to process('user_read_seq_len') is
set to 0 on error or message dequeue completed sucecssfully.

include/linux/virtio_vsock.h | 14 +++
include/uapi/linux/virtio_vsock.h | 16 ++++
net/vmw_vsock/virtio_transport_common.c | 121 ++++++++++++++++++++++++
3 files changed, 151 insertions(+)

diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
index dc636b727179..0e3aa395c07c 100644
--- a/include/linux/virtio_vsock.h
+++ b/include/linux/virtio_vsock.h
@@ -18,6 +18,12 @@ enum {
VSOCK_VQ_MAX = 3,
};

+struct virtio_vsock_seq_state {
+ u32 user_read_seq_len;
+ u32 user_read_copied;
+ u32 curr_rx_msg_id;
+};
+
/* Per-socket state (accessed via vsk->trans) */
struct virtio_vsock_sock {
struct vsock_sock *vsk;
@@ -36,6 +42,8 @@ struct virtio_vsock_sock {
u32 rx_bytes;
u32 buf_alloc;
struct list_head rx_queue;
+
+ struct virtio_vsock_seq_state seq_state;
};

struct virtio_vsock_pkt {
@@ -80,6 +88,12 @@ virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
struct msghdr *msg,
size_t len, int flags);

+int
+virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
+ struct msghdr *msg,
+ int flags,
+ bool *msg_ready,
+ size_t *msg_len);
s64 virtio_transport_stream_has_data(struct vsock_sock *vsk);
s64 virtio_transport_stream_has_space(struct vsock_sock *vsk);

diff --git a/include/uapi/linux/virtio_vsock.h b/include/uapi/linux/virtio_vsock.h
index 1d57ed3d84d2..692f8078cced 100644
--- a/include/uapi/linux/virtio_vsock.h
+++ b/include/uapi/linux/virtio_vsock.h
@@ -63,8 +63,14 @@ struct virtio_vsock_hdr {
__le32 fwd_cnt;
} __attribute__((packed));

+struct virtio_vsock_seq_hdr {
+ __le32 msg_id;
+ __le32 msg_len;
+} __attribute__((packed));
+
enum virtio_vsock_type {
VIRTIO_VSOCK_TYPE_STREAM = 1,
+ VIRTIO_VSOCK_TYPE_SEQPACKET = 2,
};

enum virtio_vsock_op {
@@ -83,6 +89,11 @@ enum virtio_vsock_op {
VIRTIO_VSOCK_OP_CREDIT_UPDATE = 6,
/* Request the peer to send the credit info to us */
VIRTIO_VSOCK_OP_CREDIT_REQUEST = 7,
+
+ /* Record begin for SOCK_SEQPACKET */
+ VIRTIO_VSOCK_OP_SEQ_BEGIN = 8,
+ /* Record end for SOCK_SEQPACKET */
+ VIRTIO_VSOCK_OP_SEQ_END = 9,
};

/* VIRTIO_VSOCK_OP_SHUTDOWN flags values */
@@ -91,4 +102,9 @@ enum virtio_vsock_shutdown {
VIRTIO_VSOCK_SHUTDOWN_SEND = 2,
};

+/* VIRTIO_VSOCK_OP_RW flags values */
+enum virtio_vsock_rw {
+ VIRTIO_VSOCK_RW_EOR = 1,
+};
+
#endif /* _UAPI_LINUX_VIRTIO_VSOCK_H */
diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
index 833104b71a1c..a8f4326e45e8 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -393,6 +393,114 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
return err;
}

+static inline void virtio_transport_remove_pkt(struct virtio_vsock_pkt *pkt)
+{
+ list_del(&pkt->list);
+ virtio_transport_free_pkt(pkt);
+}
+
+static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
+ struct msghdr *msg,
+ bool *msg_ready)
+{
+ struct virtio_vsock_sock *vvs = vsk->trans;
+ struct virtio_vsock_pkt *pkt;
+ int err = 0;
+ size_t user_buf_len = msg->msg_iter.count;
+
+ *msg_ready = false;
+ spin_lock_bh(&vvs->rx_lock);
+
+ while (!*msg_ready && !list_empty(&vvs->rx_queue) && !err) {
+ pkt = list_first_entry(&vvs->rx_queue, struct virtio_vsock_pkt, list);
+
+ switch (le16_to_cpu(pkt->hdr.op)) {
+ case VIRTIO_VSOCK_OP_SEQ_BEGIN: {
+ /* Unexpected 'SEQ_BEGIN' during record copy:
+ * Leave receive loop, 'EAGAIN' will restart it from
+ * outer receive loop, packet is still in queue and
+ * counters are cleared. So in next loop enter,
+ * 'SEQ_BEGIN' will be dequeued first. User's iov
+ * iterator will be reset in outer loop. Also
+ * send credit update, because some bytes could be
+ * copied. User will never see unfinished record.
+ */
+ err = -EAGAIN;
+ break;
+ }
+ case VIRTIO_VSOCK_OP_SEQ_END: {
+ struct virtio_vsock_seq_hdr *seq_hdr;
+
+ seq_hdr = (struct virtio_vsock_seq_hdr *)pkt->buf;
+ /* First check that whole record is received. */
+
+ if (vvs->seq_state.user_read_copied !=
+ vvs->seq_state.user_read_seq_len ||
+ le32_to_cpu(seq_hdr->msg_id) !=
+ vvs->seq_state.curr_rx_msg_id) {
+ /* Tail of current record and head of next missed,
+ * so this EOR is from next record. Restart receive.
+ * Current record will be dropped, next headless will
+ * be dropped on next attempt to get record length.
+ */
+ err = -EAGAIN;
+ } else {
+ /* Success. */
+ *msg_ready = true;
+ }
+
+ break;
+ }
+ case VIRTIO_VSOCK_OP_RW: {
+ size_t bytes_to_copy;
+ size_t pkt_len;
+
+ pkt_len = (size_t)le32_to_cpu(pkt->hdr.len);
+ bytes_to_copy = min(user_buf_len, pkt_len);
+
+ /* 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)) {
+ spin_lock_bh(&vvs->rx_lock);
+ err = -EINVAL;
+ break;
+ }
+
+ spin_lock_bh(&vvs->rx_lock);
+ user_buf_len -= bytes_to_copy;
+ vvs->seq_state.user_read_copied += pkt_len;
+
+ if (le32_to_cpu(pkt->hdr.flags) & VIRTIO_VSOCK_RW_EOR)
+ msg->msg_flags |= MSG_EOR;
+ break;
+ }
+ default:
+ ;
+ }
+
+ /* For unexpected 'SEQ_BEGIN', keep such packet in queue,
+ * but drop any other type of packet.
+ */
+ if (le16_to_cpu(pkt->hdr.op) != VIRTIO_VSOCK_OP_SEQ_BEGIN) {
+ virtio_transport_dec_rx_pkt(vvs, pkt);
+ virtio_transport_remove_pkt(pkt);
+ }
+ }
+
+ /* Reset current record length on error or whole message received. */
+ if (*msg_ready || err)
+ vvs->seq_state.user_read_seq_len = 0;
+
+ spin_unlock_bh(&vvs->rx_lock);
+
+ virtio_transport_send_credit_update(vsk);
+
+ return err;
+}
+
ssize_t
virtio_transport_stream_dequeue(struct vsock_sock *vsk,
struct msghdr *msg,
@@ -405,6 +513,19 @@ virtio_transport_stream_dequeue(struct vsock_sock *vsk,
}
EXPORT_SYMBOL_GPL(virtio_transport_stream_dequeue);

+int
+virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
+ struct msghdr *msg,
+ int flags, bool *msg_ready,
+ size_t *msg_len)
+{
+ if (flags & MSG_PEEK)
+ return -EOPNOTSUPP;
+
+ return virtio_transport_seqpacket_do_dequeue(vsk, msg, msg_ready);
+}
+EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_dequeue);
+
int
virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
struct msghdr *msg,
--
2.25.1

2021-03-23 13:15:51

by Arseny Krasnov

[permalink] [raw]
Subject: [RFC PATCH v7 12/22] virtio/vsock: fetch length for SEQPACKET record

This adds transport callback which tries to fetch record begin marker
from socket's rx queue. It is called from af_vsock.c before reading data
packets of record.

Signed-off-by: Arseny Krasnov <[email protected]>
---
v6 -> v7:
1) Now 'virtio_transport_seqpacket_seq_get_len()' returns 0, if rx
queue of socket is empty. Else it returns length of current message
to handle.
2) If dequeue callback is called, but there is no detected length of
message to dequeue, EAGAIN is returned, and outer loop restarts
receiving.

net/vmw_vsock/virtio_transport_common.c | 61 +++++++++++++++++++++++++
1 file changed, 61 insertions(+)

diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
index a8f4326e45e8..41f05034593e 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -399,6 +399,62 @@ static inline void virtio_transport_remove_pkt(struct virtio_vsock_pkt *pkt)
virtio_transport_free_pkt(pkt);
}

+static size_t virtio_transport_drop_until_seq_begin(struct virtio_vsock_sock *vvs)
+{
+ struct virtio_vsock_pkt *pkt, *n;
+ size_t bytes_dropped = 0;
+
+ list_for_each_entry_safe(pkt, n, &vvs->rx_queue, list) {
+ if (le16_to_cpu(pkt->hdr.op) == VIRTIO_VSOCK_OP_SEQ_BEGIN)
+ break;
+
+ bytes_dropped += le32_to_cpu(pkt->hdr.len);
+ virtio_transport_dec_rx_pkt(vvs, pkt);
+ virtio_transport_remove_pkt(pkt);
+ }
+
+ return bytes_dropped;
+}
+
+static size_t virtio_transport_seqpacket_seq_get_len(struct vsock_sock *vsk)
+{
+ struct virtio_vsock_seq_hdr *seq_hdr;
+ struct virtio_vsock_sock *vvs;
+ struct virtio_vsock_pkt *pkt;
+ size_t bytes_dropped = 0;
+
+ vvs = vsk->trans;
+
+ spin_lock_bh(&vvs->rx_lock);
+
+ /* Have some record to process, return it's length. */
+ if (vvs->seq_state.user_read_seq_len)
+ goto out;
+
+ /* Fetch all orphaned 'RW' packets and send credit update. */
+ bytes_dropped = virtio_transport_drop_until_seq_begin(vvs);
+
+ if (list_empty(&vvs->rx_queue))
+ goto out;
+
+ pkt = list_first_entry(&vvs->rx_queue, struct virtio_vsock_pkt, list);
+
+ vvs->seq_state.user_read_copied = 0;
+
+ seq_hdr = (struct virtio_vsock_seq_hdr *)pkt->buf;
+ vvs->seq_state.user_read_seq_len = le32_to_cpu(seq_hdr->msg_len);
+ vvs->seq_state.curr_rx_msg_id = le32_to_cpu(seq_hdr->msg_id);
+ virtio_transport_dec_rx_pkt(vvs, pkt);
+ virtio_transport_remove_pkt(pkt);
+out:
+ spin_unlock_bh(&vvs->rx_lock);
+
+ if (bytes_dropped)
+ virtio_transport_send_credit_update(vsk);
+
+ return vvs->seq_state.user_read_seq_len;
+}
+
static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
struct msghdr *msg,
bool *msg_ready)
@@ -522,6 +578,11 @@ virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
if (flags & MSG_PEEK)
return -EOPNOTSUPP;

+ *msg_len = virtio_transport_seqpacket_seq_get_len(vsk);
+
+ if (*msg_len == 0)
+ return -EAGAIN;
+
return virtio_transport_seqpacket_do_dequeue(vsk, msg, msg_ready);
}
EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_dequeue);
--
2.25.1

2021-03-23 13:15:52

by Arseny Krasnov

[permalink] [raw]
Subject: [RFC PATCH v7 13/22] virtio/vsock: add SEQPACKET receive logic

This modifies current receive logic for SEQPACKET support:
1) Inserts 'SEQ_BEGIN' packet to socket's rx queue.
2) Inserts 'RW' packet to socket's rx queue, but without merging with
buffer of last packet in queue.
3) Performs check for packet and socket types on receive(if mismatch,
then reset connection).

Signed-off-by: Arseny Krasnov <[email protected]>
---
v6 -> v7:
In 'virtio_transport_recv_pkt()', 'sock_put()' is added, when type of
received packet does not match to the type of socket.

net/vmw_vsock/virtio_transport_common.c | 64 +++++++++++++++++--------
1 file changed, 45 insertions(+), 19 deletions(-)

diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
index 41f05034593e..bfe0d7026bf8 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -165,6 +165,14 @@ void virtio_transport_deliver_tap_pkt(struct virtio_vsock_pkt *pkt)
}
EXPORT_SYMBOL_GPL(virtio_transport_deliver_tap_pkt);

+static u16 virtio_transport_get_type(struct sock *sk)
+{
+ if (sk->sk_type == SOCK_STREAM)
+ return VIRTIO_VSOCK_TYPE_STREAM;
+ else
+ return VIRTIO_VSOCK_TYPE_SEQPACKET;
+}
+
/* This function can only be used on connecting/connected sockets,
* since a socket assigned to a transport is required.
*
@@ -1075,25 +1083,27 @@ virtio_transport_recv_enqueue(struct vsock_sock *vsk,
goto out;
}

- /* Try to copy small packets into the buffer of last packet queued,
- * to avoid wasting memory queueing the entire buffer with a small
- * payload.
- */
- if (pkt->len <= GOOD_COPY_LEN && !list_empty(&vvs->rx_queue)) {
- struct virtio_vsock_pkt *last_pkt;
+ if (le16_to_cpu(pkt->hdr.type) == VIRTIO_VSOCK_TYPE_STREAM) {
+ /* Try to copy small packets into the buffer of last packet queued,
+ * to avoid wasting memory queueing the entire buffer with a small
+ * payload.
+ */
+ if (pkt->len <= GOOD_COPY_LEN && !list_empty(&vvs->rx_queue)) {
+ struct virtio_vsock_pkt *last_pkt;

- last_pkt = list_last_entry(&vvs->rx_queue,
- struct virtio_vsock_pkt, list);
+ last_pkt = list_last_entry(&vvs->rx_queue,
+ struct virtio_vsock_pkt, list);

- /* If there is space in the last packet queued, we copy the
- * new packet in its buffer.
- */
- if (pkt->len <= last_pkt->buf_len - last_pkt->len) {
- memcpy(last_pkt->buf + last_pkt->len, pkt->buf,
- pkt->len);
- last_pkt->len += pkt->len;
- free_pkt = true;
- goto out;
+ /* If there is space in the last packet queued, we copy the
+ * new packet in its buffer.
+ */
+ if (pkt->len <= last_pkt->buf_len - last_pkt->len) {
+ memcpy(last_pkt->buf + last_pkt->len, pkt->buf,
+ pkt->len);
+ last_pkt->len += pkt->len;
+ free_pkt = true;
+ goto out;
+ }
}
}

@@ -1113,9 +1123,13 @@ virtio_transport_recv_connected(struct sock *sk,
int err = 0;

switch (le16_to_cpu(pkt->hdr.op)) {
+ case VIRTIO_VSOCK_OP_SEQ_BEGIN:
+ case VIRTIO_VSOCK_OP_SEQ_END:
case VIRTIO_VSOCK_OP_RW:
virtio_transport_recv_enqueue(vsk, pkt);
- sk->sk_data_ready(sk);
+
+ if (le16_to_cpu(pkt->hdr.op) != VIRTIO_VSOCK_OP_SEQ_BEGIN)
+ sk->sk_data_ready(sk);
return err;
case VIRTIO_VSOCK_OP_CREDIT_UPDATE:
sk->sk_write_space(sk);
@@ -1258,6 +1272,12 @@ virtio_transport_recv_listen(struct sock *sk, struct virtio_vsock_pkt *pkt,
return 0;
}

+static bool virtio_transport_valid_type(u16 type)
+{
+ return (type == VIRTIO_VSOCK_TYPE_STREAM) ||
+ (type == VIRTIO_VSOCK_TYPE_SEQPACKET);
+}
+
/* We are under the virtio-vsock's vsock->rx_lock or vhost-vsock's vq->mutex
* lock.
*/
@@ -1283,7 +1303,7 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,
le32_to_cpu(pkt->hdr.buf_alloc),
le32_to_cpu(pkt->hdr.fwd_cnt));

- if (le16_to_cpu(pkt->hdr.type) != VIRTIO_VSOCK_TYPE_STREAM) {
+ if (!virtio_transport_valid_type(le16_to_cpu(pkt->hdr.type))) {
(void)virtio_transport_reset_no_sock(t, pkt);
goto free_pkt;
}
@@ -1300,6 +1320,12 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,
}
}

+ if (virtio_transport_get_type(sk) != le16_to_cpu(pkt->hdr.type)) {
+ (void)virtio_transport_reset_no_sock(t, pkt);
+ sock_put(sk);
+ goto free_pkt;
+ }
+
vsk = vsock_sk(sk);

lock_sock(sk);
--
2.25.1

2021-03-23 13:16:14

by Arseny Krasnov

[permalink] [raw]
Subject: [RFC PATCH v7 09/22] virtio/vsock: set packet's type in virtio_transport_send_pkt_info()

This moves passing type of packet from 'info' structure to 'virtio_
transport_send_pkt_info()' function. There is no need to set type of
packet which differs from type of socket. 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 e4370b1b7494..f69993d67f89 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,
};
@@ -990,7 +982,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

2021-03-23 13:16:47

by Arseny Krasnov

[permalink] [raw]
Subject: [RFC PATCH v7 14/22] virtio/vsock: rest of SOCK_SEQPACKET support

This adds rest of logic for SEQPACKET:
1) SEQPACKET specific functions which send SEQ_BEGIN/SEQ_END.
Note that both functions may sleep to wait enough space for
SEQPACKET header.
2) SEQ_BEGIN/SEQ_END in TAP packet capture.
3) Send SHUTDOWN on socket close for SEQPACKET type.
4) Set SEQPACKET packet type during send.
5) Set MSG_EOR in flags for SEQPACKET during send.
6) 'seqpacket_allow' flag to virtio transport.

Signed-off-by: Arseny Krasnov <[email protected]>
---
v6 -> v7:
In 'virtio_transport_seqpacket_enqueue()', 'next_tx_msg_id' is updated
in both cases when message send successfully or error occured.

include/linux/virtio_vsock.h | 7 ++
net/vmw_vsock/virtio_transport_common.c | 88 ++++++++++++++++++++++++-
2 files changed, 93 insertions(+), 2 deletions(-)

diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
index 0e3aa395c07c..ab5f56fd7251 100644
--- a/include/linux/virtio_vsock.h
+++ b/include/linux/virtio_vsock.h
@@ -22,6 +22,7 @@ struct virtio_vsock_seq_state {
u32 user_read_seq_len;
u32 user_read_copied;
u32 curr_rx_msg_id;
+ u32 next_tx_msg_id;
};

/* Per-socket state (accessed via vsk->trans) */
@@ -76,6 +77,8 @@ struct virtio_transport {

/* Takes ownership of the packet */
int (*send_pkt)(struct virtio_vsock_pkt *pkt);
+
+ bool seqpacket_allow;
};

ssize_t
@@ -89,6 +92,10 @@ virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
size_t len, int flags);

int
+virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk,
+ struct msghdr *msg,
+ size_t len);
+int
virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
struct msghdr *msg,
int flags,
diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
index bfe0d7026bf8..01a56c7da8bd 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -139,6 +139,8 @@ static struct sk_buff *virtio_transport_build_skb(void *opaque)
break;
case VIRTIO_VSOCK_OP_CREDIT_UPDATE:
case VIRTIO_VSOCK_OP_CREDIT_REQUEST:
+ case VIRTIO_VSOCK_OP_SEQ_BEGIN:
+ case VIRTIO_VSOCK_OP_SEQ_END:
hdr->op = cpu_to_le16(AF_VSOCK_OP_CONTROL);
break;
default:
@@ -187,7 +189,12 @@ 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));
+
+ if (info->type == VIRTIO_VSOCK_TYPE_SEQPACKET &&
+ info->msg &&
+ info->msg->msg_flags & MSG_EOR)
+ info->flags |= VIRTIO_VSOCK_RW_EOR;

t_ops = virtio_transport_get_ops(vsk);
if (unlikely(!t_ops))
@@ -401,6 +408,43 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
return err;
}

+static int virtio_transport_seqpacket_send_ctrl(struct vsock_sock *vsk,
+ int type,
+ size_t len,
+ int flags)
+{
+ struct virtio_vsock_sock *vvs = vsk->trans;
+ struct virtio_vsock_pkt_info info = {
+ .op = type,
+ .vsk = vsk,
+ .pkt_len = sizeof(struct virtio_vsock_seq_hdr)
+ };
+
+ struct virtio_vsock_seq_hdr seq_hdr = {
+ .msg_id = cpu_to_le32(vvs->seq_state.next_tx_msg_id),
+ .msg_len = cpu_to_le32(len)
+ };
+
+ struct kvec seq_hdr_kiov = {
+ .iov_base = (void *)&seq_hdr,
+ .iov_len = sizeof(struct virtio_vsock_seq_hdr)
+ };
+
+ struct msghdr msg = {0};
+
+ //XXX: do we need 'vsock_transport_send_notify_data' pointer?
+ if (vsock_wait_space(sk_vsock(vsk),
+ sizeof(struct virtio_vsock_seq_hdr),
+ flags, NULL))
+ return -1;
+
+ iov_iter_kvec(&msg.msg_iter, WRITE, &seq_hdr_kiov, 1, sizeof(seq_hdr));
+
+ info.msg = &msg;
+
+ return virtio_transport_send_pkt_info(vsk, &info);
+}
+
static inline void virtio_transport_remove_pkt(struct virtio_vsock_pkt *pkt)
{
list_del(&pkt->list);
@@ -595,6 +639,46 @@ 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)
+{
+ int written = -1;
+
+ if (msg->msg_iter.iov_offset == 0) {
+ /* Send SEQBEGIN. */
+ if (virtio_transport_seqpacket_send_ctrl(vsk,
+ VIRTIO_VSOCK_OP_SEQ_BEGIN,
+ len,
+ msg->msg_flags) < 0)
+ goto out;
+ }
+
+ written = virtio_transport_stream_enqueue(vsk, msg, len);
+
+ if (written < 0)
+ goto out;
+
+ if (msg->msg_iter.count == 0) {
+ /* Send SEQEND. */
+ virtio_transport_seqpacket_send_ctrl(vsk,
+ VIRTIO_VSOCK_OP_SEQ_END,
+ 0,
+ msg->msg_flags);
+ }
+out:
+ /* Update next id on error or message transmission done. */
+ if (written < 0 || msg->msg_iter.count == 0) {
+ struct virtio_vsock_sock *vvs = vsk->trans;
+
+ vvs->seq_state.next_tx_msg_id++;
+ }
+
+ return written;
+}
+EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_enqueue);
+
int
virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
struct msghdr *msg,
@@ -1014,7 +1098,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);

list_for_each_entry_safe(pkt, tmp, &vvs->rx_queue, list) {
--
2.25.1

2021-03-23 13:16:54

by Arseny Krasnov

[permalink] [raw]
Subject: [RFC PATCH v7 15/22] virtio/vsock: SEQPACKET support feature bit

This adds new virtio vsock specific feature bit which means
SOCK_SEQPACKET support. Guest negotiates this bit with vhost,
thus checking that vhost side supports SEQPACKET.

Signed-off-by: Arseny Krasnov <[email protected]>
---
include/uapi/linux/virtio_vsock.h | 3 +++
1 file changed, 3 insertions(+)

diff --git a/include/uapi/linux/virtio_vsock.h b/include/uapi/linux/virtio_vsock.h
index 692f8078cced..619aaebb355a 100644
--- a/include/uapi/linux/virtio_vsock.h
+++ b/include/uapi/linux/virtio_vsock.h
@@ -38,6 +38,9 @@
#include <linux/virtio_ids.h>
#include <linux/virtio_config.h>

+/* The feature bitmap for virtio vsock */
+#define VIRTIO_VSOCK_F_SEQPACKET 0 /* SOCK_SEQPACKET supported */
+
struct virtio_vsock_config {
__le64 guest_cid;
} __attribute__((packed));
--
2.25.1

2021-03-23 13:17:10

by Arseny Krasnov

[permalink] [raw]
Subject: [RFC PATCH v7 16/22] virtio/vsock: setup SEQPACKET ops for transport

This adds SEQPACKET ops for virtio transport and 'seqpacket_allow()'
callback.

Signed-off-by: Arseny Krasnov <[email protected]>
---
net/vmw_vsock/virtio_transport.c | 12 ++++++++++++
1 file changed, 12 insertions(+)

diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c
index 2700a63ab095..83ae2078c847 100644
--- a/net/vmw_vsock/virtio_transport.c
+++ b/net/vmw_vsock/virtio_transport.c
@@ -443,6 +443,8 @@ static void virtio_vsock_rx_done(struct virtqueue *vq)
queue_work(virtio_vsock_workqueue, &vsock->rx_work);
}

+static bool virtio_transport_seqpacket_allow(void);
+
static struct virtio_transport virtio_transport = {
.transport = {
.module = THIS_MODULE,
@@ -469,6 +471,10 @@ static struct virtio_transport virtio_transport = {
.stream_is_active = virtio_transport_stream_is_active,
.stream_allow = virtio_transport_stream_allow,

+ .seqpacket_dequeue = virtio_transport_seqpacket_dequeue,
+ .seqpacket_enqueue = virtio_transport_seqpacket_enqueue,
+ .seqpacket_allow = virtio_transport_seqpacket_allow,
+
.notify_poll_in = virtio_transport_notify_poll_in,
.notify_poll_out = virtio_transport_notify_poll_out,
.notify_recv_init = virtio_transport_notify_recv_init,
@@ -483,8 +489,14 @@ static struct virtio_transport virtio_transport = {
},

.send_pkt = virtio_transport_send_pkt,
+ .seqpacket_allow = false
};

+static bool virtio_transport_seqpacket_allow(void)
+{
+ return virtio_transport.seqpacket_allow;
+}
+
static void virtio_transport_rx_work(struct work_struct *work)
{
struct virtio_vsock *vsock =
--
2.25.1

2021-03-23 13:17:41

by Arseny Krasnov

[permalink] [raw]
Subject: [RFC PATCH v7 17/22] vhost/vsock: setup SEQPACKET ops for transport

This also removes ignore of non-stream type of packets and adds
'seqpacket_allow()' callback.

Signed-off-by: Arseny Krasnov <[email protected]>
---
drivers/vhost/vsock.c | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
index 5e78fb719602..5af141772068 100644
--- a/drivers/vhost/vsock.c
+++ b/drivers/vhost/vsock.c
@@ -354,8 +354,7 @@ vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq,
return NULL;
}

- if (le16_to_cpu(pkt->hdr.type) == VIRTIO_VSOCK_TYPE_STREAM)
- pkt->len = le32_to_cpu(pkt->hdr.len);
+ pkt->len = le32_to_cpu(pkt->hdr.len);

/* No payload */
if (!pkt->len)
@@ -398,6 +397,8 @@ static bool vhost_vsock_more_replies(struct vhost_vsock *vsock)
return val < vq->num;
}

+static bool vhost_transport_seqpacket_allow(void);
+
static struct virtio_transport vhost_transport = {
.transport = {
.module = THIS_MODULE,
@@ -424,6 +425,10 @@ static struct virtio_transport vhost_transport = {
.stream_is_active = virtio_transport_stream_is_active,
.stream_allow = virtio_transport_stream_allow,

+ .seqpacket_dequeue = virtio_transport_seqpacket_dequeue,
+ .seqpacket_enqueue = virtio_transport_seqpacket_enqueue,
+ .seqpacket_allow = vhost_transport_seqpacket_allow,
+
.notify_poll_in = virtio_transport_notify_poll_in,
.notify_poll_out = virtio_transport_notify_poll_out,
.notify_recv_init = virtio_transport_notify_recv_init,
@@ -439,8 +444,14 @@ static struct virtio_transport vhost_transport = {
},

.send_pkt = vhost_transport_send_pkt,
+ .seqpacket_allow = false
};

+static bool vhost_transport_seqpacket_allow(void)
+{
+ return vhost_transport.seqpacket_allow;
+}
+
static void vhost_vsock_handle_tx_kick(struct vhost_work *work)
{
struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
--
2.25.1

2021-03-23 13:18:04

by Arseny Krasnov

[permalink] [raw]
Subject: [RFC PATCH v7 18/22] vsock/loopback: setup SEQPACKET ops for transport

This adds SEQPACKET ops for loopback transport and 'seqpacket_allow()'
callback.

Signed-off-by: Arseny Krasnov <[email protected]>
---
net/vmw_vsock/vsock_loopback.c | 12 ++++++++++++
1 file changed, 12 insertions(+)

diff --git a/net/vmw_vsock/vsock_loopback.c b/net/vmw_vsock/vsock_loopback.c
index a45f7ffca8c5..d38ffdbecc84 100644
--- a/net/vmw_vsock/vsock_loopback.c
+++ b/net/vmw_vsock/vsock_loopback.c
@@ -63,6 +63,8 @@ static int vsock_loopback_cancel_pkt(struct vsock_sock *vsk)
return 0;
}

+static bool vsock_loopback_seqpacket_allow(void);
+
static struct virtio_transport loopback_transport = {
.transport = {
.module = THIS_MODULE,
@@ -89,6 +91,10 @@ static struct virtio_transport loopback_transport = {
.stream_is_active = virtio_transport_stream_is_active,
.stream_allow = virtio_transport_stream_allow,

+ .seqpacket_dequeue = virtio_transport_seqpacket_dequeue,
+ .seqpacket_enqueue = virtio_transport_seqpacket_enqueue,
+ .seqpacket_allow = vsock_loopback_seqpacket_allow,
+
.notify_poll_in = virtio_transport_notify_poll_in,
.notify_poll_out = virtio_transport_notify_poll_out,
.notify_recv_init = virtio_transport_notify_recv_init,
@@ -103,8 +109,14 @@ static struct virtio_transport loopback_transport = {
},

.send_pkt = vsock_loopback_send_pkt,
+ .seqpacket_allow = true
};

+static bool vsock_loopback_seqpacket_allow(void)
+{
+ return loopback_transport.seqpacket_allow;
+}
+
static void vsock_loopback_work(struct work_struct *work)
{
struct vsock_loopback *vsock =
--
2.25.1

2021-03-23 13:18:12

by Arseny Krasnov

[permalink] [raw]
Subject: [RFC PATCH v7 19/22] vhost/vsock: SEQPACKET feature bit support

This adds handling of SEQPACKET bit: if guest sets features with
this bit cleared, then SOCK_SEQPACKET support will be disabled.

Signed-off-by: Arseny Krasnov <[email protected]>
---
drivers/vhost/vsock.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
index 5af141772068..710d99e61ccc 100644
--- a/drivers/vhost/vsock.c
+++ b/drivers/vhost/vsock.c
@@ -31,7 +31,8 @@

enum {
VHOST_VSOCK_FEATURES = VHOST_FEATURES |
- (1ULL << VIRTIO_F_ACCESS_PLATFORM)
+ (1ULL << VIRTIO_F_ACCESS_PLATFORM) |
+ (1ULL << VIRTIO_VSOCK_F_SEQPACKET)
};

enum {
@@ -796,6 +797,9 @@ static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
goto err;
}

+ if (features & (1ULL << VIRTIO_VSOCK_F_SEQPACKET))
+ vhost_transport.seqpacket_allow = true;
+
for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
vq = &vsock->vqs[i];
mutex_lock(&vq->mutex);
--
2.25.1

2021-03-23 13:18:29

by Arseny Krasnov

[permalink] [raw]
Subject: [RFC PATCH v7 20/22] virtio/vsock: SEQPACKET feature bit support

This adds handling of SEQPACKET bit: guest tries to negotiate it
with vhost.

Signed-off-by: Arseny Krasnov <[email protected]>
---
net/vmw_vsock/virtio_transport.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c
index 83ae2078c847..ee99bd919a12 100644
--- a/net/vmw_vsock/virtio_transport.c
+++ b/net/vmw_vsock/virtio_transport.c
@@ -624,6 +624,10 @@ static int virtio_vsock_probe(struct virtio_device *vdev)
rcu_assign_pointer(the_virtio_vsock, vsock);

mutex_unlock(&the_virtio_vsock_mutex);
+
+ if (vdev->features & (1ULL << VIRTIO_VSOCK_F_SEQPACKET))
+ virtio_transport.seqpacket_allow = true;
+
return 0;

out:
@@ -707,6 +711,7 @@ static struct virtio_device_id id_table[] = {
};

static unsigned int features[] = {
+ VIRTIO_VSOCK_F_SEQPACKET
};

static struct virtio_driver virtio_vsock_driver = {
--
2.25.1

2021-03-23 13:20:43

by Arseny Krasnov

[permalink] [raw]
Subject: [RFC PATCH v7 22/22] virtio/vsock: update trace event for SEQPACKET

This updates current implementation for trace event of virtio vsock:
SEQPACKET socket's type, SEQPACKET specific ops and SEQPACKET 'msg_len'
and 'msg_id' fields are added.

Signed-off-by: Arseny Krasnov <[email protected]>
---
.../events/vsock_virtio_transport_common.h | 48 ++++++++++++++-----
net/vmw_vsock/virtio_transport_common.c | 25 +++++++++-
2 files changed, 60 insertions(+), 13 deletions(-)

diff --git a/include/trace/events/vsock_virtio_transport_common.h b/include/trace/events/vsock_virtio_transport_common.h
index 6782213778be..3254607eac04 100644
--- a/include/trace/events/vsock_virtio_transport_common.h
+++ b/include/trace/events/vsock_virtio_transport_common.h
@@ -9,9 +9,12 @@
#include <linux/tracepoint.h>

TRACE_DEFINE_ENUM(VIRTIO_VSOCK_TYPE_STREAM);
+TRACE_DEFINE_ENUM(VIRTIO_VSOCK_TYPE_SEQPACKET);

#define show_type(val) \
- __print_symbolic(val, { VIRTIO_VSOCK_TYPE_STREAM, "STREAM" })
+ __print_symbolic(val, \
+ { VIRTIO_VSOCK_TYPE_STREAM, "STREAM" }, \
+ { VIRTIO_VSOCK_TYPE_SEQPACKET, "SEQPACKET" })

TRACE_DEFINE_ENUM(VIRTIO_VSOCK_OP_INVALID);
TRACE_DEFINE_ENUM(VIRTIO_VSOCK_OP_REQUEST);
@@ -21,6 +24,8 @@ TRACE_DEFINE_ENUM(VIRTIO_VSOCK_OP_SHUTDOWN);
TRACE_DEFINE_ENUM(VIRTIO_VSOCK_OP_RW);
TRACE_DEFINE_ENUM(VIRTIO_VSOCK_OP_CREDIT_UPDATE);
TRACE_DEFINE_ENUM(VIRTIO_VSOCK_OP_CREDIT_REQUEST);
+TRACE_DEFINE_ENUM(VIRTIO_VSOCK_OP_SEQ_BEGIN);
+TRACE_DEFINE_ENUM(VIRTIO_VSOCK_OP_SEQ_END);

#define show_op(val) \
__print_symbolic(val, \
@@ -31,7 +36,9 @@ TRACE_DEFINE_ENUM(VIRTIO_VSOCK_OP_CREDIT_REQUEST);
{ VIRTIO_VSOCK_OP_SHUTDOWN, "SHUTDOWN" }, \
{ VIRTIO_VSOCK_OP_RW, "RW" }, \
{ VIRTIO_VSOCK_OP_CREDIT_UPDATE, "CREDIT_UPDATE" }, \
- { VIRTIO_VSOCK_OP_CREDIT_REQUEST, "CREDIT_REQUEST" })
+ { VIRTIO_VSOCK_OP_CREDIT_REQUEST, "CREDIT_REQUEST" }, \
+ { VIRTIO_VSOCK_OP_SEQ_BEGIN, "SEQ_BEGIN" }, \
+ { VIRTIO_VSOCK_OP_SEQ_END, "SEQ_END" })

TRACE_EVENT(virtio_transport_alloc_pkt,
TP_PROTO(
@@ -40,7 +47,9 @@ TRACE_EVENT(virtio_transport_alloc_pkt,
__u32 len,
__u16 type,
__u16 op,
- __u32 flags
+ __u32 flags,
+ __u32 msg_len,
+ __u32 msg_id
),
TP_ARGS(
src_cid, src_port,
@@ -48,7 +57,9 @@ TRACE_EVENT(virtio_transport_alloc_pkt,
len,
type,
op,
- flags
+ flags,
+ msg_len,
+ msg_id
),
TP_STRUCT__entry(
__field(__u32, src_cid)
@@ -59,6 +70,8 @@ TRACE_EVENT(virtio_transport_alloc_pkt,
__field(__u16, type)
__field(__u16, op)
__field(__u32, flags)
+ __field(__u32, msg_len)
+ __field(__u32, msg_id)
),
TP_fast_assign(
__entry->src_cid = src_cid;
@@ -69,14 +82,18 @@ TRACE_EVENT(virtio_transport_alloc_pkt,
__entry->type = type;
__entry->op = op;
__entry->flags = flags;
+ __entry->msg_len = msg_len;
+ __entry->msg_id = msg_id;
),
- TP_printk("%u:%u -> %u:%u len=%u type=%s op=%s flags=%#x",
+ TP_printk("%u:%u -> %u:%u len=%u type=%s op=%s flags=%#x msg_len=%u msg_id=%u",
__entry->src_cid, __entry->src_port,
__entry->dst_cid, __entry->dst_port,
__entry->len,
show_type(__entry->type),
show_op(__entry->op),
- __entry->flags)
+ __entry->flags,
+ __entry->msg_len,
+ __entry->msg_id)
);

TRACE_EVENT(virtio_transport_recv_pkt,
@@ -88,7 +105,9 @@ TRACE_EVENT(virtio_transport_recv_pkt,
__u16 op,
__u32 flags,
__u32 buf_alloc,
- __u32 fwd_cnt
+ __u32 fwd_cnt,
+ __u32 msg_len,
+ __u32 msg_id
),
TP_ARGS(
src_cid, src_port,
@@ -98,7 +117,9 @@ TRACE_EVENT(virtio_transport_recv_pkt,
op,
flags,
buf_alloc,
- fwd_cnt
+ fwd_cnt,
+ msg_len,
+ msg_id
),
TP_STRUCT__entry(
__field(__u32, src_cid)
@@ -111,6 +132,8 @@ TRACE_EVENT(virtio_transport_recv_pkt,
__field(__u32, flags)
__field(__u32, buf_alloc)
__field(__u32, fwd_cnt)
+ __field(__u32, msg_len)
+ __field(__u32, msg_id)
),
TP_fast_assign(
__entry->src_cid = src_cid;
@@ -123,9 +146,10 @@ TRACE_EVENT(virtio_transport_recv_pkt,
__entry->flags = flags;
__entry->buf_alloc = buf_alloc;
__entry->fwd_cnt = fwd_cnt;
+ __entry->msg_len = msg_len;
+ __entry->msg_id = msg_id;
),
- TP_printk("%u:%u -> %u:%u len=%u type=%s op=%s flags=%#x "
- "buf_alloc=%u fwd_cnt=%u",
+ TP_printk("%u:%u -> %u:%u len=%u type=%s op=%s flags=%#x buf_alloc=%u fwd_cnt=%u msg_len=%u msg_id=%u",
__entry->src_cid, __entry->src_port,
__entry->dst_cid, __entry->dst_port,
__entry->len,
@@ -133,7 +157,9 @@ TRACE_EVENT(virtio_transport_recv_pkt,
show_op(__entry->op),
__entry->flags,
__entry->buf_alloc,
- __entry->fwd_cnt)
+ __entry->fwd_cnt,
+ __entry->msg_len,
+ __entry->msg_id)
);

#endif /* _TRACE_VSOCK_VIRTIO_TRANSPORT_COMMON_H */
diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
index 01a56c7da8bd..9e27c0c67ee2 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -47,6 +47,8 @@ virtio_transport_alloc_pkt(struct virtio_vsock_pkt_info *info,
{
struct virtio_vsock_pkt *pkt;
int err;
+ u32 msg_len = 0;
+ u32 msg_id = 0;

pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
if (!pkt)
@@ -74,6 +76,14 @@ virtio_transport_alloc_pkt(struct virtio_vsock_pkt_info *info,
err = memcpy_from_msg(pkt->buf, info->msg, len);
if (err)
goto out;
+
+ if (info->op == VIRTIO_VSOCK_OP_SEQ_BEGIN ||
+ info->op == VIRTIO_VSOCK_OP_SEQ_END) {
+ struct virtio_vsock_seq_hdr *seq_hdr = pkt->buf;
+
+ msg_len = le32_to_cpu(seq_hdr->msg_len);
+ msg_id = le32_to_cpu(seq_hdr->msg_id);
+ }
}

trace_virtio_transport_alloc_pkt(src_cid, src_port,
@@ -81,7 +91,7 @@ virtio_transport_alloc_pkt(struct virtio_vsock_pkt_info *info,
len,
info->type,
info->op,
- info->flags);
+ info->flags, msg_len, msg_id);

return pkt;

@@ -1372,12 +1382,22 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,
struct vsock_sock *vsk;
struct sock *sk;
bool space_available;
+ u32 msg_len = 0;
+ u32 msg_id = 0;

vsock_addr_init(&src, le64_to_cpu(pkt->hdr.src_cid),
le32_to_cpu(pkt->hdr.src_port));
vsock_addr_init(&dst, le64_to_cpu(pkt->hdr.dst_cid),
le32_to_cpu(pkt->hdr.dst_port));

+ if (le16_to_cpu(pkt->hdr.op) == VIRTIO_VSOCK_OP_SEQ_BEGIN ||
+ le16_to_cpu(pkt->hdr.op) == VIRTIO_VSOCK_OP_SEQ_END) {
+ struct virtio_vsock_seq_hdr *seq_hdr = pkt->buf;
+
+ msg_len = le32_to_cpu(seq_hdr->msg_len);
+ msg_id = le32_to_cpu(seq_hdr->msg_id);
+ }
+
trace_virtio_transport_recv_pkt(src.svm_cid, src.svm_port,
dst.svm_cid, dst.svm_port,
le32_to_cpu(pkt->hdr.len),
@@ -1385,7 +1405,8 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,
le16_to_cpu(pkt->hdr.op),
le32_to_cpu(pkt->hdr.flags),
le32_to_cpu(pkt->hdr.buf_alloc),
- le32_to_cpu(pkt->hdr.fwd_cnt));
+ le32_to_cpu(pkt->hdr.fwd_cnt),
+ msg_len, msg_id);

if (!virtio_transport_valid_type(le16_to_cpu(pkt->hdr.type))) {
(void)virtio_transport_reset_no_sock(t, pkt);
--
2.25.1

2021-03-23 13:21:02

by Arseny Krasnov

[permalink] [raw]
Subject: [RFC PATCH v7 21/22] vsock_test: add SOCK_SEQPACKET tests

This adds two tests of SOCK_SEQPACKET socket: both transfer data and
then test MSG_EOR and MSG_TRUNC flags. Cases for connect(), bind(),
etc. are not tested, because it is same as for stream socket.

Signed-off-by: Arseny Krasnov <[email protected]>
---
tools/testing/vsock/util.c | 32 ++++++--
tools/testing/vsock/util.h | 3 +
tools/testing/vsock/vsock_test.c | 126 +++++++++++++++++++++++++++++++
3 files changed, 156 insertions(+), 5 deletions(-)

diff --git a/tools/testing/vsock/util.c b/tools/testing/vsock/util.c
index 93cbd6f603f9..2acbb7703c6a 100644
--- a/tools/testing/vsock/util.c
+++ b/tools/testing/vsock/util.c
@@ -84,7 +84,7 @@ void vsock_wait_remote_close(int fd)
}

/* Connect to <cid, port> and return the file descriptor. */
-int vsock_stream_connect(unsigned int cid, unsigned int port)
+static int vsock_connect(unsigned int cid, unsigned int port, int type)
{
union {
struct sockaddr sa;
@@ -101,7 +101,7 @@ int vsock_stream_connect(unsigned int cid, unsigned int port)

control_expectln("LISTENING");

- fd = socket(AF_VSOCK, SOCK_STREAM, 0);
+ fd = socket(AF_VSOCK, type, 0);

timeout_begin(TIMEOUT);
do {
@@ -120,11 +120,21 @@ int vsock_stream_connect(unsigned int cid, unsigned int port)
return fd;
}

+int vsock_stream_connect(unsigned int cid, unsigned int port)
+{
+ return vsock_connect(cid, port, SOCK_STREAM);
+}
+
+int vsock_seqpacket_connect(unsigned int cid, unsigned int port)
+{
+ return vsock_connect(cid, port, SOCK_SEQPACKET);
+}
+
/* Listen on <cid, port> and return the first incoming connection. The remote
* address is stored to clientaddrp. clientaddrp may be NULL.
*/
-int vsock_stream_accept(unsigned int cid, unsigned int port,
- struct sockaddr_vm *clientaddrp)
+static int vsock_accept(unsigned int cid, unsigned int port,
+ struct sockaddr_vm *clientaddrp, int type)
{
union {
struct sockaddr sa;
@@ -145,7 +155,7 @@ int vsock_stream_accept(unsigned int cid, unsigned int port,
int client_fd;
int old_errno;

- fd = socket(AF_VSOCK, SOCK_STREAM, 0);
+ fd = socket(AF_VSOCK, type, 0);

if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) {
perror("bind");
@@ -189,6 +199,18 @@ int vsock_stream_accept(unsigned int cid, unsigned int port,
return client_fd;
}

+int vsock_stream_accept(unsigned int cid, unsigned int port,
+ struct sockaddr_vm *clientaddrp)
+{
+ return vsock_accept(cid, port, clientaddrp, SOCK_STREAM);
+}
+
+int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
+ struct sockaddr_vm *clientaddrp)
+{
+ return vsock_accept(cid, port, clientaddrp, SOCK_SEQPACKET);
+}
+
/* Transmit one byte and check the return value.
*
* expected_ret:
diff --git a/tools/testing/vsock/util.h b/tools/testing/vsock/util.h
index e53dd09d26d9..a3375ad2fb7f 100644
--- a/tools/testing/vsock/util.h
+++ b/tools/testing/vsock/util.h
@@ -36,8 +36,11 @@ struct test_case {
void init_signals(void);
unsigned int parse_cid(const char *str);
int vsock_stream_connect(unsigned int cid, unsigned int port);
+int vsock_seqpacket_connect(unsigned int cid, unsigned int port);
int vsock_stream_accept(unsigned int cid, unsigned int port,
struct sockaddr_vm *clientaddrp);
+int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
+ struct sockaddr_vm *clientaddrp);
void vsock_wait_remote_close(int fd);
void send_byte(int fd, int expected_ret, int flags);
void recv_byte(int fd, int expected_ret, int flags);
diff --git a/tools/testing/vsock/vsock_test.c b/tools/testing/vsock/vsock_test.c
index 5a4fb80fa832..5fca9be5b1dd 100644
--- a/tools/testing/vsock/vsock_test.c
+++ b/tools/testing/vsock/vsock_test.c
@@ -14,6 +14,8 @@
#include <errno.h>
#include <unistd.h>
#include <linux/kernel.h>
+#include <sys/types.h>
+#include <sys/socket.h>

#include "timeout.h"
#include "control.h"
@@ -279,6 +281,120 @@ static void test_stream_msg_peek_server(const struct test_opts *opts)
close(fd);
}

+#define MESSAGES_CNT 7
+#define MESSAGE_EOR_IDX (MESSAGES_CNT / 2)
+static void test_seqpacket_msg_eor_client(const struct test_opts *opts)
+{
+ int fd;
+
+ fd = vsock_seqpacket_connect(opts->peer_cid, 1234);
+ if (fd < 0) {
+ perror("connect");
+ exit(EXIT_FAILURE);
+ }
+
+ /* Send several messages, one with MSG_EOR flag */
+ for (int i = 0; i < MESSAGES_CNT; i++)
+ send_byte(fd, 1, (i != MESSAGE_EOR_IDX) ? 0 : MSG_EOR);
+
+ control_writeln("SENDDONE");
+ close(fd);
+}
+
+static void test_seqpacket_msg_eor_server(const struct test_opts *opts)
+{
+ int fd;
+ char buf[16];
+ struct msghdr msg = {0};
+ struct iovec iov = {0};
+
+ fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL);
+ if (fd < 0) {
+ perror("accept");
+ exit(EXIT_FAILURE);
+ }
+
+ control_expectln("SENDDONE");
+ iov.iov_base = buf;
+ iov.iov_len = sizeof(buf);
+ msg.msg_iov = &iov;
+ msg.msg_iovlen = 1;
+
+ for (int i = 0; i < MESSAGES_CNT; i++) {
+ if (recvmsg(fd, &msg, 0) != 1) {
+ perror("message bound violated");
+ exit(EXIT_FAILURE);
+ }
+
+ if (i == MESSAGE_EOR_IDX) {
+ if (!(msg.msg_flags & MSG_EOR)) {
+ fprintf(stderr, "MSG_EOR flag expected\n");
+ exit(EXIT_FAILURE);
+ }
+ } else {
+ if (msg.msg_flags & MSG_EOR) {
+ fprintf(stderr, "unexpected MSG_EOR flag\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+ }
+
+ close(fd);
+}
+
+#define MESSAGE_TRUNC_SZ 32
+static void test_seqpacket_msg_trunc_client(const struct test_opts *opts)
+{
+ int fd;
+ char buf[MESSAGE_TRUNC_SZ];
+
+ fd = vsock_seqpacket_connect(opts->peer_cid, 1234);
+ if (fd < 0) {
+ perror("connect");
+ exit(EXIT_FAILURE);
+ }
+
+ if (send(fd, buf, sizeof(buf), 0) != sizeof(buf)) {
+ perror("send failed");
+ exit(EXIT_FAILURE);
+ }
+
+ control_writeln("SENDDONE");
+ close(fd);
+}
+
+static void test_seqpacket_msg_trunc_server(const struct test_opts *opts)
+{
+ int fd;
+ char buf[MESSAGE_TRUNC_SZ / 2];
+ struct msghdr msg = {0};
+ struct iovec iov = {0};
+
+ fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL);
+ if (fd < 0) {
+ perror("accept");
+ exit(EXIT_FAILURE);
+ }
+
+ control_expectln("SENDDONE");
+ iov.iov_base = buf;
+ iov.iov_len = sizeof(buf);
+ msg.msg_iov = &iov;
+ msg.msg_iovlen = 1;
+
+ if (recvmsg(fd, &msg, MSG_TRUNC) != MESSAGE_TRUNC_SZ) {
+ perror("MSG_TRUNC doesn't work");
+ exit(EXIT_FAILURE);
+ }
+
+ if (!(msg.msg_flags & MSG_TRUNC)) {
+ fprintf(stderr, "MSG_TRUNC expected\n");
+ exit(EXIT_FAILURE);
+ }
+
+ close(fd);
+}
+
static struct test_case test_cases[] = {
{
.name = "SOCK_STREAM connection reset",
@@ -309,6 +425,16 @@ static struct test_case test_cases[] = {
.run_client = test_stream_msg_peek_client,
.run_server = test_stream_msg_peek_server,
},
+ {
+ .name = "SOCK_SEQPACKET send data MSG_EOR",
+ .run_client = test_seqpacket_msg_eor_client,
+ .run_server = test_seqpacket_msg_eor_server,
+ },
+ {
+ .name = "SOCK_SEQPACKET send data MSG_TRUNC",
+ .run_client = test_seqpacket_msg_trunc_client,
+ .run_server = test_seqpacket_msg_trunc_server,
+ },
{},
};

--
2.25.1

2021-03-25 09:39:40

by Stefano Garzarella

[permalink] [raw]
Subject: Re: [RFC PATCH v7 05/22] af_vsock: separate wait space loop

On Tue, Mar 23, 2021 at 04:10:23PM +0300, Arseny Krasnov wrote:
>This moves loop that waits for space on send to separate function,
>because it will be used for SEQ_BEGIN/SEQ_END sending before and
>after data transmission. Waiting for SEQ_BEGIN/SEQ_END is needed
>because such packets carries SEQPACKET header that couldn't be
>fragmented by credit mechanism, so to avoid it, sender waits until
>enough space will be ready.
>
>Signed-off-by: Arseny Krasnov <[email protected]>
>---
> include/net/af_vsock.h | 2 +
> net/vmw_vsock/af_vsock.c | 99 +++++++++++++++++++++++++---------------
> 2 files changed, 63 insertions(+), 38 deletions(-)

I had already reviewed this one as well and it doesn't seem to have
changed :-)

Reviewed-by: Stefano Garzarella <[email protected]>

>
>diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h
>index 74ac8a4c4168..7232f6c42a36 100644
>--- a/include/net/af_vsock.h
>+++ b/include/net/af_vsock.h
>@@ -204,6 +204,8 @@ void vsock_remove_sock(struct vsock_sock *vsk);
> void vsock_for_each_connected_socket(void (*fn)(struct sock *sk));
> int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk);
> bool vsock_find_cid(unsigned int cid);
>+int vsock_wait_space(struct sock *sk, size_t space, int flags,
>+ struct vsock_transport_send_notify_data *send_data);
>
> /**** TAP ****/
>
>diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
>index fa0c37f97330..617ffe42693d 100644
>--- a/net/vmw_vsock/af_vsock.c
>+++ b/net/vmw_vsock/af_vsock.c
>@@ -1692,6 +1692,65 @@ static int vsock_connectible_getsockopt(struct socket *sock,
> return 0;
> }
>
>+int vsock_wait_space(struct sock *sk, size_t space, int flags,
>+ struct vsock_transport_send_notify_data *send_data)
>+{
>+ const struct vsock_transport *transport;
>+ struct vsock_sock *vsk;
>+ long timeout;
>+ int err;
>+
>+ DEFINE_WAIT_FUNC(wait, woken_wake_function);
>+
>+ vsk = vsock_sk(sk);
>+ transport = vsk->transport;
>+ timeout = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
>+ err = 0;
>+
>+ add_wait_queue(sk_sleep(sk), &wait);
>+
>+ while (vsock_stream_has_space(vsk) < space &&
>+ sk->sk_err == 0 &&
>+ !(sk->sk_shutdown & SEND_SHUTDOWN) &&
>+ !(vsk->peer_shutdown & RCV_SHUTDOWN)) {
>+
>+ /* Don't wait for non-blocking sockets. */
>+ if (timeout == 0) {
>+ err = -EAGAIN;
>+ goto out_err;
>+ }
>+
>+ if (send_data) {
>+ err = transport->notify_send_pre_block(vsk, send_data);
>+ if (err < 0)
>+ goto out_err;
>+ }
>+
>+ release_sock(sk);
>+ timeout = wait_woken(&wait, TASK_INTERRUPTIBLE, timeout);
>+ lock_sock(sk);
>+ if (signal_pending(current)) {
>+ err = sock_intr_errno(timeout);
>+ goto out_err;
>+ } else if (timeout == 0) {
>+ err = -EAGAIN;
>+ goto out_err;
>+ }
>+ }
>+
>+ if (sk->sk_err) {
>+ err = -sk->sk_err;
>+ } else if ((sk->sk_shutdown & SEND_SHUTDOWN) ||
>+ (vsk->peer_shutdown & RCV_SHUTDOWN)) {
>+ err = -EPIPE;
>+ }
>+
>+out_err:
>+ remove_wait_queue(sk_sleep(sk), &wait);
>+ return err;
>+}
>+EXPORT_SYMBOL_GPL(vsock_wait_space);
>+
> static int vsock_connectible_sendmsg(struct socket *sock, struct msghdr *msg,
> size_t len)
> {
>@@ -1699,10 +1758,8 @@ static int vsock_connectible_sendmsg(struct socket *sock, struct msghdr *msg,
> struct vsock_sock *vsk;
> const struct vsock_transport *transport;
> ssize_t total_written;
>- long timeout;
> int err;
> struct vsock_transport_send_notify_data send_data;
>- DEFINE_WAIT_FUNC(wait, woken_wake_function);
>
> sk = sock->sk;
> vsk = vsock_sk(sk);
>@@ -1740,9 +1797,6 @@ static int vsock_connectible_sendmsg(struct socket *sock, struct msghdr *msg,
> goto out;
> }
>
>- /* Wait for room in the produce queue to enqueue our user's data. */
>- timeout = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
>-
> err = transport->notify_send_init(vsk, &send_data);
> if (err < 0)
> goto out;
>@@ -1750,39 +1804,8 @@ static int vsock_connectible_sendmsg(struct socket *sock, struct msghdr *msg,
> while (total_written < len) {
> ssize_t written;
>
>- add_wait_queue(sk_sleep(sk), &wait);
>- while (vsock_stream_has_space(vsk) == 0 &&
>- sk->sk_err == 0 &&
>- !(sk->sk_shutdown & SEND_SHUTDOWN) &&
>- !(vsk->peer_shutdown & RCV_SHUTDOWN)) {
>-
>- /* Don't wait for non-blocking sockets. */
>- if (timeout == 0) {
>- err = -EAGAIN;
>- remove_wait_queue(sk_sleep(sk), &wait);
>- goto out_err;
>- }
>-
>- err = transport->notify_send_pre_block(vsk, &send_data);
>- if (err < 0) {
>- remove_wait_queue(sk_sleep(sk), &wait);
>- goto out_err;
>- }
>-
>- release_sock(sk);
>- timeout = wait_woken(&wait, TASK_INTERRUPTIBLE, timeout);
>- lock_sock(sk);
>- if (signal_pending(current)) {
>- err = sock_intr_errno(timeout);
>- remove_wait_queue(sk_sleep(sk), &wait);
>- goto out_err;
>- } else if (timeout == 0) {
>- err = -EAGAIN;
>- remove_wait_queue(sk_sleep(sk), &wait);
>- goto out_err;
>- }
>- }
>- remove_wait_queue(sk_sleep(sk), &wait);
>+ if (vsock_wait_space(sk, 1, msg->msg_flags, &send_data))
>+ goto out_err;
>
> /* These checks occur both as part of and after the loop
> * conditional since we need to check before and after
>--
>2.25.1
>

2021-03-25 09:45:09

by Stefano Garzarella

[permalink] [raw]
Subject: Re: [RFC PATCH v7 06/22] af_vsock: implement send logic for SEQPACKET

On Tue, Mar 23, 2021 at 04:10:42PM +0300, Arseny Krasnov wrote:
>This adds some logic to current stream enqueue function for SEQPACKET
>support:
>1) Use 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]>
>---
> v6 -> v7:
> 'seqpacket_enqueue' callback interface changed, 'flags' argument was
> removed, because it was 'msg_flags' field of 'msg' argument which is
> already exists.
>
> include/net/af_vsock.h | 2 ++
> net/vmw_vsock/af_vsock.c | 21 +++++++++++++++------
> 2 files changed, 17 insertions(+), 6 deletions(-)


Reviewed-by: Stefano Garzarella <[email protected]>

2021-03-25 09:58:27

by Stefano Garzarella

[permalink] [raw]
Subject: Re: [RFC PATCH v7 11/22] virtio/vsock: dequeue callback for SOCK_SEQPACKET

On Tue, Mar 23, 2021 at 04:12:41PM +0300, Arseny Krasnov wrote:
>This adds transport callback and it's logic for SEQPACKET dequeue.
>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. If during copy SEQ_BEGIN
>was found(and not all data was copied), copying is restarted by reset
>user's iov iterator(previous unfinished data is dropped).
>
>Signed-off-by: Arseny Krasnov <[email protected]>
>---
> v6 -> v7:
> 1) 'struct virtio_vsock_seqpacket_state' now renamed to
> 'struct virtio_vsock_seq_state'.
> 2) Field 'seqpacket_state' of 'struct virtio_vsock_sock' now
> renamed to 'seq_state'.
> 3) Current message length to process('user_read_seq_len') is
> set to 0 on error or message dequeue completed sucecssfully.
>
> include/linux/virtio_vsock.h | 14 +++
> include/uapi/linux/virtio_vsock.h | 16 ++++
> net/vmw_vsock/virtio_transport_common.c | 121 ++++++++++++++++++++++++
> 3 files changed, 151 insertions(+)
>
>diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
>index dc636b727179..0e3aa395c07c 100644
>--- a/include/linux/virtio_vsock.h
>+++ b/include/linux/virtio_vsock.h
>@@ -18,6 +18,12 @@ enum {
> VSOCK_VQ_MAX = 3,
> };
>
>+struct virtio_vsock_seq_state {
>+ u32 user_read_seq_len;
>+ u32 user_read_copied;
>+ u32 curr_rx_msg_id;
>+};
>+
> /* Per-socket state (accessed via vsk->trans) */
> struct virtio_vsock_sock {
> struct vsock_sock *vsk;
>@@ -36,6 +42,8 @@ struct virtio_vsock_sock {
> u32 rx_bytes;
> u32 buf_alloc;
> struct list_head rx_queue;
>+
>+ struct virtio_vsock_seq_state seq_state;
> };
>
> struct virtio_vsock_pkt {
>@@ -80,6 +88,12 @@ virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
> struct msghdr *msg,
> size_t len, int flags);
>
>+int
>+virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
>+ struct msghdr *msg,
>+ int flags,
>+ bool *msg_ready,
>+ size_t *msg_len);
> s64 virtio_transport_stream_has_data(struct vsock_sock *vsk);
> s64 virtio_transport_stream_has_space(struct vsock_sock *vsk);
>
>diff --git a/include/uapi/linux/virtio_vsock.h b/include/uapi/linux/virtio_vsock.h
>index 1d57ed3d84d2..692f8078cced 100644
>--- a/include/uapi/linux/virtio_vsock.h
>+++ b/include/uapi/linux/virtio_vsock.h

Maybe we can move the following changes to this file (that should match
the virtio-spec discussed in the separate thread) in a separate patch
with a reference to the spec.

You can include this in the series before this patch.

>@@ -63,8 +63,14 @@ struct virtio_vsock_hdr {
> __le32 fwd_cnt;
> } __attribute__((packed));
>
>+struct virtio_vsock_seq_hdr {
>+ __le32 msg_id;
>+ __le32 msg_len;
>+} __attribute__((packed));
>+
> enum virtio_vsock_type {
> VIRTIO_VSOCK_TYPE_STREAM = 1,
>+ VIRTIO_VSOCK_TYPE_SEQPACKET = 2,
> };
>
> enum virtio_vsock_op {
>@@ -83,6 +89,11 @@ enum virtio_vsock_op {
> VIRTIO_VSOCK_OP_CREDIT_UPDATE = 6,
> /* Request the peer to send the credit info to us */
> VIRTIO_VSOCK_OP_CREDIT_REQUEST = 7,
>+
>+ /* Record begin for SOCK_SEQPACKET */
>+ VIRTIO_VSOCK_OP_SEQ_BEGIN = 8,
>+ /* Record end for SOCK_SEQPACKET */
>+ VIRTIO_VSOCK_OP_SEQ_END = 9,
> };
>
> /* VIRTIO_VSOCK_OP_SHUTDOWN flags values */
>@@ -91,4 +102,9 @@ enum virtio_vsock_shutdown {
> VIRTIO_VSOCK_SHUTDOWN_SEND = 2,
> };
>
>+/* VIRTIO_VSOCK_OP_RW flags values */
>+enum virtio_vsock_rw {
>+ VIRTIO_VSOCK_RW_EOR = 1,
>+};
>+
> #endif /* _UAPI_LINUX_VIRTIO_VSOCK_H */
>diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
>index 833104b71a1c..a8f4326e45e8 100644
>--- a/net/vmw_vsock/virtio_transport_common.c
>+++ b/net/vmw_vsock/virtio_transport_common.c
>@@ -393,6 +393,114 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
> return err;
> }
>
>+static inline void virtio_transport_remove_pkt(struct virtio_vsock_pkt *pkt)
>+{
>+ list_del(&pkt->list);
>+ virtio_transport_free_pkt(pkt);
>+}
>+
>+static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
>+ struct msghdr *msg,
>+ bool *msg_ready)
>+{
>+ struct virtio_vsock_sock *vvs = vsk->trans;
>+ struct virtio_vsock_pkt *pkt;
>+ int err = 0;
>+ size_t user_buf_len = msg->msg_iter.count;
>+
>+ *msg_ready = false;
>+ spin_lock_bh(&vvs->rx_lock);
>+
>+ while (!*msg_ready && !list_empty(&vvs->rx_queue) && !err) {
>+ pkt = list_first_entry(&vvs->rx_queue, struct virtio_vsock_pkt, list);
>+
>+ switch (le16_to_cpu(pkt->hdr.op)) {
>+ case VIRTIO_VSOCK_OP_SEQ_BEGIN: {
>+ /* Unexpected 'SEQ_BEGIN' during record copy:
>+ * Leave receive loop, 'EAGAIN' will restart it from
>+ * outer receive loop, packet is still in queue and
>+ * counters are cleared. So in next loop enter,
>+ * 'SEQ_BEGIN' will be dequeued first. User's iov
>+ * iterator will be reset in outer loop. Also
>+ * send credit update, because some bytes could be
>+ * copied. User will never see unfinished record.
>+ */
>+ err = -EAGAIN;
>+ break;
>+ }
>+ case VIRTIO_VSOCK_OP_SEQ_END: {
>+ struct virtio_vsock_seq_hdr *seq_hdr;
>+
>+ seq_hdr = (struct virtio_vsock_seq_hdr *)pkt->buf;
>+ /* First check that whole record is received. */
>+
>+ if (vvs->seq_state.user_read_copied !=
>+ vvs->seq_state.user_read_seq_len ||
>+ le32_to_cpu(seq_hdr->msg_id) !=
>+ vvs->seq_state.curr_rx_msg_id) {
>+ /* Tail of current record and head of
>next missed,
>+ * so this EOR is from next record. Restart receive.
>+ * Current record will be dropped, next headless will
>+ * be dropped on next attempt to get record length.
>+ */
>+ err = -EAGAIN;
>+ } else {
>+ /* Success. */
>+ *msg_ready = true;
>+ }
>+
>+ break;
>+ }
>+ case VIRTIO_VSOCK_OP_RW: {
>+ size_t bytes_to_copy;
>+ size_t pkt_len;
>+
>+ pkt_len = (size_t)le32_to_cpu(pkt->hdr.len);
>+ bytes_to_copy = min(user_buf_len, pkt_len);
>+
>+ /* 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)) {
>+ spin_lock_bh(&vvs->rx_lock);
>+ err = -EINVAL;
>+ break;
>+ }
>+
>+ spin_lock_bh(&vvs->rx_lock);
>+ user_buf_len -= bytes_to_copy;
>+ vvs->seq_state.user_read_copied += pkt_len;
>+
>+ if (le32_to_cpu(pkt->hdr.flags) & VIRTIO_VSOCK_RW_EOR)
>+ msg->msg_flags |= MSG_EOR;
>+ break;
>+ }
>+ default:
>+ ;
>+ }
>+
>+ /* For unexpected 'SEQ_BEGIN', keep such packet in queue,
>+ * but drop any other type of packet.
>+ */
>+ if (le16_to_cpu(pkt->hdr.op) != VIRTIO_VSOCK_OP_SEQ_BEGIN) {
>+ virtio_transport_dec_rx_pkt(vvs, pkt);
>+ virtio_transport_remove_pkt(pkt);
>+ }
>+ }
>+
>+ /* Reset current record length on error or whole message received. */
>+ if (*msg_ready || err)
>+ vvs->seq_state.user_read_seq_len = 0;
>+
>+ spin_unlock_bh(&vvs->rx_lock);
>+
>+ virtio_transport_send_credit_update(vsk);
>+
>+ return err;
>+}
>+
> ssize_t
> virtio_transport_stream_dequeue(struct vsock_sock *vsk,
> struct msghdr *msg,
>@@ -405,6 +513,19 @@ virtio_transport_stream_dequeue(struct vsock_sock *vsk,
> }
> EXPORT_SYMBOL_GPL(virtio_transport_stream_dequeue);
>
>+int
>+virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
>+ struct msghdr *msg,
>+ int flags, bool *msg_ready,
>+ size_t *msg_len)
>+{
>+ if (flags & MSG_PEEK)
>+ return -EOPNOTSUPP;
>+

msg_len seems not filled...


>+ return virtio_transport_seqpacket_do_dequeue(vsk, msg,
>msg_ready);
>+}
>+EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_dequeue);
>+
> int
> virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
> struct msghdr *msg,
>--
>2.25.1
>

2021-03-25 10:09:53

by Stefano Garzarella

[permalink] [raw]
Subject: Re: [RFC PATCH v7 12/22] virtio/vsock: fetch length for SEQPACKET record

On Tue, Mar 23, 2021 at 04:12:55PM +0300, Arseny Krasnov wrote:
>This adds transport callback which tries to fetch record begin marker
>from socket's rx queue. It is called from af_vsock.c before reading data
>packets of record.
>
>Signed-off-by: Arseny Krasnov <[email protected]>
>---
> v6 -> v7:
> 1) Now 'virtio_transport_seqpacket_seq_get_len()' returns 0, if rx
> queue of socket is empty. Else it returns length of current message
> to handle.
> 2) If dequeue callback is called, but there is no detected length of
> message to dequeue, EAGAIN is returned, and outer loop restarts
> receiving.
>
> net/vmw_vsock/virtio_transport_common.c | 61 +++++++++++++++++++++++++
> 1 file changed, 61 insertions(+)
>
>diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
>index a8f4326e45e8..41f05034593e 100644
>--- a/net/vmw_vsock/virtio_transport_common.c
>+++ b/net/vmw_vsock/virtio_transport_common.c
>@@ -399,6 +399,62 @@ static inline void virtio_transport_remove_pkt(struct virtio_vsock_pkt *pkt)
> virtio_transport_free_pkt(pkt);
> }
>
>+static size_t virtio_transport_drop_until_seq_begin(struct
>virtio_vsock_sock *vvs)
>+{
>+ struct virtio_vsock_pkt *pkt, *n;
>+ size_t bytes_dropped = 0;
>+
>+ list_for_each_entry_safe(pkt, n, &vvs->rx_queue, list) {
>+ if (le16_to_cpu(pkt->hdr.op) == VIRTIO_VSOCK_OP_SEQ_BEGIN)
>+ break;
>+
>+ bytes_dropped += le32_to_cpu(pkt->hdr.len);
>+ virtio_transport_dec_rx_pkt(vvs, pkt);
>+ virtio_transport_remove_pkt(pkt);
>+ }
>+
>+ return bytes_dropped;
>+}
>+
>+static size_t virtio_transport_seqpacket_seq_get_len(struct vsock_sock *vsk)
>+{
>+ struct virtio_vsock_seq_hdr *seq_hdr;
>+ struct virtio_vsock_sock *vvs;
>+ struct virtio_vsock_pkt *pkt;
>+ size_t bytes_dropped = 0;
>+
>+ vvs = vsk->trans;
>+
>+ spin_lock_bh(&vvs->rx_lock);
>+
>+ /* Have some record to process, return it's length. */
>+ if (vvs->seq_state.user_read_seq_len)
>+ goto out;
>+
>+ /* Fetch all orphaned 'RW' packets and send credit update. */
>+ bytes_dropped = virtio_transport_drop_until_seq_begin(vvs);
>+
>+ if (list_empty(&vvs->rx_queue))
>+ goto out;
>+
>+ pkt = list_first_entry(&vvs->rx_queue, struct virtio_vsock_pkt, list);
>+
>+ vvs->seq_state.user_read_copied = 0;
>+
>+ seq_hdr = (struct virtio_vsock_seq_hdr *)pkt->buf;
>+ vvs->seq_state.user_read_seq_len = le32_to_cpu(seq_hdr->msg_len);
>+ vvs->seq_state.curr_rx_msg_id = le32_to_cpu(seq_hdr->msg_id);
>+ virtio_transport_dec_rx_pkt(vvs, pkt);
>+ virtio_transport_remove_pkt(pkt);
>+out:
>+ spin_unlock_bh(&vvs->rx_lock);
>+
>+ if (bytes_dropped)
>+ virtio_transport_send_credit_update(vsk);
>+
>+ return vvs->seq_state.user_read_seq_len;
>+}
>+
> static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
> struct msghdr *msg,
> bool *msg_ready)
>@@ -522,6 +578,11 @@ virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
> if (flags & MSG_PEEK)
> return -EOPNOTSUPP;
>
>+ *msg_len = virtio_transport_seqpacket_seq_get_len(vsk);
>+
>+ if (*msg_len == 0)
>+ return -EAGAIN;
>+

Okay, I see now, I think you can move this patch before the previous one
or merge them in a single patch, it is better to review and to bisect.

As mentioned, I think we can return msg_len if
virtio_transport_seqpacket_do_dequeue() does not fail, otherwise the
error.

I mean something like this:

static ssize_t virtio_transport_seqpacket_do_dequeue(...)
{
size_t msg_len;
ssize_t ret;

msg_len = virtio_transport_seqpacket_seq_get_len(vsk);
if (msg_len == 0)
return -EAGAIN;

ret = virtio_transport_seqpacket_do_dequeue(vsk, msg, msg_ready);
if (ret < 0)
return ret;

return msg_len;
}

> return virtio_transport_seqpacket_do_dequeue(vsk, msg, msg_ready);
> }
> EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_dequeue);
>-- 2.25.1
>

2021-03-25 10:13:54

by Stefano Garzarella

[permalink] [raw]
Subject: Re: [RFC PATCH v7 13/22] virtio/vsock: add SEQPACKET receive logic

On Tue, Mar 23, 2021 at 04:13:13PM +0300, Arseny Krasnov wrote:
>This modifies current receive logic for SEQPACKET support:
>1) Inserts 'SEQ_BEGIN' packet to socket's rx queue.
>2) Inserts 'RW' packet to socket's rx queue, but without merging with
> buffer of last packet in queue.
>3) Performs check for packet and socket types on receive(if mismatch,
> then reset connection).
>
>Signed-off-by: Arseny Krasnov <[email protected]>
>---
> v6 -> v7:
> In 'virtio_transport_recv_pkt()', 'sock_put()' is added, when type of
> received packet does not match to the type of socket.
>
> net/vmw_vsock/virtio_transport_common.c | 64 +++++++++++++++++--------
> 1 file changed, 45 insertions(+), 19 deletions(-)


Reviewed-by: Stefano Garzarella <[email protected]>

2021-03-25 10:21:21

by Stefano Garzarella

[permalink] [raw]
Subject: Re: [RFC PATCH v7 14/22] virtio/vsock: rest of SOCK_SEQPACKET support

On Tue, Mar 23, 2021 at 04:13:29PM +0300, Arseny Krasnov wrote:
>This adds rest of logic for SEQPACKET:
>1) SEQPACKET specific functions which send SEQ_BEGIN/SEQ_END.
> Note that both functions may sleep to wait enough space for
> SEQPACKET header.
>2) SEQ_BEGIN/SEQ_END in TAP packet capture.
>3) Send SHUTDOWN on socket close for SEQPACKET type.
>4) Set SEQPACKET packet type during send.
>5) Set MSG_EOR in flags for SEQPACKET during send.
>6) 'seqpacket_allow' flag to virtio transport.
>
>Signed-off-by: Arseny Krasnov <[email protected]>
>---
> v6 -> v7:
> In 'virtio_transport_seqpacket_enqueue()', 'next_tx_msg_id' is updated
> in both cases when message send successfully or error occured.
>
> include/linux/virtio_vsock.h | 7 ++
> net/vmw_vsock/virtio_transport_common.c | 88 ++++++++++++++++++++++++-
> 2 files changed, 93 insertions(+), 2 deletions(-)
>
>diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
>index 0e3aa395c07c..ab5f56fd7251 100644
>--- a/include/linux/virtio_vsock.h
>+++ b/include/linux/virtio_vsock.h
>@@ -22,6 +22,7 @@ struct virtio_vsock_seq_state {
> u32 user_read_seq_len;
> u32 user_read_copied;
> u32 curr_rx_msg_id;
>+ u32 next_tx_msg_id;
> };
>
> /* Per-socket state (accessed via vsk->trans) */
>@@ -76,6 +77,8 @@ struct virtio_transport {
>
> /* Takes ownership of the packet */
> int (*send_pkt)(struct virtio_vsock_pkt *pkt);
>+
>+ bool seqpacket_allow;
> };
>
> ssize_t
>@@ -89,6 +92,10 @@ virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
> size_t len, int flags);
>
> int
>+virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk,
>+ struct msghdr *msg,
>+ size_t len);
>+int
> virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
> struct msghdr *msg,
> int flags,
>diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
>index bfe0d7026bf8..01a56c7da8bd 100644
>--- a/net/vmw_vsock/virtio_transport_common.c
>+++ b/net/vmw_vsock/virtio_transport_common.c
>@@ -139,6 +139,8 @@ static struct sk_buff *virtio_transport_build_skb(void *opaque)
> break;
> case VIRTIO_VSOCK_OP_CREDIT_UPDATE:
> case VIRTIO_VSOCK_OP_CREDIT_REQUEST:
>+ case VIRTIO_VSOCK_OP_SEQ_BEGIN:
>+ case VIRTIO_VSOCK_OP_SEQ_END:
> hdr->op = cpu_to_le16(AF_VSOCK_OP_CONTROL);
> break;
> default:
>@@ -187,7 +189,12 @@ 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));
>+
>+ if (info->type == VIRTIO_VSOCK_TYPE_SEQPACKET &&
>+ info->msg &&
>+ info->msg->msg_flags & MSG_EOR)
>+ info->flags |= VIRTIO_VSOCK_RW_EOR;
>
> t_ops = virtio_transport_get_ops(vsk);
> if (unlikely(!t_ops))
>@@ -401,6 +408,43 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
> return err;
> }
>
>+static int virtio_transport_seqpacket_send_ctrl(struct vsock_sock *vsk,
>+ int type,
>+ size_t len,
>+ int flags)
>+{
>+ struct virtio_vsock_sock *vvs = vsk->trans;
>+ struct virtio_vsock_pkt_info info = {
>+ .op = type,
>+ .vsk = vsk,
>+ .pkt_len = sizeof(struct virtio_vsock_seq_hdr)
>+ };
>+
>+ struct virtio_vsock_seq_hdr seq_hdr = {
>+ .msg_id = cpu_to_le32(vvs->seq_state.next_tx_msg_id),
>+ .msg_len = cpu_to_le32(len)
>+ };
>+
>+ struct kvec seq_hdr_kiov = {
>+ .iov_base = (void *)&seq_hdr,
>+ .iov_len = sizeof(struct virtio_vsock_seq_hdr)
>+ };
>+
>+ struct msghdr msg = {0};
>+
>+ //XXX: do we need 'vsock_transport_send_notify_data' pointer?
>+ if (vsock_wait_space(sk_vsock(vsk),
>+ sizeof(struct virtio_vsock_seq_hdr),
>+ flags, NULL))
>+ return -1;
>+
>+ iov_iter_kvec(&msg.msg_iter, WRITE, &seq_hdr_kiov, 1, sizeof(seq_hdr));
>+
>+ info.msg = &msg;
>+
>+ return virtio_transport_send_pkt_info(vsk, &info);
>+}
>+
> static inline void virtio_transport_remove_pkt(struct virtio_vsock_pkt *pkt)
> {
> list_del(&pkt->list);
>@@ -595,6 +639,46 @@ 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)
>+{
>+ int written = -1;
>+
>+ if (msg->msg_iter.iov_offset == 0) {
>+ /* Send SEQBEGIN. */
>+ if (virtio_transport_seqpacket_send_ctrl(vsk,
>+ VIRTIO_VSOCK_OP_SEQ_BEGIN,
>+ len,
>+ msg->msg_flags) < 0)
>+ goto out;
>+ }
>+
>+ written = virtio_transport_stream_enqueue(vsk, msg, len);
>+
>+ if (written < 0)
>+ goto out;
>+
>+ if (msg->msg_iter.count == 0) {
>+ /* Send SEQEND. */
>+ virtio_transport_seqpacket_send_ctrl(vsk,
>+ VIRTIO_VSOCK_OP_SEQ_END,
>+ 0,
>+ msg->msg_flags);

What happen if this fail?

In the previous version we returned -1, now we return the bytes
transmitted, is that right?

The rest LGTM.

>+ }
>+out:
>+ /* Update next id on error or message transmission done. */
>+ if (written < 0 || msg->msg_iter.count == 0) {
>+ struct virtio_vsock_sock *vvs = vsk->trans;
>+
>+ vvs->seq_state.next_tx_msg_id++;
>+ }
>+
>+ return written;
>+}
>+EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_enqueue);
>+
> int
> virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
> struct msghdr *msg,
>@@ -1014,7 +1098,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);
>
> list_for_each_entry_safe(pkt, tmp, &vvs->rx_queue, list) {
>--
>2.25.1
>

2021-03-25 10:28:47

by Stefano Garzarella

[permalink] [raw]
Subject: Re: [RFC PATCH v7 15/22] virtio/vsock: SEQPACKET support feature bit

On Tue, Mar 23, 2021 at 04:13:49PM +0300, Arseny Krasnov wrote:
>This adds new virtio vsock specific feature bit which means
>SOCK_SEQPACKET support. Guest negotiates this bit with vhost,
>thus checking that vhost side supports SEQPACKET.
>
>Signed-off-by: Arseny Krasnov <[email protected]>
>---
> include/uapi/linux/virtio_vsock.h | 3 +++
> 1 file changed, 3 insertions(+)

Since you have this patch, I think you can generalize the title, update
the description, and merge here the changes I mentioned in patch 11/22
about changes of include/uapi/linux/virtio_vsock.h.

So you can have a single patch with the new virtio-spec defines and
structs related to SEQPACKET, of course then we move it before patch 11.

What do you think?

Stefano

>
>diff --git a/include/uapi/linux/virtio_vsock.h b/include/uapi/linux/virtio_vsock.h
>index 692f8078cced..619aaebb355a 100644
>--- a/include/uapi/linux/virtio_vsock.h
>+++ b/include/uapi/linux/virtio_vsock.h
>@@ -38,6 +38,9 @@
> #include <linux/virtio_ids.h>
> #include <linux/virtio_config.h>
>
>+/* The feature bitmap for virtio vsock */
>+#define VIRTIO_VSOCK_F_SEQPACKET 0 /* SOCK_SEQPACKET supported */
>+
> struct virtio_vsock_config {
> __le64 guest_cid;
> } __attribute__((packed));
>--
>2.25.1
>

2021-03-25 10:44:07

by Stefano Garzarella

[permalink] [raw]
Subject: Re: [RFC PATCH v7 16/22] virtio/vsock: setup SEQPACKET ops for transport

On Tue, Mar 23, 2021 at 04:14:03PM +0300, Arseny Krasnov wrote:
>This adds SEQPACKET ops for virtio transport and 'seqpacket_allow()'
>callback.
>
>Signed-off-by: Arseny Krasnov <[email protected]>
>---
> net/vmw_vsock/virtio_transport.c | 12 ++++++++++++
> 1 file changed, 12 insertions(+)

Sorry for not mentioning this in the previous review, but maybe we can
merge this patch with "virtio/vsock: SEQPACKET feature bit support", so
we have a single patch when we fully enable the SEQPACKET support in
this transport.

Anyway, I don't have a strong opinion on that.

What do you think?

Stefano

>
>diff --git a/net/vmw_vsock/virtio_transport.c
>b/net/vmw_vsock/virtio_transport.c
>index 2700a63ab095..83ae2078c847 100644
>--- a/net/vmw_vsock/virtio_transport.c
>+++ b/net/vmw_vsock/virtio_transport.c
>@@ -443,6 +443,8 @@ static void virtio_vsock_rx_done(struct virtqueue *vq)
> queue_work(virtio_vsock_workqueue, &vsock->rx_work);
> }
>
>+static bool virtio_transport_seqpacket_allow(void);
>+
> static struct virtio_transport virtio_transport = {
> .transport = {
> .module = THIS_MODULE,
>@@ -469,6 +471,10 @@ static struct virtio_transport virtio_transport = {
> .stream_is_active = virtio_transport_stream_is_active,
> .stream_allow = virtio_transport_stream_allow,
>
>+ .seqpacket_dequeue = virtio_transport_seqpacket_dequeue,
>+ .seqpacket_enqueue = virtio_transport_seqpacket_enqueue,
>+ .seqpacket_allow = virtio_transport_seqpacket_allow,
>+
> .notify_poll_in = virtio_transport_notify_poll_in,
> .notify_poll_out = virtio_transport_notify_poll_out,
> .notify_recv_init = virtio_transport_notify_recv_init,
>@@ -483,8 +489,14 @@ static struct virtio_transport virtio_transport = {
> },
>
> .send_pkt = virtio_transport_send_pkt,
>+ .seqpacket_allow = false
> };
>
>+static bool virtio_transport_seqpacket_allow(void)
>+{
>+ return virtio_transport.seqpacket_allow;
>+}
>+
> static void virtio_transport_rx_work(struct work_struct *work)
> {
> struct virtio_vsock *vsock =
>-- 2.25.1
>

2021-03-25 10:44:10

by Stefano Garzarella

[permalink] [raw]
Subject: Re: [RFC PATCH v7 17/22] vhost/vsock: setup SEQPACKET ops for transport

On Tue, Mar 23, 2021 at 04:14:18PM +0300, Arseny Krasnov wrote:
>This also removes ignore of non-stream type of packets and adds
>'seqpacket_allow()' callback.
>
>Signed-off-by: Arseny Krasnov <[email protected]>
>---
> drivers/vhost/vsock.c | 15 +++++++++++++--
> 1 file changed, 13 insertions(+), 2 deletions(-)

Same thing for this transporter too, maybe we can merge with the patch
"vhost/vsock: SEQPACKET feature bit support".

Stefano

>
>diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
>index 5e78fb719602..5af141772068 100644
>--- a/drivers/vhost/vsock.c
>+++ b/drivers/vhost/vsock.c
>@@ -354,8 +354,7 @@ vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq,
> return NULL;
> }
>
>- if (le16_to_cpu(pkt->hdr.type) == VIRTIO_VSOCK_TYPE_STREAM)
>- pkt->len = le32_to_cpu(pkt->hdr.len);
>+ pkt->len = le32_to_cpu(pkt->hdr.len);
>
> /* No payload */
> if (!pkt->len)
>@@ -398,6 +397,8 @@ static bool vhost_vsock_more_replies(struct vhost_vsock *vsock)
> return val < vq->num;
> }
>
>+static bool vhost_transport_seqpacket_allow(void);
>+
> static struct virtio_transport vhost_transport = {
> .transport = {
> .module = THIS_MODULE,
>@@ -424,6 +425,10 @@ static struct virtio_transport vhost_transport = {
> .stream_is_active = virtio_transport_stream_is_active,
> .stream_allow = virtio_transport_stream_allow,
>
>+ .seqpacket_dequeue = virtio_transport_seqpacket_dequeue,
>+ .seqpacket_enqueue = virtio_transport_seqpacket_enqueue,
>+ .seqpacket_allow = vhost_transport_seqpacket_allow,
>+
> .notify_poll_in = virtio_transport_notify_poll_in,
> .notify_poll_out = virtio_transport_notify_poll_out,
> .notify_recv_init = virtio_transport_notify_recv_init,
>@@ -439,8 +444,14 @@ static struct virtio_transport vhost_transport = {
> },
>
> .send_pkt = vhost_transport_send_pkt,
>+ .seqpacket_allow = false
> };
>
>+static bool vhost_transport_seqpacket_allow(void)
>+{
>+ return vhost_transport.seqpacket_allow;
>+}
>+
> static void vhost_vsock_handle_tx_kick(struct vhost_work *work)
> {
> struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
>--
>2.25.1
>

2021-03-25 10:50:47

by Stefano Garzarella

[permalink] [raw]
Subject: Re: [RFC PATCH v7 18/22] vsock/loopback: setup SEQPACKET ops for transport

On Tue, Mar 23, 2021 at 04:14:33PM +0300, Arseny Krasnov wrote:
>This adds SEQPACKET ops for loopback transport and 'seqpacket_allow()'
>callback.
>
>Signed-off-by: Arseny Krasnov <[email protected]>
>---
> net/vmw_vsock/vsock_loopback.c | 12 ++++++++++++
> 1 file changed, 12 insertions(+)


Reviewed-by: Stefano Garzarella <[email protected]>

2021-03-25 10:55:15

by Stefano Garzarella

[permalink] [raw]
Subject: Re: [RFC PATCH v7 00/22] virtio/vsock: introduce SOCK_SEQPACKET support

Hi Arseny,

On Tue, Mar 23, 2021 at 04:07:13PM +0300, Arseny Krasnov wrote:
> This patchset implements support of SOCK_SEQPACKET for virtio
>transport.
> As SOCK_SEQPACKET guarantees to save record boundaries, so to
>do it, two new packet operations were added: first for start of record
> and second to mark end of record(SEQ_BEGIN and SEQ_END later). Also,
>both operations carries metadata - to maintain boundaries and payload
>integrity. Metadata is introduced by adding special header with two
>fields - message id and message length:
>
> struct virtio_vsock_seq_hdr {
> __le32 msg_id;
> __le32 msg_len;
> } __attribute__((packed));
>
> This header is transmitted as payload of SEQ_BEGIN and SEQ_END
>packets(buffer of second virtio descriptor in chain) in the same way as
>data transmitted in RW packets. Payload was chosen as buffer for this
>header to avoid touching first virtio buffer which carries header of
>packet, because someone could check that size of this buffer is equal
>to size of packet header. To send record, packet with start marker is
>sent first(it's header carries length of record and id),then all data
>is sent as usual 'RW' packets and finally SEQ_END is sent(it carries
>id of message, which is equal to id of SEQ_BEGIN), also after sending
>SEQ_END id is incremented. On receiver's side,size of record is known
>from packet with start record marker. To check that no packets were
>dropped by transport, 'msg_id's of two sequential SEQ_BEGIN and SEQ_END
>are checked to be equal and length of data between two markers is
>compared to then length in SEQ_BEGIN header.
> Now as packets of one socket are not reordered neither on
>vsock nor on vhost transport layers, such markers allows to restore
>original record on receiver's side. If user's buffer is smaller that
>record 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_EOR' and 'MSG_TRUNC' flags.
> Tests also implemented.
>
> Thanks to [email protected] for encouragements and initial design
>recommendations.
>
> Arseny Krasnov (22):
> 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: separate wait space 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: dequeue callback for SOCK_SEQPACKET
> virtio/vsock: fetch length for SEQPACKET record
> virtio/vsock: add SEQPACKET receive logic
> virtio/vsock: rest of SOCK_SEQPACKET support
> virtio/vsock: SEQPACKET support feature bit
> virtio/vsock: setup SEQPACKET ops for transport
> vhost/vsock: setup SEQPACKET ops for transport
> vsock/loopback: setup SEQPACKET ops for transport
> vhost/vsock: SEQPACKET feature bit support
> virtio/vsock: SEQPACKET feature bit support
> vsock_test: add SOCK_SEQPACKET tests
> virtio/vsock: update trace event for SEQPACKET
>
> drivers/vhost/vsock.c | 21 +-
> include/linux/virtio_vsock.h | 21 +
> include/net/af_vsock.h | 9 +
> .../events/vsock_virtio_transport_common.h | 48 +-
> include/uapi/linux/virtio_vsock.h | 19 +
> net/vmw_vsock/af_vsock.c | 581 +++++++++++------
> net/vmw_vsock/virtio_transport.c | 17 +
> net/vmw_vsock/virtio_transport_common.c | 379 +++++++++--
> net/vmw_vsock/vsock_loopback.c | 12 +
> tools/testing/vsock/util.c | 32 +-
> tools/testing/vsock/util.h | 3 +
> tools/testing/vsock/vsock_test.c | 126 ++++
> 12 files changed, 1015 insertions(+), 253 deletions(-)
>
> 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.

I reviewed the series and I left some comments, I think we are at a good
point, but we should have the specification accepted before merging this
series to avoid having to change the implementation later.

What do you think?

Thanks,
Stefano

2021-03-25 16:04:26

by Arseny Krasnov

[permalink] [raw]
Subject: Re: [RFC PATCH v7 11/22] virtio/vsock: dequeue callback for SOCK_SEQPACKET


On 25.03.2021 12:56, Stefano Garzarella wrote:
> On Tue, Mar 23, 2021 at 04:12:41PM +0300, Arseny Krasnov wrote:
>> This adds transport callback and it's logic for SEQPACKET dequeue.
>> 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. If during copy SEQ_BEGIN
>> was found(and not all data was copied), copying is restarted by reset
>> user's iov iterator(previous unfinished data is dropped).
>>
>> Signed-off-by: Arseny Krasnov <[email protected]>
>> ---
>> v6 -> v7:
>> 1) 'struct virtio_vsock_seqpacket_state' now renamed to
>> 'struct virtio_vsock_seq_state'.
>> 2) Field 'seqpacket_state' of 'struct virtio_vsock_sock' now
>> renamed to 'seq_state'.
>> 3) Current message length to process('user_read_seq_len') is
>> set to 0 on error or message dequeue completed sucecssfully.
>>
>> include/linux/virtio_vsock.h | 14 +++
>> include/uapi/linux/virtio_vsock.h | 16 ++++
>> net/vmw_vsock/virtio_transport_common.c | 121 ++++++++++++++++++++++++
>> 3 files changed, 151 insertions(+)
>>
>> diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
>> index dc636b727179..0e3aa395c07c 100644
>> --- a/include/linux/virtio_vsock.h
>> +++ b/include/linux/virtio_vsock.h
>> @@ -18,6 +18,12 @@ enum {
>> VSOCK_VQ_MAX = 3,
>> };
>>
>> +struct virtio_vsock_seq_state {
>> + u32 user_read_seq_len;
>> + u32 user_read_copied;
>> + u32 curr_rx_msg_id;
>> +};
>> +
>> /* Per-socket state (accessed via vsk->trans) */
>> struct virtio_vsock_sock {
>> struct vsock_sock *vsk;
>> @@ -36,6 +42,8 @@ struct virtio_vsock_sock {
>> u32 rx_bytes;
>> u32 buf_alloc;
>> struct list_head rx_queue;
>> +
>> + struct virtio_vsock_seq_state seq_state;
>> };
>>
>> struct virtio_vsock_pkt {
>> @@ -80,6 +88,12 @@ virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
>> struct msghdr *msg,
>> size_t len, int flags);
>>
>> +int
>> +virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
>> + struct msghdr *msg,
>> + int flags,
>> + bool *msg_ready,
>> + size_t *msg_len);
>> s64 virtio_transport_stream_has_data(struct vsock_sock *vsk);
>> s64 virtio_transport_stream_has_space(struct vsock_sock *vsk);
>>
>> diff --git a/include/uapi/linux/virtio_vsock.h b/include/uapi/linux/virtio_vsock.h
>> index 1d57ed3d84d2..692f8078cced 100644
>> --- a/include/uapi/linux/virtio_vsock.h
>> +++ b/include/uapi/linux/virtio_vsock.h
> Maybe we can move the following changes to this file (that should match
> the virtio-spec discussed in the separate thread) in a separate patch
> with a reference to the spec.
>
> You can include this in the series before this patch.
Ack
>
>> @@ -63,8 +63,14 @@ struct virtio_vsock_hdr {
>> __le32 fwd_cnt;
>> } __attribute__((packed));
>>
>> +struct virtio_vsock_seq_hdr {
>> + __le32 msg_id;
>> + __le32 msg_len;
>> +} __attribute__((packed));
>> +
>> enum virtio_vsock_type {
>> VIRTIO_VSOCK_TYPE_STREAM = 1,
>> + VIRTIO_VSOCK_TYPE_SEQPACKET = 2,
>> };
>>
>> enum virtio_vsock_op {
>> @@ -83,6 +89,11 @@ enum virtio_vsock_op {
>> VIRTIO_VSOCK_OP_CREDIT_UPDATE = 6,
>> /* Request the peer to send the credit info to us */
>> VIRTIO_VSOCK_OP_CREDIT_REQUEST = 7,
>> +
>> + /* Record begin for SOCK_SEQPACKET */
>> + VIRTIO_VSOCK_OP_SEQ_BEGIN = 8,
>> + /* Record end for SOCK_SEQPACKET */
>> + VIRTIO_VSOCK_OP_SEQ_END = 9,
>> };
>>
>> /* VIRTIO_VSOCK_OP_SHUTDOWN flags values */
>> @@ -91,4 +102,9 @@ enum virtio_vsock_shutdown {
>> VIRTIO_VSOCK_SHUTDOWN_SEND = 2,
>> };
>>
>> +/* VIRTIO_VSOCK_OP_RW flags values */
>> +enum virtio_vsock_rw {
>> + VIRTIO_VSOCK_RW_EOR = 1,
>> +};
>> +
>> #endif /* _UAPI_LINUX_VIRTIO_VSOCK_H */
>> diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
>> index 833104b71a1c..a8f4326e45e8 100644
>> --- a/net/vmw_vsock/virtio_transport_common.c
>> +++ b/net/vmw_vsock/virtio_transport_common.c
>> @@ -393,6 +393,114 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
>> return err;
>> }
>>
>> +static inline void virtio_transport_remove_pkt(struct virtio_vsock_pkt *pkt)
>> +{
>> + list_del(&pkt->list);
>> + virtio_transport_free_pkt(pkt);
>> +}
>> +
>> +static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
>> + struct msghdr *msg,
>> + bool *msg_ready)
>> +{
>> + struct virtio_vsock_sock *vvs = vsk->trans;
>> + struct virtio_vsock_pkt *pkt;
>> + int err = 0;
>> + size_t user_buf_len = msg->msg_iter.count;
>> +
>> + *msg_ready = false;
>> + spin_lock_bh(&vvs->rx_lock);
>> +
>> + while (!*msg_ready && !list_empty(&vvs->rx_queue) && !err) {
>> + pkt = list_first_entry(&vvs->rx_queue, struct virtio_vsock_pkt, list);
>> +
>> + switch (le16_to_cpu(pkt->hdr.op)) {
>> + case VIRTIO_VSOCK_OP_SEQ_BEGIN: {
>> + /* Unexpected 'SEQ_BEGIN' during record copy:
>> + * Leave receive loop, 'EAGAIN' will restart it from
>> + * outer receive loop, packet is still in queue and
>> + * counters are cleared. So in next loop enter,
>> + * 'SEQ_BEGIN' will be dequeued first. User's iov
>> + * iterator will be reset in outer loop. Also
>> + * send credit update, because some bytes could be
>> + * copied. User will never see unfinished record.
>> + */
>> + err = -EAGAIN;
>> + break;
>> + }
>> + case VIRTIO_VSOCK_OP_SEQ_END: {
>> + struct virtio_vsock_seq_hdr *seq_hdr;
>> +
>> + seq_hdr = (struct virtio_vsock_seq_hdr *)pkt->buf;
>> + /* First check that whole record is received. */
>> +
>> + if (vvs->seq_state.user_read_copied !=
>> + vvs->seq_state.user_read_seq_len ||
>> + le32_to_cpu(seq_hdr->msg_id) !=
>> + vvs->seq_state.curr_rx_msg_id) {
>> + /* Tail of current record and head of
>> next missed,
>> + * so this EOR is from next record. Restart receive.
>> + * Current record will be dropped, next headless will
>> + * be dropped on next attempt to get record length.
>> + */
>> + err = -EAGAIN;
>> + } else {
>> + /* Success. */
>> + *msg_ready = true;
>> + }
>> +
>> + break;
>> + }
>> + case VIRTIO_VSOCK_OP_RW: {
>> + size_t bytes_to_copy;
>> + size_t pkt_len;
>> +
>> + pkt_len = (size_t)le32_to_cpu(pkt->hdr.len);
>> + bytes_to_copy = min(user_buf_len, pkt_len);
>> +
>> + /* 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)) {
>> + spin_lock_bh(&vvs->rx_lock);
>> + err = -EINVAL;
>> + break;
>> + }
>> +
>> + spin_lock_bh(&vvs->rx_lock);
>> + user_buf_len -= bytes_to_copy;
>> + vvs->seq_state.user_read_copied += pkt_len;
>> +
>> + if (le32_to_cpu(pkt->hdr.flags) & VIRTIO_VSOCK_RW_EOR)
>> + msg->msg_flags |= MSG_EOR;
>> + break;
>> + }
>> + default:
>> + ;
>> + }
>> +
>> + /* For unexpected 'SEQ_BEGIN', keep such packet in queue,
>> + * but drop any other type of packet.
>> + */
>> + if (le16_to_cpu(pkt->hdr.op) != VIRTIO_VSOCK_OP_SEQ_BEGIN) {
>> + virtio_transport_dec_rx_pkt(vvs, pkt);
>> + virtio_transport_remove_pkt(pkt);
>> + }
>> + }
>> +
>> + /* Reset current record length on error or whole message received. */
>> + if (*msg_ready || err)
>> + vvs->seq_state.user_read_seq_len = 0;
>> +
>> + spin_unlock_bh(&vvs->rx_lock);
>> +
>> + virtio_transport_send_credit_update(vsk);
>> +
>> + return err;
>> +}
>> +
>> ssize_t
>> virtio_transport_stream_dequeue(struct vsock_sock *vsk,
>> struct msghdr *msg,
>> @@ -405,6 +513,19 @@ virtio_transport_stream_dequeue(struct vsock_sock *vsk,
>> }
>> EXPORT_SYMBOL_GPL(virtio_transport_stream_dequeue);
>>
>> +int
>> +virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
>> + struct msghdr *msg,
>> + int flags, bool *msg_ready,
>> + size_t *msg_len)
>> +{
>> + if (flags & MSG_PEEK)
>> + return -EOPNOTSUPP;
>> +
> msg_len seems not filled...
>
>
>> + return virtio_transport_seqpacket_do_dequeue(vsk, msg,
>> msg_ready);
>> +}
>> +EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_dequeue);
>> +
>> int
>> virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
>> struct msghdr *msg,
>> --
>> 2.25.1
>>
>

2021-03-25 16:05:42

by Arseny Krasnov

[permalink] [raw]
Subject: Re: [RFC PATCH v7 14/22] virtio/vsock: rest of SOCK_SEQPACKET support


On 25.03.2021 13:18, Stefano Garzarella wrote:
> On Tue, Mar 23, 2021 at 04:13:29PM +0300, Arseny Krasnov wrote:
>> This adds rest of logic for SEQPACKET:
>> 1) SEQPACKET specific functions which send SEQ_BEGIN/SEQ_END.
>> Note that both functions may sleep to wait enough space for
>> SEQPACKET header.
>> 2) SEQ_BEGIN/SEQ_END in TAP packet capture.
>> 3) Send SHUTDOWN on socket close for SEQPACKET type.
>> 4) Set SEQPACKET packet type during send.
>> 5) Set MSG_EOR in flags for SEQPACKET during send.
>> 6) 'seqpacket_allow' flag to virtio transport.
>>
>> Signed-off-by: Arseny Krasnov <[email protected]>
>> ---
>> v6 -> v7:
>> In 'virtio_transport_seqpacket_enqueue()', 'next_tx_msg_id' is updated
>> in both cases when message send successfully or error occured.
>>
>> include/linux/virtio_vsock.h | 7 ++
>> net/vmw_vsock/virtio_transport_common.c | 88 ++++++++++++++++++++++++-
>> 2 files changed, 93 insertions(+), 2 deletions(-)
>>
>> diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
>> index 0e3aa395c07c..ab5f56fd7251 100644
>> --- a/include/linux/virtio_vsock.h
>> +++ b/include/linux/virtio_vsock.h
>> @@ -22,6 +22,7 @@ struct virtio_vsock_seq_state {
>> u32 user_read_seq_len;
>> u32 user_read_copied;
>> u32 curr_rx_msg_id;
>> + u32 next_tx_msg_id;
>> };
>>
>> /* Per-socket state (accessed via vsk->trans) */
>> @@ -76,6 +77,8 @@ struct virtio_transport {
>>
>> /* Takes ownership of the packet */
>> int (*send_pkt)(struct virtio_vsock_pkt *pkt);
>> +
>> + bool seqpacket_allow;
>> };
>>
>> ssize_t
>> @@ -89,6 +92,10 @@ virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
>> size_t len, int flags);
>>
>> int
>> +virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk,
>> + struct msghdr *msg,
>> + size_t len);
>> +int
>> virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
>> struct msghdr *msg,
>> int flags,
>> diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
>> index bfe0d7026bf8..01a56c7da8bd 100644
>> --- a/net/vmw_vsock/virtio_transport_common.c
>> +++ b/net/vmw_vsock/virtio_transport_common.c
>> @@ -139,6 +139,8 @@ static struct sk_buff *virtio_transport_build_skb(void *opaque)
>> break;
>> case VIRTIO_VSOCK_OP_CREDIT_UPDATE:
>> case VIRTIO_VSOCK_OP_CREDIT_REQUEST:
>> + case VIRTIO_VSOCK_OP_SEQ_BEGIN:
>> + case VIRTIO_VSOCK_OP_SEQ_END:
>> hdr->op = cpu_to_le16(AF_VSOCK_OP_CONTROL);
>> break;
>> default:
>> @@ -187,7 +189,12 @@ 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));
>> +
>> + if (info->type == VIRTIO_VSOCK_TYPE_SEQPACKET &&
>> + info->msg &&
>> + info->msg->msg_flags & MSG_EOR)
>> + info->flags |= VIRTIO_VSOCK_RW_EOR;
>>
>> t_ops = virtio_transport_get_ops(vsk);
>> if (unlikely(!t_ops))
>> @@ -401,6 +408,43 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
>> return err;
>> }
>>
>> +static int virtio_transport_seqpacket_send_ctrl(struct vsock_sock *vsk,
>> + int type,
>> + size_t len,
>> + int flags)
>> +{
>> + struct virtio_vsock_sock *vvs = vsk->trans;
>> + struct virtio_vsock_pkt_info info = {
>> + .op = type,
>> + .vsk = vsk,
>> + .pkt_len = sizeof(struct virtio_vsock_seq_hdr)
>> + };
>> +
>> + struct virtio_vsock_seq_hdr seq_hdr = {
>> + .msg_id = cpu_to_le32(vvs->seq_state.next_tx_msg_id),
>> + .msg_len = cpu_to_le32(len)
>> + };
>> +
>> + struct kvec seq_hdr_kiov = {
>> + .iov_base = (void *)&seq_hdr,
>> + .iov_len = sizeof(struct virtio_vsock_seq_hdr)
>> + };
>> +
>> + struct msghdr msg = {0};
>> +
>> + //XXX: do we need 'vsock_transport_send_notify_data' pointer?
>> + if (vsock_wait_space(sk_vsock(vsk),
>> + sizeof(struct virtio_vsock_seq_hdr),
>> + flags, NULL))
>> + return -1;
>> +
>> + iov_iter_kvec(&msg.msg_iter, WRITE, &seq_hdr_kiov, 1, sizeof(seq_hdr));
>> +
>> + info.msg = &msg;
>> +
>> + return virtio_transport_send_pkt_info(vsk, &info);
>> +}
>> +
>> static inline void virtio_transport_remove_pkt(struct virtio_vsock_pkt *pkt)
>> {
>> list_del(&pkt->list);
>> @@ -595,6 +639,46 @@ 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)
>> +{
>> + int written = -1;
>> +
>> + if (msg->msg_iter.iov_offset == 0) {
>> + /* Send SEQBEGIN. */
>> + if (virtio_transport_seqpacket_send_ctrl(vsk,
>> + VIRTIO_VSOCK_OP_SEQ_BEGIN,
>> + len,
>> + msg->msg_flags) < 0)
>> + goto out;
>> + }
>> +
>> + written = virtio_transport_stream_enqueue(vsk, msg, len);
>> +
>> + if (written < 0)
>> + goto out;
>> +
>> + if (msg->msg_iter.count == 0) {
>> + /* Send SEQEND. */
>> + virtio_transport_seqpacket_send_ctrl(vsk,
>> + VIRTIO_VSOCK_OP_SEQ_END,
>> + 0,
>> + msg->msg_flags);
> What happen if this fail?
>
> In the previous version we returned -1, now we return the bytes
> transmitted, is that right?
Ack, i'll fix it
>
> The rest LGTM.
>
>> + }
>> +out:
>> + /* Update next id on error or message transmission done. */
>> + if (written < 0 || msg->msg_iter.count == 0) {
>> + struct virtio_vsock_sock *vvs = vsk->trans;
>> +
>> + vvs->seq_state.next_tx_msg_id++;
>> + }
>> +
>> + return written;
>> +}
>> +EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_enqueue);
>> +
>> int
>> virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
>> struct msghdr *msg,
>> @@ -1014,7 +1098,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);
>>
>> list_for_each_entry_safe(pkt, tmp, &vvs->rx_queue, list) {
>> --
>> 2.25.1
>>
>

2021-03-25 16:06:05

by Arseny Krasnov

[permalink] [raw]
Subject: Re: [RFC PATCH v7 15/22] virtio/vsock: SEQPACKET support feature bit


On 25.03.2021 13:26, Stefano Garzarella wrote:
> On Tue, Mar 23, 2021 at 04:13:49PM +0300, Arseny Krasnov wrote:
>> This adds new virtio vsock specific feature bit which means
>> SOCK_SEQPACKET support. Guest negotiates this bit with vhost,
>> thus checking that vhost side supports SEQPACKET.
>>
>> Signed-off-by: Arseny Krasnov <[email protected]>
>> ---
>> include/uapi/linux/virtio_vsock.h | 3 +++
>> 1 file changed, 3 insertions(+)
> Since you have this patch, I think you can generalize the title, update
> the description, and merge here the changes I mentioned in patch 11/22
> about changes of include/uapi/linux/virtio_vsock.h.
>
> So you can have a single patch with the new virtio-spec defines and
> structs related to SEQPACKET, of course then we move it before patch 11.
>
> What do you think?
Ok, i'll move all changes related to spec to separate patch
>
> Stefano
>
>> diff --git a/include/uapi/linux/virtio_vsock.h b/include/uapi/linux/virtio_vsock.h
>> index 692f8078cced..619aaebb355a 100644
>> --- a/include/uapi/linux/virtio_vsock.h
>> +++ b/include/uapi/linux/virtio_vsock.h
>> @@ -38,6 +38,9 @@
>> #include <linux/virtio_ids.h>
>> #include <linux/virtio_config.h>
>>
>> +/* The feature bitmap for virtio vsock */
>> +#define VIRTIO_VSOCK_F_SEQPACKET 0 /* SOCK_SEQPACKET supported */
>> +
>> struct virtio_vsock_config {
>> __le64 guest_cid;
>> } __attribute__((packed));
>> --
>> 2.25.1
>>
>

2021-03-25 16:06:34

by Arseny Krasnov

[permalink] [raw]
Subject: Re: [RFC PATCH v7 16/22] virtio/vsock: setup SEQPACKET ops for transport


On 25.03.2021 13:39, Stefano Garzarella wrote:
> On Tue, Mar 23, 2021 at 04:14:03PM +0300, Arseny Krasnov wrote:
>> This adds SEQPACKET ops for virtio transport and 'seqpacket_allow()'
>> callback.
>>
>> Signed-off-by: Arseny Krasnov <[email protected]>
>> ---
>> net/vmw_vsock/virtio_transport.c | 12 ++++++++++++
>> 1 file changed, 12 insertions(+)
> Sorry for not mentioning this in the previous review, but maybe we can
> merge this patch with "virtio/vsock: SEQPACKET feature bit support", so
> we have a single patch when we fully enable the SEQPACKET support in
> this transport.
>
> Anyway, I don't have a strong opinion on that.
>
> What do you think?
Ok, no problem
>
> Stefano
>
>> diff --git a/net/vmw_vsock/virtio_transport.c
>> b/net/vmw_vsock/virtio_transport.c
>> index 2700a63ab095..83ae2078c847 100644
>> --- a/net/vmw_vsock/virtio_transport.c
>> +++ b/net/vmw_vsock/virtio_transport.c
>> @@ -443,6 +443,8 @@ static void virtio_vsock_rx_done(struct virtqueue *vq)
>> queue_work(virtio_vsock_workqueue, &vsock->rx_work);
>> }
>>
>> +static bool virtio_transport_seqpacket_allow(void);
>> +
>> static struct virtio_transport virtio_transport = {
>> .transport = {
>> .module = THIS_MODULE,
>> @@ -469,6 +471,10 @@ static struct virtio_transport virtio_transport = {
>> .stream_is_active = virtio_transport_stream_is_active,
>> .stream_allow = virtio_transport_stream_allow,
>>
>> + .seqpacket_dequeue = virtio_transport_seqpacket_dequeue,
>> + .seqpacket_enqueue = virtio_transport_seqpacket_enqueue,
>> + .seqpacket_allow = virtio_transport_seqpacket_allow,
>> +
>> .notify_poll_in = virtio_transport_notify_poll_in,
>> .notify_poll_out = virtio_transport_notify_poll_out,
>> .notify_recv_init = virtio_transport_notify_recv_init,
>> @@ -483,8 +489,14 @@ static struct virtio_transport virtio_transport = {
>> },
>>
>> .send_pkt = virtio_transport_send_pkt,
>> + .seqpacket_allow = false
>> };
>>
>> +static bool virtio_transport_seqpacket_allow(void)
>> +{
>> + return virtio_transport.seqpacket_allow;
>> +}
>> +
>> static void virtio_transport_rx_work(struct work_struct *work)
>> {
>> struct virtio_vsock *vsock =
>> -- 2.25.1
>>
>

2021-03-25 16:06:41

by Arseny Krasnov

[permalink] [raw]
Subject: Re: [RFC PATCH v7 17/22] vhost/vsock: setup SEQPACKET ops for transport


On 25.03.2021 13:42, Stefano Garzarella wrote:
> On Tue, Mar 23, 2021 at 04:14:18PM +0300, Arseny Krasnov wrote:
>> This also removes ignore of non-stream type of packets and adds
>> 'seqpacket_allow()' callback.
>>
>> Signed-off-by: Arseny Krasnov <[email protected]>
>> ---
>> drivers/vhost/vsock.c | 15 +++++++++++++--
>> 1 file changed, 13 insertions(+), 2 deletions(-)
> Same thing for this transporter too, maybe we can merge with the patch
> "vhost/vsock: SEQPACKET feature bit support".
Ok, no problem
>
> Stefano
>
>> diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
>> index 5e78fb719602..5af141772068 100644
>> --- a/drivers/vhost/vsock.c
>> +++ b/drivers/vhost/vsock.c
>> @@ -354,8 +354,7 @@ vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq,
>> return NULL;
>> }
>>
>> - if (le16_to_cpu(pkt->hdr.type) == VIRTIO_VSOCK_TYPE_STREAM)
>> - pkt->len = le32_to_cpu(pkt->hdr.len);
>> + pkt->len = le32_to_cpu(pkt->hdr.len);
>>
>> /* No payload */
>> if (!pkt->len)
>> @@ -398,6 +397,8 @@ static bool vhost_vsock_more_replies(struct vhost_vsock *vsock)
>> return val < vq->num;
>> }
>>
>> +static bool vhost_transport_seqpacket_allow(void);
>> +
>> static struct virtio_transport vhost_transport = {
>> .transport = {
>> .module = THIS_MODULE,
>> @@ -424,6 +425,10 @@ static struct virtio_transport vhost_transport = {
>> .stream_is_active = virtio_transport_stream_is_active,
>> .stream_allow = virtio_transport_stream_allow,
>>
>> + .seqpacket_dequeue = virtio_transport_seqpacket_dequeue,
>> + .seqpacket_enqueue = virtio_transport_seqpacket_enqueue,
>> + .seqpacket_allow = vhost_transport_seqpacket_allow,
>> +
>> .notify_poll_in = virtio_transport_notify_poll_in,
>> .notify_poll_out = virtio_transport_notify_poll_out,
>> .notify_recv_init = virtio_transport_notify_recv_init,
>> @@ -439,8 +444,14 @@ static struct virtio_transport vhost_transport = {
>> },
>>
>> .send_pkt = vhost_transport_send_pkt,
>> + .seqpacket_allow = false
>> };
>>
>> +static bool vhost_transport_seqpacket_allow(void)
>> +{
>> + return vhost_transport.seqpacket_allow;
>> +}
>> +
>> static void vhost_vsock_handle_tx_kick(struct vhost_work *work)
>> {
>> struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
>> --
>> 2.25.1
>>
>

2021-03-25 16:06:43

by Arseny Krasnov

[permalink] [raw]
Subject: Re: [RFC PATCH v7 12/22] virtio/vsock: fetch length for SEQPACKET record


On 25.03.2021 13:08, Stefano Garzarella wrote:
> On Tue, Mar 23, 2021 at 04:12:55PM +0300, Arseny Krasnov wrote:
>> This adds transport callback which tries to fetch record begin marker
> >from socket's rx queue. It is called from af_vsock.c before reading data
>> packets of record.
>>
>> Signed-off-by: Arseny Krasnov <[email protected]>
>> ---
>> v6 -> v7:
>> 1) Now 'virtio_transport_seqpacket_seq_get_len()' returns 0, if rx
>> queue of socket is empty. Else it returns length of current message
>> to handle.
>> 2) If dequeue callback is called, but there is no detected length of
>> message to dequeue, EAGAIN is returned, and outer loop restarts
>> receiving.
>>
>> net/vmw_vsock/virtio_transport_common.c | 61 +++++++++++++++++++++++++
>> 1 file changed, 61 insertions(+)
>>
>> diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
>> index a8f4326e45e8..41f05034593e 100644
>> --- a/net/vmw_vsock/virtio_transport_common.c
>> +++ b/net/vmw_vsock/virtio_transport_common.c
>> @@ -399,6 +399,62 @@ static inline void virtio_transport_remove_pkt(struct virtio_vsock_pkt *pkt)
>> virtio_transport_free_pkt(pkt);
>> }
>>
>> +static size_t virtio_transport_drop_until_seq_begin(struct
>> virtio_vsock_sock *vvs)
>> +{
>> + struct virtio_vsock_pkt *pkt, *n;
>> + size_t bytes_dropped = 0;
>> +
>> + list_for_each_entry_safe(pkt, n, &vvs->rx_queue, list) {
>> + if (le16_to_cpu(pkt->hdr.op) == VIRTIO_VSOCK_OP_SEQ_BEGIN)
>> + break;
>> +
>> + bytes_dropped += le32_to_cpu(pkt->hdr.len);
>> + virtio_transport_dec_rx_pkt(vvs, pkt);
>> + virtio_transport_remove_pkt(pkt);
>> + }
>> +
>> + return bytes_dropped;
>> +}
>> +
>> +static size_t virtio_transport_seqpacket_seq_get_len(struct vsock_sock *vsk)
>> +{
>> + struct virtio_vsock_seq_hdr *seq_hdr;
>> + struct virtio_vsock_sock *vvs;
>> + struct virtio_vsock_pkt *pkt;
>> + size_t bytes_dropped = 0;
>> +
>> + vvs = vsk->trans;
>> +
>> + spin_lock_bh(&vvs->rx_lock);
>> +
>> + /* Have some record to process, return it's length. */
>> + if (vvs->seq_state.user_read_seq_len)
>> + goto out;
>> +
>> + /* Fetch all orphaned 'RW' packets and send credit update. */
>> + bytes_dropped = virtio_transport_drop_until_seq_begin(vvs);
>> +
>> + if (list_empty(&vvs->rx_queue))
>> + goto out;
>> +
>> + pkt = list_first_entry(&vvs->rx_queue, struct virtio_vsock_pkt, list);
>> +
>> + vvs->seq_state.user_read_copied = 0;
>> +
>> + seq_hdr = (struct virtio_vsock_seq_hdr *)pkt->buf;
>> + vvs->seq_state.user_read_seq_len = le32_to_cpu(seq_hdr->msg_len);
>> + vvs->seq_state.curr_rx_msg_id = le32_to_cpu(seq_hdr->msg_id);
>> + virtio_transport_dec_rx_pkt(vvs, pkt);
>> + virtio_transport_remove_pkt(pkt);
>> +out:
>> + spin_unlock_bh(&vvs->rx_lock);
>> +
>> + if (bytes_dropped)
>> + virtio_transport_send_credit_update(vsk);
>> +
>> + return vvs->seq_state.user_read_seq_len;
>> +}
>> +
>> static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
>> struct msghdr *msg,
>> bool *msg_ready)
>> @@ -522,6 +578,11 @@ virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
>> if (flags & MSG_PEEK)
>> return -EOPNOTSUPP;
>>
>> + *msg_len = virtio_transport_seqpacket_seq_get_len(vsk);
>> +
>> + if (*msg_len == 0)
>> + return -EAGAIN;
>> +
> Okay, I see now, I think you can move this patch before the previous one
> or merge them in a single patch, it is better to review and to bisect.
>
> As mentioned, I think we can return msg_len if
> virtio_transport_seqpacket_do_dequeue() does not fail, otherwise the
> error.
>
> I mean something like this:
>
> static ssize_t virtio_transport_seqpacket_do_dequeue(...)
> {
> size_t msg_len;
> ssize_t ret;
>
> msg_len = virtio_transport_seqpacket_seq_get_len(vsk);
> if (msg_len == 0)
> return -EAGAIN;
>
> ret = virtio_transport_seqpacket_do_dequeue(vsk, msg, msg_ready);
> if (ret < 0)
> return ret;
>
> return msg_len;
> }
Ack
>
>> return virtio_transport_seqpacket_do_dequeue(vsk, msg, msg_ready);
>> }
>> EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_dequeue);
>> -- 2.25.1
>>
>

2021-03-25 16:09:31

by Arseny Krasnov

[permalink] [raw]
Subject: Re: [RFC PATCH v7 00/22] virtio/vsock: introduce SOCK_SEQPACKET support


On 25.03.2021 13:52, Stefano Garzarella wrote:
> Hi Arseny,
>
> On Tue, Mar 23, 2021 at 04:07:13PM +0300, Arseny Krasnov wrote:
>> This patchset implements support of SOCK_SEQPACKET for virtio
>> transport.
>> As SOCK_SEQPACKET guarantees to save record boundaries, so to
>> do it, two new packet operations were added: first for start of record
>> and second to mark end of record(SEQ_BEGIN and SEQ_END later). Also,
>> both operations carries metadata - to maintain boundaries and payload
>> integrity. Metadata is introduced by adding special header with two
>> fields - message id and message length:
>>
>> struct virtio_vsock_seq_hdr {
>> __le32 msg_id;
>> __le32 msg_len;
>> } __attribute__((packed));
>>
>> This header is transmitted as payload of SEQ_BEGIN and SEQ_END
>> packets(buffer of second virtio descriptor in chain) in the same way as
>> data transmitted in RW packets. Payload was chosen as buffer for this
>> header to avoid touching first virtio buffer which carries header of
>> packet, because someone could check that size of this buffer is equal
>> to size of packet header. To send record, packet with start marker is
>> sent first(it's header carries length of record and id),then all data
>> is sent as usual 'RW' packets and finally SEQ_END is sent(it carries
>> id of message, which is equal to id of SEQ_BEGIN), also after sending
>> SEQ_END id is incremented. On receiver's side,size of record is known
> >from packet with start record marker. To check that no packets were
>> dropped by transport, 'msg_id's of two sequential SEQ_BEGIN and SEQ_END
>> are checked to be equal and length of data between two markers is
>> compared to then length in SEQ_BEGIN header.
>> Now as packets of one socket are not reordered neither on
>> vsock nor on vhost transport layers, such markers allows to restore
>> original record on receiver's side. If user's buffer is smaller that
>> record 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_EOR' and 'MSG_TRUNC' flags.
>> Tests also implemented.
>>
>> Thanks to [email protected] for encouragements and initial design
>> recommendations.
>>
>> Arseny Krasnov (22):
>> 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: separate wait space 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: dequeue callback for SOCK_SEQPACKET
>> virtio/vsock: fetch length for SEQPACKET record
>> virtio/vsock: add SEQPACKET receive logic
>> virtio/vsock: rest of SOCK_SEQPACKET support
>> virtio/vsock: SEQPACKET support feature bit
>> virtio/vsock: setup SEQPACKET ops for transport
>> vhost/vsock: setup SEQPACKET ops for transport
>> vsock/loopback: setup SEQPACKET ops for transport
>> vhost/vsock: SEQPACKET feature bit support
>> virtio/vsock: SEQPACKET feature bit support
>> vsock_test: add SOCK_SEQPACKET tests
>> virtio/vsock: update trace event for SEQPACKET
>>
>> drivers/vhost/vsock.c | 21 +-
>> include/linux/virtio_vsock.h | 21 +
>> include/net/af_vsock.h | 9 +
>> .../events/vsock_virtio_transport_common.h | 48 +-
>> include/uapi/linux/virtio_vsock.h | 19 +
>> net/vmw_vsock/af_vsock.c | 581 +++++++++++------
>> net/vmw_vsock/virtio_transport.c | 17 +
>> net/vmw_vsock/virtio_transport_common.c | 379 +++++++++--
>> net/vmw_vsock/vsock_loopback.c | 12 +
>> tools/testing/vsock/util.c | 32 +-
>> tools/testing/vsock/util.h | 3 +
>> tools/testing/vsock/vsock_test.c | 126 ++++
>> 12 files changed, 1015 insertions(+), 253 deletions(-)
>>
>> 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.
> I reviewed the series and I left some comments, I think we are at a good
> point, but we should have the specification accepted before merging this
> series to avoid having to change the implementation later.
I'll prepare new version of specification patch
>
> What do you think?
Agree, Thank You
>
> Thanks,
> Stefano
>
>