linux-user: Introducing support for 'recvmmsg_time64()'
cover linux-user: Introducing support for 'recvmmsg_time64()'
This patch introduces functionality for 'recvmmsg_time64()' which is a year 2038 safe variant of 'recvmmsg()'. This new time64 syscall is introduced in series of three patches rather than in one patch because a little modification was needed before the syscall could be introduced properly.
The first patch in the series introduces a little modification for already existing implementations for 'recvmmsg()' and 'sendmmsg()' to use the host syscall result instead of using a loop over 'recvmsg()' and 'sendmsg()'.
The second patch in the series introduces a little fix in the implementation of 'recvmmsg()' that introduces the timeout argument which is of type 'struct timespec' for which the separate time64 variant is added.
The third patch in the series introduces the implementation of 'recvmmsg_time64()' itself.
Testing method:
The implementation was tested using a modified LTP test suite where a timeout test case was added for 'recvmmsg()'. This modified test suite can be found at:
https://github.com/bozutaf/ltp
(A patch with this fix will be sent to LTP mailing list soon)
Filip Bozuta (3):
linux-user: Modify 'sendmmsg()' and 'recvmmsg()' implementation
linux-user: Fix 'recvmmsg()' implementation
linux-user: Add support for 'recvmmsg_time64()'
linux-user/syscall.c | 274 +++++++++++++++++++++++++++++-------------- 1 file changed, 184 insertions(+), 90 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/3 linux-user: Modify 'sendmmsg()' and 'recvmmsg()' implementation
Implementations of 'sendmmsg()' and 'recvmmsg()' in 'syscall.c' use a loop over a host command of 'sendmsg()' and 'recvmsg()' respectively to send/receive individual messages from a socket. This patch changes these implementations to use the host commands 'sendmmsg()' and 'recvmmsg()' to send all messages without looping over 'sendmsg()' and 'recvmsg()'.
Implementation notes:
Parts of code from 'do_sendrecvmsg_locked()', that are used to transfer values of 'struct msghdr' between host and target, were moved to separate functions 'target_to_host_msghdr()' and 'host_to_target_msghdr()'. These functions are used in 'do_sendrecvmmsg()' to transfer the data of each individual 'struct msghdr' from the 'msgvec' argument. Memory allocation for the 'iovec' field is done outside of these functions as to ensure that the memory is freed after the syscall execution.
--- linux-user/syscall.c | 243 ++++++++++++++++++++++++++++--------------- 1 file changed, 159 insertions(+), 84 deletions(-)
linux-user/syscall.c +159 -84
@@ -798,6 +798,10 @@ safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len, int, flags, struct sockaddr *, addr, socklen_t *, addrlen) safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags) safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags) +safe_syscall4(int, sendmmsg, int, fd, struct mmsghdr *, msgvec, + unsigned int, vlen, int, flags) +safe_syscall5(int, recvmmsg, int, fd, struct mmsghdr *, msgvec, + unsigned int, vlen, int, flags, struct timespec *, timeout) safe_syscall2(int, flock, int, fd, int, operation) #ifdef TARGET_NR_rt_sigtimedwait safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo,
@@ -3064,41 +3068,94 @@ static abi_long do_connect(int sockfd, abi_ulong target_addr,return get_errno (safe_connect (sockfd, addr, addrlen)); } -/* do_sendrecvmsg_locked() Must return target values and target errnos. */ -static abi_long do_sendrecvmsg_locked (int fd,struct target_msghdr *msgp, -int flags,int send) +static abi_long target_to_host_msghdr (int fd,struct msghdr *host_msg, +struct target_msghdr *target_msg, +int send) { -abi_long ret, len; -struct msghdr msg; -abi_ulong count; -struct iovec *vec; -abi_ulong target_vec; - -if (msgp->msg_name) { - msg.msg_namelen =tswap32 (msgp->msg_namelen); - msg.msg_name =alloca (msg.msg_namelen+1 ); - ret =target_to_host_sockaddr (fd, msg.msg_name, -tswapal (msgp->msg_name), - msg.msg_namelen); +abi_long ret =0 ; +if (target_msg->msg_name) { + host_msg->msg_namelen =tswap32 (target_msg->msg_namelen); + host_msg->msg_name =alloca (host_msg->msg_namelen +1 ); + ret =target_to_host_sockaddr (fd, host_msg->msg_name, +tswapal (target_msg->msg_name), + host_msg->msg_namelen);if (ret == -TARGET_EFAULT ) { -/* For connected sockets msg_name and msg_namelen must +/* +* For connected sockets msg_name and msg_namelen must * be ignored, so returning EFAULT immediately is wrong. * Instead, pass a bad msg_name to the host kernel, and * let it decide whether to return EFAULT or not. */ - msg.msg_name = (void *)-1 ; + host_msg->msg_name = (void *)-1 ; + ret =0 ; }else if (ret) { - goto out2; +return ret; } }else { - msg.msg_name =NULL ; - msg.msg_namelen =0 ; + host_msg->msg_name =NULL ; + host_msg->msg_namelen =0 ; + } + host_msg->msg_controllen =2 *tswapal (target_msg->msg_controllen); + host_msg->msg_control =alloca (host_msg->msg_controllen); +memset (host_msg->msg_control,0 , host_msg->msg_controllen); + host_msg->msg_flags =tswap32 (target_msg->msg_flags); +if (send) { +if (fd_trans_target_to_host_data (fd)) { +void *host_iov_base; + + host_iov_base =g_malloc (host_msg->msg_iov->iov_len); +memcpy (host_iov_base, host_msg->msg_iov->iov_base, + host_msg->msg_iov->iov_len); + ret =fd_trans_target_to_host_data (fd)(host_iov_base, + host_msg->msg_iov->iov_len); +if (ret >=0 ) { + host_msg->msg_iov->iov_base = host_iov_base; + ret =0 ; + } +g_free (host_iov_base); + }else { + ret =target_to_host_cmsg (host_msg, target_msg); + } + } +return ret; +} + +static abi_long host_to_target_msghdr (int fd,struct target_msghdr *target_msg, +struct msghdr *host_msg,int len) +{ +abi_long ret; + +if (fd_trans_host_to_target_data (fd)) { + ret =fd_trans_host_to_target_data (fd)(host_msg->msg_iov->iov_base, +MIN (host_msg->msg_iov->iov_len, len)); + }else { + ret =host_to_target_cmsg (target_msg, host_msg); + } +if (!is_error (ret)) { + target_msg->msg_namelen =tswap32 (host_msg->msg_namelen); + target_msg->msg_flags =tswap32 (host_msg->msg_flags); +if (host_msg->msg_name !=NULL && host_msg->msg_name != (void *)-1 ) { + ret =host_to_target_sockaddr (tswapal (target_msg->msg_name), + host_msg->msg_name, host_msg->msg_namelen); +if (ret) { +return ret; + } + } + ret = len; } - msg.msg_controllen =2 *tswapal (msgp->msg_controllen); - msg.msg_control =alloca (msg.msg_controllen); -memset (msg.msg_control,0 , msg.msg_controllen); - msg.msg_flags =tswap32 (msgp->msg_flags); +return ret; +} + +/* do_sendrecvmsg_locked() Must return target values and target errnos. */ +static abi_long do_sendrecvmsg_locked (int fd,struct target_msghdr *msgp, +int flags,int send) +{ +abi_long ret; +struct msghdr msg; +abi_ulong count; +struct iovec *vec; +abi_ulong target_vec; count =tswapal (msgp->msg_iovlen); target_vec =tswapal (msgp->msg_iov);
@@ -3120,48 +3177,18 @@ static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp, msg.msg_iovlen = count; msg.msg_iov = vec; -if (send) { -if (fd_trans_target_to_host_data (fd)) { -void *host_msg; + ret =target_to_host_msghdr (fd, &msg, msgp, send); - host_msg =g_malloc (msg.msg_iov->iov_len); -memcpy (host_msg, msg.msg_iov->iov_base, msg.msg_iov->iov_len); - ret =fd_trans_target_to_host_data (fd)(host_msg, - msg.msg_iov->iov_len); -if (ret >=0 ) { - msg.msg_iov->iov_base = host_msg; - ret =get_errno (safe_sendmsg (fd, &msg, flags)); - } -g_free (host_msg); - }else { - ret =target_to_host_cmsg (&msg, msgp); -if (ret ==0 ) { - ret =get_errno (safe_sendmsg (fd, &msg, flags)); - } - } +if (ret) { + goto out; + } + +if (send) { + ret =get_errno (safe_sendmsg (fd, &msg, flags)); }else { ret =get_errno (safe_recvmsg (fd, &msg, flags));if (!is_error (ret)) { - len = ret; -if (fd_trans_host_to_target_data (fd)) { - ret =fd_trans_host_to_target_data (fd)(msg.msg_iov->iov_base, -MIN (msg.msg_iov->iov_len, len)); - }else { - ret =host_to_target_cmsg (msgp, &msg); - } -if (!is_error (ret)) { - msgp->msg_namelen =tswap32 (msg.msg_namelen); - msgp->msg_flags =tswap32 (msg.msg_flags); -if (msg.msg_name !=NULL && msg.msg_name != (void *)-1 ) { - ret =host_to_target_sockaddr (tswapal (msgp->msg_name), - msg.msg_name, msg.msg_namelen); -if (ret) { - goto out; - } - } - - ret = len; - } + ret =host_to_target_msghdr (fd, msgp, &msg, ret); } }
@@ -3188,50 +3215,98 @@ static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,return ret; } -/* We don't rely on the C library to have sendmmsg/recvmmsg support, -* so it might not have this *mmsg-specific flag either. -*/ -#ifndef MSG_WAITFORONE -#define MSG_WAITFORONE 0x10000 -#endif - -static abi_long do_sendrecvmmsg (int fd,abi_ulong target_msgvec, +static abi_long do_sendrecvmmsg (int fd,abi_ulong target_msgvec_addr,unsigned int vlen,unsigned int flags,int send) { -struct target_mmsghdr *mmsgp; +struct mmsghdr *host_msgvec; +struct target_mmsghdr *target_msgvec;abi_long ret =0 ; +int num_sentrecv =0 ; +int num_iovec =0 ; +abi_ulong iovlen =0 ; +struct iovec *host_iovec; +abi_ulong target_iovec;int i;if (vlen >UIO_MAXIOV ) { vlen =UIO_MAXIOV ; } - mmsgp =lock_user (VERIFY_WRITE , target_msgvec,sizeof (*mmsgp) * vlen,1 ); -if (!mmsgp) { + target_msgvec =lock_user (VERIFY_WRITE , target_msgvec_addr, +sizeof (target_msgvec) * vlen,1 ); +if (!target_msgvec) {return -TARGET_EFAULT ; } + host_msgvec =alloca (sizeof (*host_msgvec) * vlen); +for (i =0 ; i < vlen; i++) { - ret =do_sendrecvmsg_locked (fd, &mmsgp[i].msg_hdr, flags, send); + + iovlen =tswapal (target_msgvec[i].msg_hdr.msg_iovlen); + target_iovec =tswapal (target_msgvec[i].msg_hdr.msg_iov); + +if (iovlen >IOV_MAX ) { + ret = -TARGET_EMSGSIZE ; +break ; + } + host_iovec =lock_iovec (send ?VERIFY_READ :VERIFY_WRITE , + target_iovec, iovlen, send); + +if (host_iovec ==NULL ) { + ret = -host_to_target_errno (errno); +break ; + } + + num_iovec++; + + host_msgvec[i].msg_hdr.msg_iovlen = iovlen; + host_msgvec[i].msg_hdr.msg_iov = host_iovec; + + ret =target_to_host_msghdr (fd, &host_msgvec[i].msg_hdr, + &target_msgvec[i].msg_hdr, send); +if (is_error (ret)) {break ; } - mmsgp[i].msg_len =tswap32 (ret); -/* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */ -if (flags &MSG_WAITFORONE ) { - flags |=MSG_DONTWAIT ; + } + +if (i >0 ) { +if (send) { + ret =get_errno (safe_sendmmsg (fd, host_msgvec, i, flags)); + }else { + ret =get_errno (safe_recvmmsg (fd, host_msgvec, i, flags,NULL )); + } + } + +if (!is_error (ret)) { + num_sentrecv = ret; +for (i =0 ; i < num_sentrecv; i++) { +if (!send) { + ret =host_to_target_msghdr (fd, &target_msgvec[i].msg_hdr, + &host_msgvec[i].msg_hdr, + host_msgvec[i].msg_len); +if (is_error (ret)) { + num_sentrecv = i; +break ; + } + } + target_msgvec[i].msg_len =tswap32 (host_msgvec[i].msg_len); } } -unlock_user (mmsgp, target_msgvec,sizeof (*mmsgp) * i); +for (i =0 ; i < num_iovec; i++) { + target_iovec =tswapal (target_msgvec[i].msg_hdr.msg_iov); +unlock_iovec (host_msgvec[i].msg_hdr.msg_iov, + target_iovec, iovlen, !send); + } + +unlock_user (target_msgvec, target_msgvec_addr,sizeof (*target_msgvec) * i); -/* Return number of datagrams sent if we sent any at all; -* otherwise return the error. -*/ -if (i) { -return i; +if (num_sentrecv) { +return num_sentrecv; } +return ret; }
Signature · 1 line
2.25.1
Re: [PATCH 1/3] linux-user: Modify 'sendmmsg()' and 'recvmmsg()' implementation
Le 31/07/2020 à 21:06, Filip Bozuta a écrit :
Quoted 16 lines
Implementations of '
sendmmsg()' and 'recvmmsg()' in 'syscall.c' use a loop over a host command of 'sendmsg()' and 'recvmsg()' respectively to send/receive individual messages from a socket. This patch changes these implementations to use the host commands 'sendmmsg()' and 'recvmmsg()' to send all messages without looping over 'sendmsg()' and 'recvmsg()'.Implementation notes:
Parts of code from '
do_sendrecvmsg_locked()', that are used to transfer values of 'struct msghdr' between host and target, were moved to separate functions 'target_to_host_msghdr()' and 'host_to_target_msghdr()'. These functions are used in 'do_sendrecvmmsg()' to transfer the data of each individual 'struct msghdr' from the 'msgvec' argument. Memory allocation for the 'iovec' field is done outside of these functions as to ensure that the memory is freed after the syscall execution.
Quoted 5 lines
Signed-off-by Filip Bozuta <[email protected]>--- linux-user/syscall.c | 243 ++++++++++++++++++++++++++++--------------- 1 file changed, 159 insertions(+), 84 deletions(-)
I'm sorry but after studying the changes needed I think it's better to keep the existing code.
For instance, if we have a EFAULT (or EMSGSIZE) while reading the iovec we must stop to send data and exit with the error code.
Your code correctly manages the detection of the error and stops the conversion of the iovec. Then it uses the converted iovecs with sendmmsg() and recvmmsg(), but "ret" is overwritten and the error is lost.
So, in the end, and as it is done in kernel, the best is to loop around sendmsg()... and this is what the existing code does.
Thanks,
Laurent
PATCH 2/3 linux-user: Fix 'recvmmsg()' implementation
In reply to Filip BozutaSyscall 'recvmmsg()' takes an argument which is of type 'struct timespec' that represents a timeout for the receive operation. Before changes from this patch, the timeout argument was ignored. This was probably due to the way 'recvmmsg()' was implemented with looping over 'recvmsg()' which doesn't have the timeout argument. This was changed with the previous patch in this series and which is why the timeout argument should be added accordingly.
Implementation notes:
Function 'do_sendrecvmmsg()' was changed with the addition of a new argument which represents the timeout. This argument is only passed in case of 'TARGET_NR_recvmmsg' and for 'TARGENT_NR_sendmmsg' 0 is passed. Function 'do_sendrecvmmsg()' was also updated accordingly in 'do_socketcall()' for 'TARGET_SYS_recvmmsg' and 'TARGET_SYS_sendmmsg'.
--- linux-user/syscall.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-)
linux-user/syscall.c +15 -7
@@ -3217,10 +3217,11 @@ static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,static abi_long do_sendrecvmmsg (int fd,abi_ulong target_msgvec_addr,unsigned int vlen,unsigned int flags, -int send) +abi_long timeout,int send) {struct mmsghdr *host_msgvec;struct target_mmsghdr *target_msgvec; +struct timespec ts;abi_long ret =0 ;int num_sentrecv =0 ;int num_iovec =0 ;
@@ -3275,7 +3276,14 @@ static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec_addr,if (send) { ret =get_errno (safe_sendmmsg (fd, host_msgvec, i, flags)); }else { - ret =get_errno (safe_recvmmsg (fd, host_msgvec, i, flags,NULL )); +if (timeout) { +if (target_to_host_timespec (&ts, timeout)) { +return -TARGET_EFAULT ; + } + ret =get_errno (safe_recvmmsg (fd, host_msgvec, i, flags, &ts)); + }else { + ret =get_errno (safe_recvmmsg (fd, host_msgvec, i, flags,NULL )); + } } }
@@ -3611,10 +3619,10 @@ static abi_long do_socketcall(int num, abi_ulong vptr)return do_sendrecvmsg (a[0 ], a[1 ], a[2 ],0 );case TARGET_SYS_ACCEPT4 :/* sockfd, addr, addrlen, flags */ return do_accept4 (a[0 ], a[1 ], a[2 ], a[3 ]); -case TARGET_SYS_RECVMMSG :/* sockfd, msgvec, vlen, flags */ -return do_sendrecvmmsg (a[0 ], a[1 ], a[2 ], a[3 ],0 ); +case TARGET_SYS_RECVMMSG :/* sockfd, msgvec, vlen, timeout, flags */ +return do_sendrecvmmsg (a[0 ], a[1 ], a[2 ], a[3 ], a[4 ],0 );case TARGET_SYS_SENDMMSG :/* sockfd, msgvec, vlen, flags */ -return do_sendrecvmmsg (a[0 ], a[1 ], a[2 ], a[3 ],1 ); +return do_sendrecvmmsg (a[0 ], a[1 ], a[2 ], a[3 ],0 ,1 );default :qemu_log_mask (LOG_UNIMP ,"Unsupported socketcall: %d\n" , num);return -TARGET_EINVAL ;
@@ -9418,11 +9426,11 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,#endif #ifdef TARGET_NR_sendmmsgcase TARGET_NR_sendmmsg: -return do_sendrecvmmsg (arg1, arg2, arg3, arg4,1 ); +return do_sendrecvmmsg (arg1, arg2, arg3, arg4,0 ,1 );#endif #ifdef TARGET_NR_recvmmsgcase TARGET_NR_recvmmsg: -return do_sendrecvmmsg (arg1, arg2, arg3, arg4,0 ); +return do_sendrecvmmsg (arg1, arg2, arg3, arg4, arg5,0 );#endif #ifdef TARGET_NR_sendtocase TARGET_NR_sendto:
Signature · 1 line
2.25.1
PATCH 3/3 linux-user: Add support for 'recvmmsg_time64()'
In reply to Filip BozutaThis patch introduces functionality for syscall:
*recvmmsg_time64
This syscall is a 2038 safe variant for syscall:
int recvmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen,
int flags, struct timespec *timeout)
--receive multiple messages on a socket--
man page: https://man7.org/linux/man-pages/man2/recvmmsg.2.html
Implementation notes:
Function 'do_sendrecvmmsg()' in 'syscall.c' was changed with the addition of a new argument 'time64' which represents a flag by which the function knows what kind of 'struct timespec' converting function to call ('target_to_host_timespec() or 'target_to_host_timespec64()'). It is 0 in case of 'TARGET_NR_recvmmsg' and 1 in case of 'TARGET_NR_recvmmsg_time64'.
In 'do_socketcall()', the 'TARGET_ABI_BITS' was checked to know what value for 'time64' argument to pass (0 if 'TARGET_ABI_BITS == 32' and 1 otherwise).
--- linux-user/syscall.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-)
linux-user/syscall.c +18 -7
@@ -3217,7 +3217,7 @@ static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,static abi_long do_sendrecvmmsg (int fd,abi_ulong target_msgvec_addr,unsigned int vlen,unsigned int flags, -abi_long timeout,int send) +abi_long timeout,int time64,int send) {struct mmsghdr *host_msgvec;struct target_mmsghdr *target_msgvec;
@@ -3277,7 +3277,10 @@ static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec_addr, ret =get_errno (safe_sendmmsg (fd, host_msgvec, i, flags)); }else {if (timeout) { -if (target_to_host_timespec (&ts, timeout)) { +if (!time64 &&target_to_host_timespec (&ts, timeout)) { +return -TARGET_EFAULT ; + } +if (time64 &&target_to_host_timespec64 (&ts, timeout)) {return -TARGET_EFAULT ; } ret =get_errno (safe_recvmmsg (fd, host_msgvec, i, flags, &ts));
@@ -3619,10 +3622,14 @@ static abi_long do_socketcall(int num, abi_ulong vptr)return do_sendrecvmsg (a[0 ], a[1 ], a[2 ],0 );case TARGET_SYS_ACCEPT4 :/* sockfd, addr, addrlen, flags */ return do_accept4 (a[0 ], a[1 ], a[2 ], a[3 ]); -case TARGET_SYS_RECVMMSG :/* sockfd, msgvec, vlen, timeout, flags */ -return do_sendrecvmmsg (a[0 ], a[1 ], a[2 ], a[3 ], a[4 ],0 ); +case TARGET_SYS_RECVMMSG :/* sockfd, msgvec, vlen, flags, timeout, */ +#if TARGET_ABI_BITS ==32 +return do_sendrecvmmsg (a[0 ], a[1 ], a[2 ], a[3 ], a[4 ],0 ,0 ); +#else +return do_sendrecvmmsg (a[0 ], a[1 ], a[2 ], a[3 ], a[4 ],1 ,0 ); +#endif case TARGET_SYS_SENDMMSG :/* sockfd, msgvec, vlen, flags */ -return do_sendrecvmmsg (a[0 ], a[1 ], a[2 ], a[3 ],0 ,1 ); +return do_sendrecvmmsg (a[0 ], a[1 ], a[2 ], a[3 ],0 ,0 ,1 );default :qemu_log_mask (LOG_UNIMP ,"Unsupported socketcall: %d\n" , num);return -TARGET_EINVAL ;
@@ -9426,11 +9433,15 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,#endif #ifdef TARGET_NR_sendmmsgcase TARGET_NR_sendmmsg: -return do_sendrecvmmsg (arg1, arg2, arg3, arg4,0 ,1 ); +return do_sendrecvmmsg (arg1, arg2, arg3, arg4,0 ,0 ,1 );#endif #ifdef TARGET_NR_recvmmsgcase TARGET_NR_recvmmsg: -return do_sendrecvmmsg (arg1, arg2, arg3, arg4, arg5,0 ); +return do_sendrecvmmsg (arg1, arg2, arg3, arg4, arg5,0 ,0 ); +#endif +#ifdef TARGET_NR_recvmmsg_time64 +case TARGET_NR_recvmmsg_time64: +return do_sendrecvmmsg (arg1, arg2, arg3, arg4, arg5,1 ,0 );#endif #ifdef TARGET_NR_sendtocase TARGET_NR_sendto:
Signature · 1 line
2.25.1