[PATCH] Receive multiple packets at a time

Michael Tokarev
Thu, 10 Dec 2015 06:20:26 -0000

10.12.2015 03:35, Samuel Thibault wrote:
[]

I suggest reducing ifdeffery in handle_incoming_vpn_data(), especially
the error checking code.

The function isn't that large now, it might be much better to have
two different implementations.  Like this (untested, patch attached):

void handle_incoming_vpn_data(int sock) {

#ifdef HAVE_RECVMMSG
#define MAX_MSG  256

        vpn_packet_t pkt[MAX_MSG];
        sockaddr_t from[MAX_MSG];
        struct mmsghdr msg[MAX_MSG];
        struct iovec iov[MAX_MSG];
        int num = 1, i;

        for(i = 0; i < MAX_MSG; i++)
        {
                msg[i].msg_hdr.msg_name = &from[i].sa;
                msg[i].msg_hdr.msg_namelen = sizeof(from[i]);
                iov[i].iov_base = &pkt[i].seqno;
                iov[i].iov_len = MAXSIZE;
                msg[i].msg_hdr.msg_iov = &iov[i];
                msg[i].msg_hdr.msg_iovlen = 1;
                msg[i].msg_hdr.msg_control = NULL;
                msg[i].msg_hdr.msg_controllen = 0;
        }
        num = recvmmsg(listen_socket[sock].udp, msg, MAX_MSG, MSG_DONTWAIT,
NULL);

        if(num < 0)
        {
                if(!sockwouldblock(sockerrno))
                        logger(LOG_ERR, "Receiving packet failed: %s",
sockstrerror(sockerrno));
                return;
        }

        for(i = 0; i < num; i++)
        {
                pkt[i].len = msg[i].msg_len;
                if(pkt[i].len <= 0 || pkt[i].len > MAXSIZE)
                        continue;
                handle_incoming_vpn_packet(sock, &pkt[i], &from[i]);
        }

#else

        vpn_packet_t pkt;
        sockaddr_t from;
        socklen_t fromlen = sizeof(from);

        pkt.len = recvfrom(listen_socket[sock].udp, (char *) &pkt.seqno,
MAXSIZE, 0, &from.sa, &fromlen);

        if(pkt.len < 0)
        {
                if(!sockwouldblock(sockerrno))
                        logger(LOG_ERR, "Receiving packet failed: %s",
sockstrerror(sockerrno));
                return;
        }

        handle_incoming_vpn_packet(sock, &pkt, &from);

#endif
}

/mjt
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mrecv.patch
Type: text/x-patch
Size: 2858 bytes
Desc: not available
URL:
<http://www.tinc-vpn.org/pipermail/tinc-devel/attachments/20151210/28885d0e/attachment.bin>