On Tue, Aug 30 2022, Moritz Buhl <[email protected]> wrote:
> Hi tech@,
>
> the following diff only contains recvmmsg which should be the more useful
> syscall of the two.
>
> I implemented some minor feedback regarding the man page and attaching
> an error from recvit to the socket in case some messages were
> received before.
>
> I am also looking into passing the timeout through recvit and
> soreceive in order to not block indefinetly on a blocking socket
> as the other implementations do:
>
> BUGS
>
>        The timeout argument does not work as intended.  The timeout is
>        checked only after the receipt of each datagram, so that if up to
>        vlen-1 datagrams are received before the timeout expires, but
>        then no further datagrams are received, the call will block
>        forever.
> https://www.man7.org/linux/man-pages/man2/recvmmsg.2.html
>
> But I would prefer doing this in another change.

As discussed with Moritz, ports/net/knot (which I co-maintain) expects
to be able to use both recvmmsg and sendmmsg.  I didn't check other
ports but from the code we read, using both recvmmsg and sendmmsg looks
legit (useful).

And since Moritz insists, please find my bikeshedding below. :)
tl;dr nothing looks wrong to me but I haven't tested it yet.

[...]

> Index: sys/kern/uipc_syscalls.c
> ===================================================================
> RCS file: /cvs/src/sys/kern/uipc_syscalls.c,v
> retrieving revision 1.201
> diff -u -p -r1.201 uipc_syscalls.c
> --- sys/kern/uipc_syscalls.c  14 Aug 2022 01:58:28 -0000      1.201
> +++ sys/kern/uipc_syscalls.c  30 Aug 2022 17:03:09 -0000
> @@ -805,6 +805,135 @@ done:
>  }
>  
>  int
> +sys_recvmmsg(struct proc *p, void *v, register_t *retval)
> +{
> +     struct sys_recvmmsg_args /* {
> +             syscallarg(int)                 s;
> +             syscallarg(struct mmsghdr *)    mmsg;
> +             syscallarg(unsigned int)        vlen;
> +             syscallarg(unsigned int)        flags;
> +             syscallarg(struct timespec *)   timeout;
> +     } */ *uap = v;
> +     struct mmsghdr mmsg;
> +     struct timespec ts, now;
> +     struct iovec aiov[UIO_SMALLIOV], *uiov, *iov = aiov;
> +     struct file *fp;
> +     struct socket *so;
> +     struct timespec *timeout;
> +     unsigned int vlen, dg;

"dg" is a weird name.  At first it looks like an index, I would name it
idx but if you want to keep the original name maybe put it on its own
line and add a comment like /* number of datagrams received */?

> +     int error = 0, flags, s;
> +
> +     timeout = SCARG(uap, timeout);
> +     if (timeout != NULL) {
> +             error = copyin(SCARG(uap, timeout), &ts, sizeof(ts));
                               ^^^^^^^^^^^^^^^^^^^ could reuse the
                               timeout local variable

> +             if (error != 0)
The local idiom is
                if (error)
> +                     return error;

There are other occurrences below which you may or may not address if
you prefer to limit the differences with NetBSD.

> +             getnanotime(&now);
> +             timespecadd(&now, &ts, &ts);
> +     }
> +
> +     s = SCARG(uap, s);
> +     if ((error = getsock(p, s, &fp)) != 0)
> +             return (error);
> +     so = (struct socket *)fp->f_data;
> +
> +     flags = SCARG(uap, flags);
> +
> +     vlen = SCARG(uap, vlen);

Maybe add a comment?
        /* Arbitrarily capped at 1024 items. */
> +     if (vlen > 1024)
> +             vlen = 1024;
> +
> +     for (dg = 0; dg < vlen;) {
> +             error = copyin(SCARG(uap, mmsg) + dg, &mmsg, sizeof(mmsg));

We could have a struct mmsghdr *mmsgp local pointer and use mmsgp[idx]
instead of a mix of a macro and pointer arithmetics.

> +             if (error != 0)
> +                     break;
> +
> +             if (mmsg.msg_hdr.msg_iovlen > IOV_MAX) {
> +                     error = EMSGSIZE;
> +                     break;
> +             }
> +
> +             if (mmsg.msg_hdr.msg_iovlen > UIO_SMALLIOV)
> +                     iov = mallocarray(mmsg.msg_hdr.msg_iovlen,
> +                         sizeof(struct iovec), M_IOV, M_WAITOK);
> +             else
> +                     iov = aiov;
> +
> +             if (mmsg.msg_hdr.msg_iovlen > 0) {
> +                     error = copyin(mmsg.msg_hdr.msg_iov, iov,
> +                         mmsg.msg_hdr.msg_iovlen * sizeof(struct iovec));
> +                     if (error)
> +                             break;
> +             }
> +
> +             uiov = mmsg.msg_hdr.msg_iov;
> +             mmsg.msg_hdr.msg_iov = iov;
> +             mmsg.msg_hdr.msg_flags = flags;
> +
> +             error = recvit(p, s, &mmsg.msg_hdr, NULL, retval);
> +             if (error != 0) {
> +                     if (error == EAGAIN && dg > 0)
> +                             error = 0;
> +                     break;
> +             }
> +
> +             if (dg == 0 && flags & MSG_WAITFORONE) {
> +                     flags &= ~MSG_WAITFORONE;
> +                     flags |= MSG_DONTWAIT;
> +             }
> +
> +             mmsg.msg_hdr.msg_iov = uiov;
> +             mmsg.msg_len = *retval;
> +#ifdef KTRACE
> +             if (KTRPOINT(p, KTR_STRUCT)) {
> +                     ktrmsghdr(p, &mmsg.msg_hdr);
> +                     if (mmsg.msg_hdr.msg_iovlen)
> +                             ktriovec(p, iov, mmsg.msg_hdr.msg_iovlen);
> +             }
> +#endif
> +
> +             error = copyout(&mmsg, SCARG(uap, mmsg) + dg, sizeof(mmsg));

Same array indexing hidden behind pointer arithmetics.

> +             if (error != 0)
> +                     break;
> +
> +             if (iov != aiov) {
> +                     free(iov, M_IOV, sizeof(struct iovec) *
> +                         mmsg.msg_hdr.msg_iovlen);
> +                     iov = aiov;
> +             }
> +
> +             dg++;
> +             if (mmsg.msg_hdr.msg_flags & MSG_OOB)
> +                     break;
> +
> +             if (timeout != NULL) {
> +                     getnanotime(&now);
> +                     timespecsub(&now, &ts, &now);
> +                     if (now.tv_sec > 0)
> +                             break;
> +             }
> +     }
> +
> +     *retval = dg;
> +
> +     /*
> +      * If we succeeded at least once, return 0, hopefully so->so_error
> +      * will catch it next time.
> +      */
> +     if (error && dg > 0) {
> +             so->so_error = error;
> +             error = 0;
> +     }
> +
> +     if (iov != aiov)
> +             free(iov, M_IOV,
> +                 sizeof(struct iovec) * mmsg.msg_hdr.msg_iovlen);
> +
> +     FRELE(fp, p);
> +     return (error);
> +}
> +
> +int
>  recvit(struct proc *p, int s, struct msghdr *mp, caddr_t namelenp,
>      register_t *retsize)
>  {

-- 
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE

Reply via email to