This patchset impelements 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 count and message length:
struct virtio_vsock_seq_hdr {
__le32 msg_cnt;
__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 contains length of record and counter), then
counter is incremented and all data is sent as usual 'RW' packets and
finally SEQ_END is sent(it also carries counter of message, which is
counter of SEQ_BEGIN + 1), also after sedning SEQ_END counter is
incremented again. On receiver's side, length of record is known from
packet with start record marker. To check that no packets were dropped
by transport, counters of two sequential SEQ_BEGIN and SEQ_END are
checked(counter of SEQ_END must be bigger that counter of SEQ_BEGIN by
1) and length of data between two markers is compared to 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.
Arseny Krasnov (17):
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: 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: setup SEQPACKET ops for transport
vhost/vsock: setup SEQPACKET ops for transport
vsock_test: add SOCK_SEQPACKET tests
loopback/vsock: setup SEQPACKET ops for transport
virtio/vsock: simplify credit update function API
drivers/vhost/vsock.c | 8 +-
include/linux/virtio_vsock.h | 15 +
include/net/af_vsock.h | 9 +
include/uapi/linux/virtio_vsock.h | 16 +
net/vmw_vsock/af_vsock.c | 588 +++++++++++++++-------
net/vmw_vsock/virtio_transport.c | 5 +
net/vmw_vsock/virtio_transport_common.c | 316 ++++++++++--
net/vmw_vsock/vsock_loopback.c | 5 +
tools/testing/vsock/util.c | 32 +-
tools/testing/vsock/util.h | 3 +
tools/testing/vsock/vsock_test.c | 126 +++++
11 files changed, 895 insertions(+), 228 deletions(-)
TODO:
- What to do, when server doesn't support SOCK_SEQPACKET. In current
implementation RST is replied in the same way when listening port
is not found. I think that current RST is enough,because case when
server doesn't support SEQ_PACKET is same when listener missed(e.g.
no listener in both cases).
v3 -> v4:
- callbacks for loopback transport
- 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.
--
2.25.1
This prepares af_vsock.c for SEQPACKET support: some functions such
as setsockopt(), getsockopt(), connect(), recvmsg(), sendmsg() are
shared between both types of sockets, so rename them in general
manner.
Signed-off-by: Arseny Krasnov <redacted>
---
net/vmw_vsock/af_vsock.c | 64 +++++++++++++++++++++-------------------
1 file changed, 34 insertions(+), 30 deletions(-)
This moves wait loop for data to dedicated function, because later
it will be used by SEQPACKET data receive loop.
Signed-off-by: Arseny Krasnov <redacted>
---
net/vmw_vsock/af_vsock.c | 158 +++++++++++++++++++++------------------
1 file changed, 86 insertions(+), 72 deletions(-)
This moves STREAM specific data receive logic to dedicated function:
'__vsock_stream_recvmsg()', while checks that will be same for both
types of socket are in shared function: 'vsock_connectible_recvmsg()'.
Signed-off-by: Arseny Krasnov <redacted>
---
net/vmw_vsock/af_vsock.c | 117 +++++++++++++++++++++++----------------
1 file changed, 68 insertions(+), 49 deletions(-)
@@ -1898,65 +1898,22 @@ static int vsock_wait_data(struct sock *sk, struct wait_queue_entry *wait,returnerr;}-staticint-vsock_connectible_recvmsg(structsocket*sock,structmsghdr*msg,size_tlen,-intflags)+staticint__vsock_stream_recvmsg(structsock*sk,structmsghdr*msg,+size_tlen,intflags){-structsock*sk;-structvsock_sock*vsk;+structvsock_transport_recv_notify_datarecv_data;conststructvsock_transport*transport;-interr;-size_ttarget;+structvsock_sock*vsk;ssize_tcopied;+size_ttarget;longtimeout;-structvsock_transport_recv_notify_datarecv_data;+interr;DEFINE_WAIT(wait);-sk=sock->sk;vsk=vsock_sk(sk);-err=0;--lock_sock(sk);-transport=vsk->transport;-if(!transport||sk->sk_state!=TCP_ESTABLISHED){-/* Recvmsg is supposed to return 0 if a peer performs an-*orderlyshutdown.Differentiatebetweenthatcaseandwhena-*peerhasnotconnectedoralocalshutdownoccuredwiththe-*SOCK_DONEflag.-*/-if(sock_flag(sk,SOCK_DONE))-err=0;-else-err=-ENOTCONN;--gotoout;-}--if(flags&MSG_OOB){-err=-EOPNOTSUPP;-gotoout;-}--/* We don't check peer_shutdown flag here since peer may actually shut-*down,buttherecanbedatainthequeuethatalocalsocketcan-*receive.-*/-if(sk->sk_shutdown&RCV_SHUTDOWN){-err=0;-gotoout;-}--/* It is valid on Linux to pass in a zero-length receive buffer. This-*isnotanerror.Wemayaswellbailoutnow.-*/-if(!len){-err=0;-gotoout;-}-/* We must not copy less than target bytes into the user's buffer*beforereturningsuccessfully,sowewaitfortheconsumequeueto*havethatmuchdatatoconsumebeforedequeueing.Notethatthis
@@ -2020,6 +1977,68 @@ vsock_connectible_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,returnerr;}+staticint+vsock_connectible_recvmsg(structsocket*sock,structmsghdr*msg,size_tlen,+intflags)+{+structsock*sk;+structvsock_sock*vsk;+conststructvsock_transport*transport;+interr;++DEFINE_WAIT(wait);++sk=sock->sk;+vsk=vsock_sk(sk);+err=0;++lock_sock(sk);++transport=vsk->transport;++if(!transport||sk->sk_state!=TCP_ESTABLISHED){+/* Recvmsg is supposed to return 0 if a peer performs an+*orderlyshutdown.Differentiatebetweenthatcaseandwhena+*peerhasnotconnectedoralocalshutdownoccurredwiththe+*SOCK_DONEflag.+*/+if(sock_flag(sk,SOCK_DONE))+err=0;+else+err=-ENOTCONN;++gotoout;+}++if(flags&MSG_OOB){+err=-EOPNOTSUPP;+gotoout;+}++/* We don't check peer_shutdown flag here since peer may actually shut+*down,buttherecanbedatainthequeuethatalocalsocketcan+*receive.+*/+if(sk->sk_shutdown&RCV_SHUTDOWN){+err=0;+gotoout;+}++/* It is valid on Linux to pass in a zero-length receive buffer. This+*isnotanerror.Wemayaswellbailoutnow.+*/+if(!len){+err=0;+gotoout;+}++err=__vsock_stream_recvmsg(sk,msg,len,flags);++out:+release_sock(sk);+returnerr;+}+staticconststructproto_opsvsock_stream_ops={.family=PF_VSOCK,.owner=THIS_MODULE,
This adds receive loop for SEQPACKET. It looks like receive loop for
STREAM, but there is a little bit difference:
1) It doesn't call notify callbacks.
2) It doesn't care about 'SO_SNDLOWAT' and 'SO_RCVLOWAT' values, because
there is no sense for these values in SEQPACKET case.
3) It waits until whole record is received or error is found during
receiving.
4) It processes and sets 'MSG_TRUNC' flag.
So to avoid extra conditions for two types of socket inside one loop, two
independent functions were created.
Signed-off-by: Arseny Krasnov <redacted>
---
include/net/af_vsock.h | 5 +++
net/vmw_vsock/af_vsock.c | 96 +++++++++++++++++++++++++++++++++++++++-
2 files changed, 100 insertions(+), 1 deletion(-)
@@ -1977,6 +1977,97 @@ static int __vsock_stream_recvmsg(struct sock *sk, struct msghdr *msg,returnerr;}+staticint__vsock_seqpacket_recvmsg(structsock*sk,structmsghdr*msg,+size_tlen,intflags)+{+conststructvsock_transport*transport;+conststructiovec*orig_iov;+unsignedlongorig_nr_segs;+boolmsg_ready;+structvsock_sock*vsk;+size_trecord_len;+longtimeout;+interr=0;+DEFINE_WAIT(wait);++vsk=vsock_sk(sk);+transport=vsk->transport;++timeout=sock_rcvtimeo(sk,flags&MSG_DONTWAIT);+orig_nr_segs=msg->msg_iter.nr_segs;+orig_iov=msg->msg_iter.iov;+msg_ready=false;+record_len=0;++while(1){+err=vsock_wait_data(sk,&wait,timeout,NULL,0);++if(err<=0){+/* In case of any loop break(timeout, signal+*interruptorshutdown),wereportuserthat+*nothingwascopied.+*/+err=0;+break;+}++if(record_len==0){+record_len=+transport->seqpacket_seq_get_len(vsk);++if(record_len==0)+continue;+}++err=transport->seqpacket_dequeue(vsk,msg,+flags,&msg_ready);+if(err<0){+if(err==-EAGAIN){+iov_iter_init(&msg->msg_iter,READ,+orig_iov,orig_nr_segs,+len);+/* Clear 'MSG_EOR' here, because dequeue+*callbackabovesetitagainifitwas+*setbysender.This'MSG_EOR'isfrom+*droppedrecord.+*/+msg->msg_flags&=~MSG_EOR;+record_len=0;+continue;+}++err=-ENOMEM;+break;+}++if(msg_ready)+break;+}++if(sk->sk_err)+err=-sk->sk_err;+elseif(sk->sk_shutdown&RCV_SHUTDOWN)+err=0;++if(msg_ready){+/* User sets MSG_TRUNC, so return real length of+*packet.+*/+if(flags&MSG_TRUNC)+err=record_len;+else+err=len-msg->msg_iter.count;++/* Always set MSG_TRUNC if real length of packet is+*biggerthanuser'sbuffer.+*/+if(record_len>len)+msg->msg_flags|=MSG_TRUNC;+}++returnerr;+}+staticintvsock_connectible_recvmsg(structsocket*sock,structmsghdr*msg,size_tlen,intflags)
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 <redacted>
---
include/net/af_vsock.h | 2 +
net/vmw_vsock/af_vsock.c | 93 ++++++++++++++++++++++++++--------------
2 files changed, 62 insertions(+), 33 deletions(-)
@@ -1693,6 +1693,64 @@ static int vsock_connectible_getsockopt(struct socket *sock,return0;}+intvsock_wait_space(structsock*sk,size_tspace,intflags,+structvsock_transport_send_notify_data*send_data)+{+conststructvsock_transport*transport;+structvsock_sock*vsk;+longtimeout;+interr;++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;+gotoout_err;+}++if(send_data){+err=transport->notify_send_pre_block(vsk,send_data);+if(err<0)+gotoout_err;+}++release_sock(sk);+timeout=wait_woken(&wait,TASK_INTERRUPTIBLE,timeout);+lock_sock(sk);+if(signal_pending(current)){+err=sock_intr_errno(timeout);+gotoout_err;+}elseif(timeout==0){+err=-EAGAIN;+gotoout_err;+}+}++if(sk->sk_err){+err=-sk->sk_err;+}elseif((sk->sk_shutdown&SEND_SHUTDOWN)||+(vsk->peer_shutdown&RCV_SHUTDOWN)){+err=-EPIPE;+}++out_err:+remove_wait_queue(sk_sleep(sk),&wait);+returnerr;+}+EXPORT_SYMBOL_GPL(vsock_wait_space);+staticintvsock_connectible_sendmsg(structsocket*sock,structmsghdr*msg,size_tlen){
@@ -1751,39 +1809,8 @@ static int vsock_connectible_sendmsg(struct socket *sock, struct msghdr *msg,while(total_written<len){ssize_twritten;-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);-gotoout_err;-}--err=transport->notify_send_pre_block(vsk,&send_data);-if(err<0){-remove_wait_queue(sk_sleep(sk),&wait);-gotoout_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);-gotoout_err;-}elseif(timeout==0){-err=-EAGAIN;-remove_wait_queue(sk_sleep(sk),&wait);-gotoout_err;-}-}-remove_wait_queue(sk_sleep(sk),&wait);+if(vsock_wait_space(sk,1,msg->msg_flags,&send_data))+gotoout_err;/* These checks occur both as part of and after the loop*conditionalsinceweneedtocheckbeforeandafter
This adds some logic to current stream enqueue function for SEQPACKET
support:
1) Send record's begin/end marker.
2) Return value from enqueue function is whole record length or error
for SOCK_SEQPACKET.
Signed-off-by: Arseny Krasnov <redacted>
---
include/net/af_vsock.h | 2 ++
net/vmw_vsock/af_vsock.c | 22 ++++++++++++++++++++--
2 files changed, 22 insertions(+), 2 deletions(-)
@@ -1852,9 +1858,21 @@ static int vsock_connectible_sendmsg(struct socket *sock, struct msghdr *msg,}+if(sk->sk_type==SOCK_SEQPACKET){+err=transport->seqpacket_seq_send_eor(vsk,msg->msg_flags);+if(err<0)+gotoout;+}+out_err:-if(total_written>0)-err=total_written;+if(total_written>0){+/* Return number of written bytes only if:+*1)SOCK_STREAMsocket.+*2)SOCK_SEQPACKETsocketwhenwholebufferissent.+*/+if(sk->sk_type==SOCK_STREAM||total_written==len)+err=total_written;+}out:release_sock(sk);returnerr;
This replaces 'stream' to 'connect oriented' in comments as SEQPACKET is
also connect oriented.
Signed-off-by: Arseny Krasnov <redacted>
---
net/vmw_vsock/af_vsock.c | 31 +++++++++++++++++--------------
1 file changed, 17 insertions(+), 14 deletions(-)
@@ -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:forstreamsocketthismustbecalledwhenvsk->remote_addrisset-*(e.g.duringtheconnect()orwhenaconnectionrequestonalistener+*Note:forconnectorientedsocketthismustbecalledwhenvsk->remote_addr+*isset(e.g.duringtheconnect()orwhenaconnectionrequestonalistener*socketisreceived).*Thevsk->remote_addrisusedtodecidewhichtransporttouse:*-remoteCID==VMADDR_CID_LOCALorg2h->local_cidorVMADDR_CID_HOSTif
@@ -479,10 +479,10 @@ int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk)return0;/* transport->release() must be called with sock lock acquired.-*Thispathcanonlybetakenduringvsock_stream_connect(),-*wherewehavealreadyheldthesocklock.-*Intheothercases,thisfunctioniscalledonanewsocket-*whichisnotassignedtoanytransport.+*Thispathcanonlybetakenduringvsock_connect(),wherewe+*havealreadyheldthesocklock.Intheothercases,this+*functioniscalledonanewsocketwhichisnotassignedto+*anytransport.*/vsk->transport->release(vsk);vsock_deassign_transport(vsk);
@@ -659,9 +659,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-*tableforeasylookupbyitsaddress.Theunboundlistissimplyan-*extraentryattheendofthehashtable,atrickusedbyAF_UNIX.+/* Remove connect oriented sockets from the unbound list and add them+*tothehashtableforeasylookupbyitsaddress.Theunboundlist+*issimplyanextraentryattheendofthehashtable,atrickused+*byAF_UNIX.*/__vsock_remove_bound(vsk);__vsock_insert_bound(vsock_bound_sockets(&vsk->local_addr),vsk);
@@ -952,10 +953,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.IfitisaDGRAMsocketthenwemustfirstkickthe-*socketsothatitwakesupfromanysleepingcalls,forexample-*recv(),andthenafterwardsreturntheerror.+/* If this is a connect oriented socket and it is not connected then+*bailoutimmediately.IfitisaDGRAMsocketthenwemustfirst+*kickthesocketsothatitwakesupfromanysleepingcalls,for+*examplerecv(),andthenafterwardsreturntheerror.*/sk=sock->sk;
@@ -1786,7 +1787,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 connect oriented+*sockets.+*/if(msg->msg_namelen){err=sk->sk_state==TCP_ESTABLISHED?-EISCONN:-EOPNOTSUPP;gotoout;
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 <redacted>
---
include/linux/virtio_vsock.h | 5 +
include/uapi/linux/virtio_vsock.h | 16 ++++
net/vmw_vsock/virtio_transport_common.c | 120 ++++++++++++++++++++++++
3 files changed, 141 insertions(+)
@@ -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 */
@@ -397,6 +397,126 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,returnerr;}+staticinlinevoidvirtio_transport_remove_pkt(structvirtio_vsock_pkt*pkt)+{+list_del(&pkt->list);+virtio_transport_free_pkt(pkt);+}++staticsize_tvirtio_transport_drop_until_seq_begin(structvirtio_vsock_sock*vvs)+{+structvirtio_vsock_pkt*pkt,*n;+size_tbytes_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);+}++returnbytes_dropped;+}++staticintvirtio_transport_seqpacket_do_dequeue(structvsock_sock*vsk,+structmsghdr*msg,+bool*msg_ready)+{+structvirtio_vsock_sock*vvs=vsk->trans;+structvirtio_vsock_pkt*pkt;+interr=0;+size_tuser_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,structvirtio_vsock_pkt,list);++switch(le16_to_cpu(pkt->hdr.op)){+caseVIRTIO_VSOCK_OP_SEQ_BEGIN:{+/* Unexpected 'SEQ_BEGIN' during record copy:+*Leavereceiveloop,'EAGAIN'willrestartitfrom+*outerreceiveloop,packetisstillinqueueand+*countersarecleared.Soinnextloopenter,+*'SEQ_BEGIN'willbedequeuedfirst.User'siov+*iteratorwillberesetinouterloop.Also+*sendcreditupdate,becausesomebytescouldbe+*copied.Userwillneverseeunfinishedrecord.+*/+err=-EAGAIN;+break;+}+caseVIRTIO_VSOCK_OP_SEQ_END:{+structvirtio_vsock_seq_hdr*seq_hdr;++seq_hdr=(structvirtio_vsock_seq_hdr*)pkt->buf;+/* First check that whole record is received. */++if(vvs->user_read_copied!=vvs->user_read_seq_len||+(le32_to_cpu(seq_hdr->msg_cnt)-vvs->curr_rx_msg_cnt)!=1){+/* Tail of current record and head of next missed,+*sothisEORisfromnextrecord.Restartreceive.+*Currentrecordwillbedropped,nextheadlesswill+*bedroppedonnextattempttogetrecordlength.+*/+err=-EAGAIN;+}else{+/* Success. */+*msg_ready=true;+}++break;+}+caseVIRTIO_VSOCK_OP_RW:{+size_tbytes_to_copy;+size_tpkt_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.+*Unlockrx_locksincememcpy_to_msg()maysleep.+*/+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->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,+*butdropanyothertypeofpacket.+*/+if(le16_to_cpu(pkt->hdr.op)!=VIRTIO_VSOCK_OP_SEQ_BEGIN){+virtio_transport_dec_rx_pkt(vvs,pkt);+virtio_transport_remove_pkt(pkt);+}+}++spin_unlock_bh(&vvs->rx_lock);++virtio_transport_send_credit_update(vsk,VIRTIO_VSOCK_TYPE_SEQPACKET,+NULL);++returnerr;+}+ssize_tvirtio_transport_stream_dequeue(structvsock_sock*vsk,structmsghdr*msg,
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 <redacted>
---
net/vmw_vsock/virtio_transport_common.c | 63 +++++++++++++++++--------
1 file changed, 44 insertions(+), 19 deletions(-)
@@ -1062,25 +1070,27 @@ virtio_transport_recv_enqueue(struct vsock_sock *vsk,gotoout;}-/* Try to copy small packets into the buffer of last packet queued,-*toavoidwastingmemoryqueueingtheentirebufferwithasmall-*payload.-*/-if(pkt->len<=GOOD_COPY_LEN&&!list_empty(&vvs->rx_queue)){-structvirtio_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,+*toavoidwastingmemoryqueueingtheentirebufferwithasmall+*payload.+*/+if(pkt->len<=GOOD_COPY_LEN&&!list_empty(&vvs->rx_queue)){+structvirtio_vsock_pkt*last_pkt;-last_pkt=list_last_entry(&vvs->rx_queue,-structvirtio_vsock_pkt,list);+last_pkt=list_last_entry(&vvs->rx_queue,+structvirtio_vsock_pkt,list);-/* If there is space in the last packet queued, we copy the-*newpacketinitsbuffer.-*/-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;-gotoout;+/* If there is space in the last packet queued, we copy the+*newpacketinitsbuffer.+*/+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;+gotoout;+}}}
@@ -1246,6 +1260,12 @@ virtio_transport_recv_listen(struct sock *sk, struct virtio_vsock_pkt *pkt,return0;}+staticboolvirtio_transport_valid_type(u16type)+{+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.*/
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 <redacted>
---
include/linux/virtio_vsock.h | 1 +
net/vmw_vsock/virtio_transport_common.c | 40 +++++++++++++++++++++++++
2 files changed, 41 insertions(+)
This adds rest of logic for SEQPACKET:
1) Packet's type is now set in 'virtio_send_pkt_info()' using
type of socket.
2) SEQPACKET specific functions which send SEQ_BEGIN/SEQ_END.
Note that both functions may sleep to wait enough space for
SEQPACKET header.
3) SEQ_BEGIN/SEQ_END to TAP packet capture.
4) Send SHUTDOWN on socket close for SEQPACKET type.
Signed-off-by: Arseny Krasnov <redacted>
---
include/linux/virtio_vsock.h | 9 +++
net/vmw_vsock/virtio_transport_common.c | 99 +++++++++++++++++++++----
2 files changed, 95 insertions(+), 13 deletions(-)
@@ -165,6 +167,14 @@ void virtio_transport_deliver_tap_pkt(struct virtio_vsock_pkt *pkt)}EXPORT_SYMBOL_GPL(virtio_transport_deliver_tap_pkt);+staticu16virtio_transport_get_type(structsock*sk)+{+if(sk->sk_type==SOCK_STREAM)+returnVIRTIO_VSOCK_TYPE_STREAM;+else+returnVIRTIO_VSOCK_TYPE_SEQPACKET;+}+/* This function can only be used on connecting/connected sockets,*sinceasocketassignedtoatransportisrequired.*
@@ -179,6 +189,13 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,structvirtio_vsock_pkt*pkt;u32pkt_len=info->pkt_len;+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))return-EFAULT;
@@ -397,13 +414,61 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,returnerr;}-staticu16virtio_transport_get_type(structsock*sk)+staticintvirtio_transport_seqpacket_send_ctrl(structvsock_sock*vsk,+inttype,+size_tlen,+intflags){-if(sk->sk_type==SOCK_STREAM)-returnVIRTIO_VSOCK_TYPE_STREAM;-else-returnVIRTIO_VSOCK_TYPE_SEQPACKET;+structvirtio_vsock_sock*vvs=vsk->trans;+structvirtio_vsock_pkt_infoinfo={+.op=type,+.vsk=vsk,+.pkt_len=sizeof(structvirtio_vsock_seq_hdr)+};++structvirtio_vsock_seq_hdrseq_hdr={+.msg_cnt=vvs->next_tx_msg_cnt,+.msg_len=len+};++structkvecseq_hdr_kiov={+.iov_base=(void*)&seq_hdr,+.iov_len=sizeof(structvirtio_vsock_seq_hdr)+};++structmsghdrmsg={0};++//XXX: do we need 'vsock_transport_send_notify_data' pointer?+if(vsock_wait_space(sk_vsock(vsk),+sizeof(structvirtio_vsock_seq_hdr),+flags,NULL))+return-1;++iov_iter_kvec(&msg.msg_iter,WRITE,&seq_hdr_kiov,1,sizeof(seq_hdr));++info.msg=&msg;+vvs->next_tx_msg_cnt++;++returnvirtio_transport_send_pkt_info(vsk,&info);+}++intvirtio_transport_seqpacket_seq_send_len(structvsock_sock*vsk,size_tlen,intflags)+{+returnvirtio_transport_seqpacket_send_ctrl(vsk,+VIRTIO_VSOCK_OP_SEQ_BEGIN,+len,+flags);}+EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_seq_send_len);++intvirtio_transport_seqpacket_seq_send_eor(structvsock_sock*vsk,intflags)+{+returnvirtio_transport_seqpacket_send_ctrl(vsk,+VIRTIO_VSOCK_OP_SEQ_END,+0,+flags);+}+EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_seq_send_eor);staticinlinevoidvirtio_transport_remove_pkt(structvirtio_vsock_pkt*pkt){
@@ -792,7 +870,6 @@ int virtio_transport_connect(struct vsock_sock *vsk){structvirtio_vsock_pkt_infoinfo={.op=VIRTIO_VSOCK_OP_REQUEST,-.type=VIRTIO_VSOCK_TYPE_STREAM,.vsk=vsk,};
@@ -804,7 +881,6 @@ int virtio_transport_shutdown(struct vsock_sock *vsk, int mode){structvirtio_vsock_pkt_infoinfo={.op=VIRTIO_VSOCK_OP_SHUTDOWN,-.type=VIRTIO_VSOCK_TYPE_STREAM,.flags=(mode&RCV_SHUTDOWN?VIRTIO_VSOCK_SHUTDOWN_RCV:0)|(mode&SEND_SHUTDOWN?
This also removes ignore of non-stream type of packets.
Signed-off-by: Arseny Krasnov <redacted>
---
drivers/vhost/vsock.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
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 <redacted>
---
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(-)
@@ -84,7 +84,7 @@ void vsock_wait_remote_close(int fd)}/* Connect to <cid, port> and return the file descriptor. */-intvsock_stream_connect(unsignedintcid,unsignedintport)+staticintvsock_connect(unsignedintcid,unsignedintport,inttype){union{structsockaddrsa;
@@ -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)returnfd;}+intvsock_stream_connect(unsignedintcid,unsignedintport)+{+returnvsock_connect(cid,port,SOCK_STREAM);+}++intvsock_seqpacket_connect(unsignedintcid,unsignedintport)+{+returnvsock_connect(cid,port,SOCK_SEQPACKET);+}+/* Listen on <cid, port> and return the first incoming connection. The remote*addressisstoredtoclientaddrp.clientaddrpmaybeNULL.*/-intvsock_stream_accept(unsignedintcid,unsignedintport,-structsockaddr_vm*clientaddrp)+staticintvsock_accept(unsignedintcid,unsignedintport,+structsockaddr_vm*clientaddrp,inttype){union{structsockaddrsa;
@@ -145,7 +155,7 @@ int vsock_stream_accept(unsigned int cid, unsigned int port,intclient_fd;intold_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,returnclient_fd;}+intvsock_stream_accept(unsignedintcid,unsignedintport,+structsockaddr_vm*clientaddrp)+{+returnvsock_accept(cid,port,clientaddrp,SOCK_STREAM);+}++intvsock_seqpacket_accept(unsignedintcid,unsignedintport,+structsockaddr_vm*clientaddrp)+{+returnvsock_accept(cid,port,clientaddrp,SOCK_SEQPACKET);+}+/* Transmit one byte and check the return value.**expected_ret:
'virtio_transport_send_credit_update()' has some extra args:
1) 'type' may be set in 'virtio_transport_send_pkt_info()' using type
of socket.
2) This function is static and 'hdr' arg was always NULL.
Signed-off-by: Arseny Krasnov <redacted>
---
net/vmw_vsock/virtio_transport_common.c | 20 +++++---------------
1 file changed, 5 insertions(+), 15 deletions(-)
From: "Michael S. Tsirkin" <mst@redhat.com> Date: 2021-02-07 16:22:23
On Sun, Feb 07, 2021 at 06:12:56PM +0300, Arseny Krasnov wrote:
This patchset impelements 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 count and message length:
struct virtio_vsock_seq_hdr {
__le32 msg_cnt;
__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 contains length of record and counter), then
counter is incremented and all data is sent as usual 'RW' packets and
finally SEQ_END is sent(it also carries counter of message, which is
counter of SEQ_BEGIN + 1), also after sedning SEQ_END counter is
incremented again. On receiver's side, length of record is known from
packet with start record marker. To check that no packets were dropped
by transport, counters of two sequential SEQ_BEGIN and SEQ_END are
checked(counter of SEQ_END must be bigger that counter of SEQ_BEGIN by
1) and length of data between two markers is compared to 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.
Arseny Krasnov (17):
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: 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: setup SEQPACKET ops for transport
vhost/vsock: setup SEQPACKET ops for transport
vsock_test: add SOCK_SEQPACKET tests
loopback/vsock: setup SEQPACKET ops for transport
virtio/vsock: simplify credit update function API
drivers/vhost/vsock.c | 8 +-
include/linux/virtio_vsock.h | 15 +
include/net/af_vsock.h | 9 +
include/uapi/linux/virtio_vsock.h | 16 +
net/vmw_vsock/af_vsock.c | 588 +++++++++++++++-------
net/vmw_vsock/virtio_transport.c | 5 +
net/vmw_vsock/virtio_transport_common.c | 316 ++++++++++--
net/vmw_vsock/vsock_loopback.c | 5 +
tools/testing/vsock/util.c | 32 +-
tools/testing/vsock/util.h | 3 +
tools/testing/vsock/vsock_test.c | 126 +++++
11 files changed, 895 insertions(+), 228 deletions(-)
TODO:
- What to do, when server doesn't support SOCK_SEQPACKET. In current
implementation RST is replied in the same way when listening port
is not found. I think that current RST is enough,because case when
server doesn't support SEQ_PACKET is same when listener missed(e.g.
no listener in both cases).
- virtio spec patch
v3 -> v4:
- callbacks for loopback transport
- 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.
--
2.25.1
On Sun, Feb 07, 2021 at 06:12:56PM +0300, Arseny Krasnov wrote:
quoted
This patchset impelements 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 count and message length:
struct virtio_vsock_seq_hdr {
__le32 msg_cnt;
__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 contains length of record and counter), then
counter is incremented and all data is sent as usual 'RW' packets and
finally SEQ_END is sent(it also carries counter of message, which is
counter of SEQ_BEGIN + 1), also after sedning SEQ_END counter is
incremented again. On receiver's side, length of record is known from
packet with start record marker. To check that no packets were dropped
by transport, counters of two sequential SEQ_BEGIN and SEQ_END are
checked(counter of SEQ_END must be bigger that counter of SEQ_BEGIN by
1) and length of data between two markers is compared to 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.
Arseny Krasnov (17):
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: 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: setup SEQPACKET ops for transport
vhost/vsock: setup SEQPACKET ops for transport
vsock_test: add SOCK_SEQPACKET tests
loopback/vsock: setup SEQPACKET ops for transport
virtio/vsock: simplify credit update function API
drivers/vhost/vsock.c | 8 +-
include/linux/virtio_vsock.h | 15 +
include/net/af_vsock.h | 9 +
include/uapi/linux/virtio_vsock.h | 16 +
net/vmw_vsock/af_vsock.c | 588 +++++++++++++++-------
net/vmw_vsock/virtio_transport.c | 5 +
net/vmw_vsock/virtio_transport_common.c | 316 ++++++++++--
net/vmw_vsock/vsock_loopback.c | 5 +
tools/testing/vsock/util.c | 32 +-
tools/testing/vsock/util.h | 3 +
tools/testing/vsock/vsock_test.c | 126 +++++
11 files changed, 895 insertions(+), 228 deletions(-)
TODO:
- What to do, when server doesn't support SOCK_SEQPACKET. In current
implementation RST is replied in the same way when listening port
is not found. I think that current RST is enough,because case when
server doesn't support SEQ_PACKET is same when listener missed(e.g.
no listener in both cases).
- virtio spec patch
Ok
quoted
v3 -> v4:
- callbacks for loopback transport
- 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.
--
2.25.1
On Sun, Feb 07, 2021 at 06:14:23PM +0300, Arseny Krasnov wrote:
This prepares af_vsock.c for SEQPACKET support: some functions such
as setsockopt(), getsockopt(), connect(), recvmsg(), sendmsg() are
shared between both types of sockets, so rename them in general
manner.
Signed-off-by: Arseny Krasnov <redacted>
---
net/vmw_vsock/af_vsock.c | 64 +++++++++++++++++++++-------------------
1 file changed, 34 insertions(+), 30 deletions(-)
This patch LGTM:
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
Thanks,
Stefano
if (vsk->transport)
vsk->transport->release(vsk);
- else if (sk->sk_type == SOCK_STREAM)
+ else if (sock_type_connectible(sk->sk_type))
vsock_remove_sock(vsk);
sock_orphan(sk);
@@ -945,7 +950,7 @@ static int vsock_shutdown(struct socket *sock, int mode)
sk = sock->sk;
if (sock->state == SS_UNCONNECTED) {
err = -ENOTCONN;
- if (sk->sk_type == SOCK_STREAM)
+ if (sock_type_connectible(sk->sk_type))
return err;
} else {
sock->state = SS_DISCONNECTING;
@@ -960,7 +965,7 @@ static int vsock_shutdown(struct socket *sock, int mode)
sk->sk_state_change(sk);
release_sock(sk);
- if (sk->sk_type == SOCK_STREAM) {
+ if (sock_type_connectible(sk->sk_type)) {
sock_reset_flag(sk, SOCK_DONE);
vsock_send_shutdown(sk, mode);
}
sock_put(sk);
}
-static int vsock_stream_connect(struct socket *sock, struct sockaddr *addr,
- int addr_len, int flags)
+static int vsock_connect(struct socket *sock, struct sockaddr *addr,
+ int addr_len, int flags)
{
int err;
struct sock *sk;
@@ -1414,7 +1419,7 @@ static int vsock_accept(struct socket *sock, struct socket *newsock, int flags,
lock_sock(listener);
- if (sock->type != SOCK_STREAM) {
+ if (!sock_type_connectible(sock->type)) {
err = -EOPNOTSUPP;
goto out;
}
@@ -1491,7 +1496,7 @@ static int vsock_listen(struct socket *sock, int backlog)
lock_sock(sk);
- if (sock->type != SOCK_STREAM) {
+ if (!sock_type_connectible(sk->sk_type)) {
err = -EOPNOTSUPP;
goto out;
}
vsk->buffer_size = val;
}
-static int vsock_stream_setsockopt(struct socket *sock,
- int level,
- int optname,
- sockptr_t optval,
- unsigned int optlen)
+static int vsock_connectible_setsockopt(struct socket *sock,
+ int level,
+ int optname,
+ sockptr_t optval,
+ unsigned int optlen)
{
int err;
struct sock *sk;
@@ -1617,10 +1622,10 @@ static int vsock_stream_setsockopt(struct socket *sock,
return err;
}
-static int vsock_stream_getsockopt(struct socket *sock,
- int level, int optname,
- char __user *optval,
- int __user *optlen)
+static int vsock_connectible_getsockopt(struct socket *sock,
+ int level, int optname,
+ char __user *optval,
+ int __user *optlen)
{
int err;
int len;
@@ -1688,8 +1693,8 @@ static int vsock_stream_getsockopt(struct socket *sock,
This adds rest of logic for SEQPACKET:
1) Packet's type is now set in 'virtio_send_pkt_info()' using
type of socket.
2) SEQPACKET specific functions which send SEQ_BEGIN/SEQ_END.
Note that both functions may sleep to wait enough space for
SEQPACKET header.
3) SEQ_BEGIN/SEQ_END to TAP packet capture.
4) Send SHUTDOWN on socket close for SEQPACKET type.
Signed-off-by: Arseny Krasnov <redacted>
---
include/linux/virtio_vsock.h | 9 +++
net/vmw_vsock/virtio_transport_common.c | 99 +++++++++++++++++++++----
2 files changed, 95 insertions(+), 13 deletions(-)
@@ -165,6 +167,14 @@ void virtio_transport_deliver_tap_pkt(struct virtio_vsock_pkt *pkt)}EXPORT_SYMBOL_GPL(virtio_transport_deliver_tap_pkt);+staticu16virtio_transport_get_type(structsock*sk)+{+if(sk->sk_type==SOCK_STREAM)+returnVIRTIO_VSOCK_TYPE_STREAM;+else+returnVIRTIO_VSOCK_TYPE_SEQPACKET;+}+/* This function can only be used on connecting/connected sockets,*sinceasocketassignedtoatransportisrequired.*
@@ -179,6 +189,13 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,structvirtio_vsock_pkt*pkt;u32pkt_len=info->pkt_len;+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))return-EFAULT;
On Sun, Feb 07, 2021 at 06:14:48PM +0300, Arseny Krasnov wrote:
quoted hunk
This moves wait loop for data to dedicated function, because later
it will be used by SEQPACKET data receive loop.
Signed-off-by: Arseny Krasnov <redacted>
---
net/vmw_vsock/af_vsock.c | 158 +++++++++++++++++++++------------------
1 file changed, 86 insertions(+), 72 deletions(-)
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;
+ prepare_to_wait(sk_sleep(sk), wait, TASK_INTERRUPTIBLE);
+
+ while ((data = vsock_stream_has_data(vsk)) == 0) {
+ if (sk->sk_err != 0 ||
+ (sk->sk_shutdown & RCV_SHUTDOWN) ||
+ (vsk->peer_shutdown & SEND_SHUTDOWN)) {
+ goto out;
+ }
+
+ /* Don't wait for non-blocking sockets. */
+ if (timeout == 0) {
+ err = -EAGAIN;
+ goto out;
+ }
+
+ if (recv_data) {
+ err = transport->notify_recv_pre_block(vsk, target, recv_data);
+ if (err < 0)
+ goto out;
+ }
+
+ release_sock(sk);
+ timeout = schedule_timeout(timeout);
+ lock_sock(sk);
+
+ if (signal_pending(current)) {
+ err = sock_intr_errno(timeout);
+ goto out;
+ } else if (timeout == 0) {
+ err = -EAGAIN;
+ goto out;
+ }
+ }
+
+ finish_wait(sk_sleep(sk), wait);
+
+ /* Invalid queue pair content. XXX This should
+ * be changed to a connection reset in a later
+ * change.
+ */
+ if (data < 0)
+ return -ENOMEM;
+
+ /* Have some data, return. */
+ if (data)
+ return data;
IIUC here data must be != 0 so you can simply return data in any case.
Or cleaner, you can do 'break' instead of 'goto out' in the error paths
and after the while loop you can do something like this:
finish_wait(sk_sleep(sk), wait);
if (err)
return err;
if (data < 0)
return -ENOMEM;
return data;
}
On Sun, Feb 07, 2021 at 06:15:05PM +0300, Arseny Krasnov wrote:
quoted hunk
This moves STREAM specific data receive logic to dedicated function:
'__vsock_stream_recvmsg()', while checks that will be same for both
types of socket are in shared function: 'vsock_connectible_recvmsg()'.
Signed-off-by: Arseny Krasnov <redacted>
---
net/vmw_vsock/af_vsock.c | 117 +++++++++++++++++++++++----------------
1 file changed, 68 insertions(+), 49 deletions(-)
return err;
}
-static int
-vsock_connectible_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
- int flags)
+static int __vsock_stream_recvmsg(struct sock *sk, struct msghdr *msg,
+ size_t len, int flags)
{
- struct sock *sk;
- struct vsock_sock *vsk;
+ struct vsock_transport_recv_notify_data recv_data;
const struct vsock_transport *transport;
- int err;
- size_t target;
+ struct vsock_sock *vsk;
ssize_t copied;
+ size_t target;
long timeout;
- struct vsock_transport_recv_notify_data recv_data;
+ int err;
DEFINE_WAIT(wait);
- sk = sock->sk;
vsk = vsock_sk(sk);
- err = 0;
-
- lock_sock(sk);
-
transport = vsk->transport;
- if (!transport || sk->sk_state != TCP_ESTABLISHED) {
- /* Recvmsg is supposed to return 0 if a peer performs an
- * orderly shutdown. Differentiate between that case and when a
- * peer has not connected or a local shutdown occured with the
- * SOCK_DONE flag.
- */
- if (sock_flag(sk, SOCK_DONE))
- err = 0;
- else
- err = -ENOTCONN;
-
- goto out;
- }
-
- if (flags & MSG_OOB) {
- err = -EOPNOTSUPP;
- goto out;
- }
-
- /* We don't check peer_shutdown flag here since peer may actually shut
- * down, but there can be data in the queue that a local socket can
- * receive.
- */
- if (sk->sk_shutdown & RCV_SHUTDOWN) {
- err = 0;
- goto out;
- }
-
- /* It is valid on Linux to pass in a zero-length receive buffer. This
- * is not an error. We may as well bail out now.
- */
- if (!len) {
- err = 0;
- goto out;
- }
-
/* We must not copy less than target bytes into the user's buffer
* before returning successfully, so we wait for the consume queue to
* have that much data to consume before dequeueing. Note that this
At the end of __vsock_stream_recvmsg() you are calling release_sock(sk)
and it's wrong since we are releasing it in vsock_connectible_recvmsg().
Please fix it.
struct msghdr *msg, size_t len,
return err;
}
+static int
+vsock_connectible_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
+ int flags)
+{
+ struct sock *sk;
+ struct vsock_sock *vsk;
+ const struct vsock_transport *transport;
+ int err;
+
+ DEFINE_WAIT(wait);
+
+ sk = sock->sk;
+ vsk = vsock_sk(sk);
+ err = 0;
+
+ lock_sock(sk);
+
+ transport = vsk->transport;
+
+ if (!transport || sk->sk_state != TCP_ESTABLISHED) {
+ /* Recvmsg is supposed to return 0 if a peer performs an
+ * orderly shutdown. Differentiate between that case and when a
+ * peer has not connected or a local shutdown occurred with the
+ * SOCK_DONE flag.
+ */
+ if (sock_flag(sk, SOCK_DONE))
+ err = 0;
+ else
+ err = -ENOTCONN;
+
+ goto out;
+ }
+
+ if (flags & MSG_OOB) {
+ err = -EOPNOTSUPP;
+ goto out;
+ }
+
+ /* We don't check peer_shutdown flag here since peer may actually shut
+ * down, but there can be data in the queue that a local socket can
+ * receive.
+ */
+ if (sk->sk_shutdown & RCV_SHUTDOWN) {
+ err = 0;
+ goto out;
+ }
+
+ /* It is valid on Linux to pass in a zero-length receive buffer. This
+ * is not an error. We may as well bail out now.
+ */
+ if (!len) {
+ err = 0;
+ goto out;
+ }
+
+ err = __vsock_stream_recvmsg(sk, msg, len, flags);
+
+out:
+ release_sock(sk);
+ return err;
+}
+
On Sun, Feb 07, 2021 at 06:15:22PM +0300, Arseny Krasnov wrote:
quoted hunk
This adds receive loop for SEQPACKET. It looks like receive loop for
STREAM, but there is a little bit difference:
1) It doesn't call notify callbacks.
2) It doesn't care about 'SO_SNDLOWAT' and 'SO_RCVLOWAT' values, because
there is no sense for these values in SEQPACKET case.
3) It waits until whole record is received or error is found during
receiving.
4) It processes and sets 'MSG_TRUNC' flag.
So to avoid extra conditions for two types of socket inside one loop, two
independent functions were created.
Signed-off-by: Arseny Krasnov <redacted>
---
include/net/af_vsock.h | 5 +++
net/vmw_vsock/af_vsock.c | 96 +++++++++++++++++++++++++++++++++++++++-
2 files changed, 100 insertions(+), 1 deletion(-)
CHECK: Alignment should match open parenthesis
#35: FILE: include/net/af_vsock.h:141:
+ int (*seqpacket_dequeue)(struct vsock_sock *, struct msghdr *,
+ int flags, bool *msg_ready);
And to make checkpatch.pl happy please use the identifier name also for
the others parameter. I know we haven't done this before, but for new
code I think we can do it.
quoted hunk
+
/* Notification. */
int (*notify_poll_in)(struct vsock_sock *, size_t, bool *);
int (*notify_poll_out)(struct vsock_sock *, size_t, bool *);
On Sun, Feb 07, 2021 at 06:15:41PM +0300, Arseny Krasnov wrote:
quoted hunk
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 <redacted>
---
include/net/af_vsock.h | 2 +
net/vmw_vsock/af_vsock.c | 93 ++++++++++++++++++++++++++--------------
2 files changed, 62 insertions(+), 33 deletions(-)
On Sun, Feb 07, 2021 at 06:15:57PM +0300, Arseny Krasnov wrote:
quoted hunk
This adds some logic to current stream enqueue function for SEQPACKET
support:
1) Send record's begin/end marker.
2) Return value from enqueue function is whole record length or error
for SOCK_SEQPACKET.
Signed-off-by: Arseny Krasnov <redacted>
---
include/net/af_vsock.h | 2 ++
net/vmw_vsock/af_vsock.c | 22 ++++++++++++++++++++--
2 files changed, 22 insertions(+), 2 deletions(-)
Maybe we should move this check after the try_module_get() call, since
the memory pointed by 'new_transport' pointer can be deallocated in the
meantime.
Also, if the socket had a transport before, we should deassign it before
returning an error.
/* 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 connect 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
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);
@@ -659,9 +659,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 connect 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);
@@ -952,10 +953,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 connect 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;
transport = vsk->transport;
- /* Callers should not provide a destination with stream sockets. */
+ /* Callers should not provide a destination with connect oriented
+ * sockets.
+ */
if (msg->msg_namelen) {
err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
goto out;
--
2.25.1
On Sun, Feb 07, 2021 at 06:16:46PM +0300, Arseny Krasnov wrote:
quoted hunk
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 <redacted>
---
include/linux/virtio_vsock.h | 5 +
include/uapi/linux/virtio_vsock.h | 16 ++++
net/vmw_vsock/virtio_transport_common.c | 120 ++++++++++++++++++++++++
3 files changed, 141 insertions(+)
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 */
Also this function is not used, maybe you can add in this patch the
virtio_transport_seqpacket_dequeue() implementation.
+ 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->user_read_copied != vvs->user_read_seq_len ||
+ (le32_to_cpu(seq_hdr->msg_cnt) - vvs->curr_rx_msg_cnt) != 1) {
+ /* 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->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);
+ }
+ }
+
+ spin_unlock_bh(&vvs->rx_lock);
+
+ virtio_transport_send_credit_update(vsk, VIRTIO_VSOCK_TYPE_SEQPACKET,
+ NULL);
+
+ return err;
+}
+
ssize_t
virtio_transport_stream_dequeue(struct vsock_sock *vsk,
struct msghdr *msg,
--
2.25.1
On Sun, Feb 07, 2021 at 06:17:08PM +0300, Arseny Krasnov wrote:
quoted hunk
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 <redacted>
---
include/linux/virtio_vsock.h | 1 +
net/vmw_vsock/virtio_transport_common.c | 40 +++++++++++++++++++++++++
2 files changed, 41 insertions(+)
On Thu, Feb 11, 2021 at 02:54:28PM +0100, Stefano Garzarella wrote:
On Sun, Feb 07, 2021 at 06:16:46PM +0300, Arseny Krasnov wrote:
quoted
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 <redacted>
---
include/linux/virtio_vsock.h | 5 +
include/uapi/linux/virtio_vsock.h | 16 ++++
net/vmw_vsock/virtio_transport_common.c | 120 ++++++++++++++++++++++++
3 files changed, 141 insertions(+)
Maybe it's better 'msg_id' for this field, since we use it to identify a
message. Then whether we use a counter or a random number, I think it's
just an implementation detail.
As Michael said, perhaps this detail should be discussed in the proposal
for VIRTIO spec changes.
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 */
Also this function is not used, maybe you can add in this patch the
virtio_transport_seqpacket_dequeue() implementation.
quoted
+ 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->user_read_copied != vvs->user_read_seq_len ||
+ (le32_to_cpu(seq_hdr->msg_cnt) - vvs->curr_rx_msg_cnt) != 1) {
+ /* 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->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);
+ }
+ }
+
+ spin_unlock_bh(&vvs->rx_lock);
+
+ virtio_transport_send_credit_update(vsk, VIRTIO_VSOCK_TYPE_SEQPACKET,
+ NULL);
+
+ return err;
+}
+
ssize_t
virtio_transport_stream_dequeue(struct vsock_sock *vsk,
struct msghdr *msg,
--
2.25.1
On Sun, Feb 07, 2021 at 06:17:44PM +0300, Arseny Krasnov wrote:
quoted hunk
This adds rest of logic for SEQPACKET:
1) Packet's type is now set in 'virtio_send_pkt_info()' using
type of socket.
2) SEQPACKET specific functions which send SEQ_BEGIN/SEQ_END.
Note that both functions may sleep to wait enough space for
SEQPACKET header.
3) SEQ_BEGIN/SEQ_END to TAP packet capture.
4) Send SHUTDOWN on socket close for SEQPACKET type.
Signed-off-by: Arseny Krasnov <redacted>
---
include/linux/virtio_vsock.h | 9 +++
net/vmw_vsock/virtio_transport_common.c | 99 +++++++++++++++++++++----
2 files changed, 95 insertions(+), 13 deletions(-)
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:
Please move this patch before the test and I'd change the prefix in
"vsock_loopback" or "vsock/loopback".
Thanks,
Stefano
On Sun, Feb 07, 2021 at 06:18:48PM +0300, Arseny Krasnov wrote:
quoted hunk
This adds SEQPACKET ops for loopback transport
Signed-off-by: Arseny Krasnov <redacted>
---
net/vmw_vsock/vsock_loopback.c | 5 +++++
1 file changed, 5 insertions(+)
On Sun, Feb 07, 2021 at 06:19:03PM +0300, Arseny Krasnov wrote:
'virtio_transport_send_credit_update()' has some extra args:
1) 'type' may be set in 'virtio_transport_send_pkt_info()' using type
of socket.
2) This function is static and 'hdr' arg was always NULL.
Okay, I saw this patch after my previous comment.
I think this looks good, but please move this before your changes (e.g.
before patch 'virtio/vsock: dequeue callback for SOCK_SEQPACKET').
In this way you don't need to modify
virtio_transport_notify_buffer_size(), calling
virtio_transport_get_type() and then remove these changes.
It's generally not a good idea to make changes in a patch and then
remove them a few patches later in the same series. This should ring a
bell about moving these changes before others.
Thanks,
Stefano
Hi Arseny,
On Mon, Feb 08, 2021 at 09:32:59AM +0300, Arseny Krasnov wrote:
On 07.02.2021 19:20, Michael S. Tsirkin wrote:
quoted
On Sun, Feb 07, 2021 at 06:12:56PM +0300, Arseny Krasnov wrote:
quoted
This patchset impelements 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 count and message length:
struct virtio_vsock_seq_hdr {
__le32 msg_cnt;
__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 contains length of record and counter), then
counter is incremented and all data is sent as usual 'RW' packets and
finally SEQ_END is sent(it also carries counter of message, which is
counter of SEQ_BEGIN + 1), also after sedning SEQ_END counter is
incremented again. On receiver's side, length of record is known from
packet with start record marker. To check that no packets were dropped
by transport, counters of two sequential SEQ_BEGIN and SEQ_END are
checked(counter of SEQ_END must be bigger that counter of SEQ_BEGIN by
1) and length of data between two markers is compared to 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.
Arseny Krasnov (17):
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: 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: setup SEQPACKET ops for transport
vhost/vsock: setup SEQPACKET ops for transport
vsock_test: add SOCK_SEQPACKET tests
loopback/vsock: setup SEQPACKET ops for transport
virtio/vsock: simplify credit update function API
drivers/vhost/vsock.c | 8 +-
include/linux/virtio_vsock.h | 15 +
include/net/af_vsock.h | 9 +
include/uapi/linux/virtio_vsock.h | 16 +
net/vmw_vsock/af_vsock.c | 588 +++++++++++++++-------
net/vmw_vsock/virtio_transport.c | 5 +
net/vmw_vsock/virtio_transport_common.c | 316 ++++++++++--
net/vmw_vsock/vsock_loopback.c | 5 +
tools/testing/vsock/util.c | 32 +-
tools/testing/vsock/util.h | 3 +
tools/testing/vsock/vsock_test.c | 126 +++++
11 files changed, 895 insertions(+), 228 deletions(-)
TODO:
- What to do, when server doesn't support SOCK_SEQPACKET. In current
implementation RST is replied in the same way when listening port
is not found. I think that current RST is enough,because case when
server doesn't support SEQ_PACKET is same when listener missed(e.g.
no listener in both cases).
I think is fine.
quoted
- virtio spec patch
Ok
Yes, please prepare a patch to discuss the VIRTIO spec changes.
For example for 'virtio_vsock_seq_hdr', I left a comment about 'msg_cnt'
naming that should be better to discuss with virtio guys.
Anyway, I reviewed this series and I left some comments.
I think we are in a good shape :-)
Thanks,
Stefano
On 7 Feb 2021, at 16:14, Arseny Krasnov [off-list ref] wrote:
This moves wait loop for data to dedicated function, because later
it will be used by SEQPACKET data receive loop.
Signed-off-by: Arseny Krasnov <redacted>
---
net/vmw_vsock/af_vsock.c | 158 +++++++++++++++++++++------------------
1 file changed, 86 insertions(+), 72 deletions(-)
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;
+ prepare_to_wait(sk_sleep(sk), wait, TASK_INTERRUPTIBLE);
+
+ while ((data = vsock_stream_has_data(vsk)) == 0) {
+ if (sk->sk_err != 0 ||
+ (sk->sk_shutdown & RCV_SHUTDOWN) ||
+ (vsk->peer_shutdown & SEND_SHUTDOWN)) {
+ goto out;
+ }
+
+ /* Don't wait for non-blocking sockets. */
+ if (timeout == 0) {
+ err = -EAGAIN;
+ goto out;
+ }
+
+ if (recv_data) {
+ err = transport->notify_recv_pre_block(vsk, target, recv_data);
+ if (err < 0)
+ goto out;
+ }
+
+ release_sock(sk);
+ timeout = schedule_timeout(timeout);
+ lock_sock(sk);
+
+ if (signal_pending(current)) {
+ err = sock_intr_errno(timeout);
+ goto out;
+ } else if (timeout == 0) {
+ err = -EAGAIN;
+ goto out;
+ }
+ }
+
+ finish_wait(sk_sleep(sk), wait);
+
+ /* Invalid queue pair content. XXX This should
+ * be changed to a connection reset in a later
+ * change.
+ */
Since you are here, could you update this comment to something like:
/* 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;
+
+ /* Have some data, return. */
+ if (data)
+ return data;
+
+out:
+ finish_wait(sk_sleep(sk), wait);
+ return err;
+}
I agree with Stefanos suggestion to get rid of the out: part and just have the single finish_wait().
quoted hunk
+
static int
vsock_connectible_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
int flags)
There is a small change in the behaviour here if vsock_stream_has_data(vsk)
returned something < 0. Since you just do a break, the err value can be updated
if there is an sk->sk_err, a receive shutdown has been performed or data has
already been copied. That should be ok, though.
Hi Arseny,
On Mon, Feb 08, 2021 at 09:32:59AM +0300, Arseny Krasnov wrote:
quoted
On 07.02.2021 19:20, Michael S. Tsirkin wrote:
quoted
On Sun, Feb 07, 2021 at 06:12:56PM +0300, Arseny Krasnov wrote:
quoted
This patchset impelements 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 count and message length:
struct virtio_vsock_seq_hdr {
__le32 msg_cnt;
__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 contains length of record and counter), then
counter is incremented and all data is sent as usual 'RW' packets and
finally SEQ_END is sent(it also carries counter of message, which is
counter of SEQ_BEGIN + 1), also after sedning SEQ_END counter is
incremented again. On receiver's side, length of record is known from
packet with start record marker. To check that no packets were dropped
by transport, counters of two sequential SEQ_BEGIN and SEQ_END are
checked(counter of SEQ_END must be bigger that counter of SEQ_BEGIN by
1) and length of data between two markers is compared to 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.
Arseny Krasnov (17):
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: 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: setup SEQPACKET ops for transport
vhost/vsock: setup SEQPACKET ops for transport
vsock_test: add SOCK_SEQPACKET tests
loopback/vsock: setup SEQPACKET ops for transport
virtio/vsock: simplify credit update function API
drivers/vhost/vsock.c | 8 +-
include/linux/virtio_vsock.h | 15 +
include/net/af_vsock.h | 9 +
include/uapi/linux/virtio_vsock.h | 16 +
net/vmw_vsock/af_vsock.c | 588 +++++++++++++++-------
net/vmw_vsock/virtio_transport.c | 5 +
net/vmw_vsock/virtio_transport_common.c | 316 ++++++++++--
net/vmw_vsock/vsock_loopback.c | 5 +
tools/testing/vsock/util.c | 32 +-
tools/testing/vsock/util.h | 3 +
tools/testing/vsock/vsock_test.c | 126 +++++
11 files changed, 895 insertions(+), 228 deletions(-)
TODO:
- What to do, when server doesn't support SOCK_SEQPACKET. In current
implementation RST is replied in the same way when listening port
is not found. I think that current RST is enough,because case when
server doesn't support SEQ_PACKET is same when listener missed(e.g.
no listener in both cases).
I think is fine.
quoted
quoted
- virtio spec patch
Ok
Yes, please prepare a patch to discuss the VIRTIO spec changes.
For example for 'virtio_vsock_seq_hdr', I left a comment about 'msg_cnt'
naming that should be better to discuss with virtio guys.
Ok, i'll prepare it in v5. So I have to send it both LKML(as one of patches) and
virtio mailing lists? (e.g. virtio-comment@lists.oasis-open.org)
Anyway, I reviewed this series and I left some comments.
I think we are in a good shape :-)
Great, thanks for review. I'll consider all review comments in next version.
On Fri, Feb 12, 2021 at 09:11:50AM +0300, Arseny Krasnov wrote:
On 11.02.2021 17:57, Stefano Garzarella wrote:
quoted
Hi Arseny,
On Mon, Feb 08, 2021 at 09:32:59AM +0300, Arseny Krasnov wrote:
quoted
On 07.02.2021 19:20, Michael S. Tsirkin wrote:
quoted
On Sun, Feb 07, 2021 at 06:12:56PM +0300, Arseny Krasnov wrote:
quoted
This patchset impelements 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 count and message length:
struct virtio_vsock_seq_hdr {
__le32 msg_cnt;
__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 contains length of record and counter), then
counter is incremented and all data is sent as usual 'RW' packets and
finally SEQ_END is sent(it also carries counter of message, which is
counter of SEQ_BEGIN + 1), also after sedning SEQ_END counter is
incremented again. On receiver's side, length of record is known from
packet with start record marker. To check that no packets were dropped
by transport, counters of two sequential SEQ_BEGIN and SEQ_END are
checked(counter of SEQ_END must be bigger that counter of SEQ_BEGIN by
1) and length of data between two markers is compared to 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.
Arseny Krasnov (17):
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: 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: setup SEQPACKET ops for transport
vhost/vsock: setup SEQPACKET ops for transport
vsock_test: add SOCK_SEQPACKET tests
loopback/vsock: setup SEQPACKET ops for transport
virtio/vsock: simplify credit update function API
drivers/vhost/vsock.c | 8 +-
include/linux/virtio_vsock.h | 15 +
include/net/af_vsock.h | 9 +
include/uapi/linux/virtio_vsock.h | 16 +
net/vmw_vsock/af_vsock.c | 588 +++++++++++++++-------
net/vmw_vsock/virtio_transport.c | 5 +
net/vmw_vsock/virtio_transport_common.c | 316 ++++++++++--
net/vmw_vsock/vsock_loopback.c | 5 +
tools/testing/vsock/util.c | 32 +-
tools/testing/vsock/util.h | 3 +
tools/testing/vsock/vsock_test.c | 126 +++++
11 files changed, 895 insertions(+), 228 deletions(-)
TODO:
- What to do, when server doesn't support SOCK_SEQPACKET. In current
implementation RST is replied in the same way when listening port
is not found. I think that current RST is enough,because case when
server doesn't support SEQ_PACKET is same when listener missed(e.g.
no listener in both cases).
I think is fine.
quoted
quoted
- virtio spec patch
Ok
Yes, please prepare a patch to discuss the VIRTIO spec changes.
For example for 'virtio_vsock_seq_hdr', I left a comment about 'msg_cnt'
naming that should be better to discuss with virtio guys.
Ok, i'll prepare it in v5. So I have to send it both LKML(as one of patches) and
virtio mailing lists? (e.g. virtio-comment@lists.oasis-open.org)
I think you can send the VIRTIO spec patch separately from this series
to virtio-comment, maybe CCing virtualization@lists.linux-foundation.org
But Michael could correct me :-)
quoted
Anyway, I reviewed this series and I left some comments.
I think we are in a good shape :-)
Great, thanks for review. I'll consider all review comments in next
version.
Maybe we should move this check after the try_module_get() call, since
the memory pointed by 'new_transport' pointer can be deallocated in the
meantime.
Also, if the socket had a transport before, we should deassign it before
returning an error.
I think previous transport is deassigned immediately after this
'switch()' on sk->sk_type:
if (vsk->transport) {
...
vsock_deassign_transport(vsk);
}
Ok, check will be moved after 'try_module_get()'.
On 7 Feb 2021, at 16:14, Arseny Krasnov [off-list ref] wrote:
This moves wait loop for data to dedicated function, because later
it will be used by SEQPACKET data receive loop.
Signed-off-by: Arseny Krasnov <redacted>
---
net/vmw_vsock/af_vsock.c | 158 +++++++++++++++++++++------------------
1 file changed, 86 insertions(+), 72 deletions(-)
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;
+ prepare_to_wait(sk_sleep(sk), wait, TASK_INTERRUPTIBLE);
+
+ while ((data = vsock_stream_has_data(vsk)) == 0) {
+ if (sk->sk_err != 0 ||
+ (sk->sk_shutdown & RCV_SHUTDOWN) ||
+ (vsk->peer_shutdown & SEND_SHUTDOWN)) {
+ goto out;
+ }
+
+ /* Don't wait for non-blocking sockets. */
+ if (timeout == 0) {
+ err = -EAGAIN;
+ goto out;
+ }
+
+ if (recv_data) {
+ err = transport->notify_recv_pre_block(vsk, target, recv_data);
+ if (err < 0)
+ goto out;
+ }
+
+ release_sock(sk);
+ timeout = schedule_timeout(timeout);
+ lock_sock(sk);
+
+ if (signal_pending(current)) {
+ err = sock_intr_errno(timeout);
+ goto out;
+ } else if (timeout == 0) {
+ err = -EAGAIN;
+ goto out;
+ }
+ }
+
+ finish_wait(sk_sleep(sk), wait);
+
+ /* Invalid queue pair content. XXX This should
+ * be changed to a connection reset in a later
+ * change.
+ */
Since you are here, could you update this comment to something like:
/* Internal transport error when checking for available
* data. XXX This should be changed to a connection
* reset in a later change.
*/
quoted
+ if (data < 0)
+ return -ENOMEM;
+
+ /* Have some data, return. */
+ if (data)
+ return data;
+
+out:
+ finish_wait(sk_sleep(sk), wait);
+ return err;
+}
I agree with Stefanos suggestion to get rid of the out: part and just have the single finish_wait().
quoted
+
static int
vsock_connectible_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
int flags)
There is a small change in the behaviour here if vsock_stream_has_data(vsk)
returned something < 0. Since you just do a break, the err value can be updated
if there is an sk->sk_err, a receive shutdown has been performed or data has
already been copied. That should be ok, though.
May be i can add the following 'if' after while (1) loop:
There was:
if (sk->sk_err)
err = -sk->sk->sk_err;
else if (sk->sk_shutdown & RCV_SHUTDOWN)
err = 0;
if (copied > 0)
err = copied;
Will be:
if (err == 0) {
if (sk->sk_err)
err = -sk->sk->sk_err;
else if (sk->sk_shutdown & RCV_SHUTDOWN)
err = 0;
if (copied > 0)
err = copied;
}
E.g. update 'err' only if it is clear. Don't touch otherwise