Blame man2/recvmmsg.2

Packit 7cfc04
.\" Copyright (C) 2011 by Andi Kleen <andi@firstfloor.org>
Packit 7cfc04
.\" and Copyright (c) 2011 by Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" Syscall added in following commit
Packit 7cfc04
.\"	commit a2e2725541fad72416326798c2d7fa4dafb7d337
Packit 7cfc04
.\"	Author: Arnaldo Carvalho de Melo <acme@redhat.com>
Packit 7cfc04
.\"	Date:   Mon Oct 12 23:40:10 2009 -0700
Packit 7cfc04
.\"
Packit 7cfc04
.TH RECVMMSG 2 2018-02-02 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
recvmmsg \- receive multiple messages on a socket
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.BR "#define _GNU_SOURCE" "         /* See feature_test_macros(7) */"
Packit 7cfc04
.BI "#include <sys/socket.h>"
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int recvmmsg(int " sockfd ", struct mmsghdr *" msgvec \
Packit 7cfc04
", unsigned int " vlen ","
Packit 7cfc04
.BI "             int " flags ", struct timespec *" timeout ");"
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The
Packit 7cfc04
.BR recvmmsg ()
Packit 7cfc04
system call is an extension of
Packit 7cfc04
.BR recvmsg (2)
Packit 7cfc04
that allows the caller to receive multiple messages from a socket
Packit 7cfc04
using a single system call.
Packit 7cfc04
(This has performance benefits for some applications.)
Packit 7cfc04
A further extension over
Packit 7cfc04
.BR recvmsg (2)
Packit 7cfc04
is support for a timeout on the receive operation.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I sockfd
Packit 7cfc04
argument is the file descriptor of the socket to receive data from.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I msgvec
Packit 7cfc04
argument is a pointer to an array of
Packit 7cfc04
.I mmsghdr
Packit 7cfc04
structures.
Packit 7cfc04
The size of this array is specified in
Packit 7cfc04
.IR vlen .
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I mmsghdr
Packit 7cfc04
structure is defined in
Packit 7cfc04
.I <sys/socket.h>
Packit 7cfc04
as:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct mmsghdr {
Packit 7cfc04
    struct msghdr msg_hdr;  /* Message header */
Packit 7cfc04
    unsigned int  msg_len;  /* Number of received bytes for header */
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I msg_hdr
Packit 7cfc04
field is a
Packit 7cfc04
.I msghdr
Packit 7cfc04
structure, as described in
Packit 7cfc04
.BR recvmsg (2).
Packit 7cfc04
The
Packit 7cfc04
.I msg_len
Packit 7cfc04
field is the number of bytes returned for the message in the entry.
Packit 7cfc04
This field has the same value as the return value of a single
Packit 7cfc04
.BR recvmsg (2)
Packit 7cfc04
on the header.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I flags
Packit 7cfc04
argument contains flags ORed together.
Packit 7cfc04
The flags are the same as documented for
Packit 7cfc04
.BR recvmsg (2),
Packit 7cfc04
with the following addition:
Packit 7cfc04
.TP
Packit 7cfc04
.BR MSG_WAITFORONE " (since Linux 2.6.34)"
Packit 7cfc04
Turns on
Packit 7cfc04
.B MSG_DONTWAIT
Packit 7cfc04
after the first message has been received.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I timeout
Packit 7cfc04
argument points to a
Packit 7cfc04
.I struct timespec
Packit 7cfc04
(see
Packit 7cfc04
.BR clock_gettime (2))
Packit 7cfc04
defining a timeout (seconds plus nanoseconds) for the receive operation
Packit 7cfc04
.RI ( "but see BUGS!" ).
Packit 7cfc04
(This interval will be rounded up to the system clock granularity,
Packit 7cfc04
and kernel scheduling delays mean that the blocking interval
Packit 7cfc04
may overrun by a small amount.)
Packit 7cfc04
If
Packit 7cfc04
.I timeout
Packit 7cfc04
is NULL, then the operation blocks indefinitely.
Packit 7cfc04
.PP
Packit 7cfc04
A blocking
Packit 7cfc04
.BR recvmmsg ()
Packit 7cfc04
call blocks until
Packit 7cfc04
.I vlen
Packit 7cfc04
messages have been received
Packit 7cfc04
or until the timeout expires.
Packit 7cfc04
A nonblocking call reads as many messages as are available
Packit 7cfc04
(up to the limit specified by
Packit 7cfc04
.IR vlen )
Packit 7cfc04
and returns immediately.
Packit 7cfc04
.PP
Packit 7cfc04
On return from
Packit 7cfc04
.BR recvmmsg (),
Packit 7cfc04
successive elements of
Packit 7cfc04
.IR msgvec
Packit 7cfc04
are updated to contain information about each received message:
Packit 7cfc04
.I msg_len
Packit 7cfc04
contains the size of the received message;
Packit 7cfc04
the subfields of
Packit 7cfc04
.I msg_hdr
Packit 7cfc04
are updated as described in
Packit 7cfc04
.BR recvmsg (2).
Packit 7cfc04
The return value of the call indicates the number of elements of
Packit 7cfc04
.I msgvec
Packit 7cfc04
that have been updated.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
On success,
Packit 7cfc04
.BR recvmmsg ()
Packit 7cfc04
returns the number of messages received in
Packit 7cfc04
.IR msgvec ;
Packit 7cfc04
on error, \-1 is returned, and
Packit 7cfc04
.I errno
Packit 7cfc04
is set to indicate the error.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
Errors are as for
Packit 7cfc04
.BR recvmsg (2).
Packit 7cfc04
In addition, the following error can occur:
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
.I timeout
Packit 7cfc04
is invalid.
Packit 7cfc04
.PP
Packit 7cfc04
See also BUGS.
Packit 7cfc04
.SH VERSIONS
Packit 7cfc04
The
Packit 7cfc04
.BR recvmmsg ()
Packit 7cfc04
system call was added in Linux 2.6.33.
Packit 7cfc04
Support in glibc was added in version 2.12.
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
.BR recvmmsg ()
Packit 7cfc04
is Linux-specific.
Packit 7cfc04
.SH BUGS
Packit 7cfc04
The
Packit 7cfc04
.I timeout
Packit 7cfc04
argument does not work as intended.
Packit 7cfc04
.\" FIXME . https://bugzilla.kernel.org/show_bug.cgi?id=75371
Packit 7cfc04
.\" http://thread.gmane.org/gmane.linux.man/5677
Packit 7cfc04
The timeout is checked only after the receipt of each datagram,
Packit 7cfc04
so that if up to
Packit 7cfc04
.I vlen\-1
Packit 7cfc04
datagrams are received before the timeout expires,
Packit 7cfc04
but then no further datagrams are received, the call will block forever.
Packit 7cfc04
.PP
Packit 7cfc04
If an error occurs after at least one message has been received,
Packit 7cfc04
the call succeeds, and returns the number of messages received.
Packit 7cfc04
The error code is expected to be returned on a subsequent call to
Packit 7cfc04
.BR recvmmsq ().
Packit 7cfc04
In the current implementation, however, the error code can be overwritten
Packit 7cfc04
in the meantime by an unrelated network event on a socket,
Packit 7cfc04
for example an incoming ICMP packet.
Packit 7cfc04
.SH EXAMPLE
Packit 7cfc04
.PP
Packit 7cfc04
The following program uses
Packit 7cfc04
.BR recvmmsg ()
Packit 7cfc04
to receive multiple messages on a socket and stores
Packit 7cfc04
them in multiple buffers.
Packit 7cfc04
The call returns if all buffers are filled or if the
Packit 7cfc04
timeout specified has expired.
Packit 7cfc04
.PP
Packit 7cfc04
The following snippet periodically generates UDP datagrams
Packit 7cfc04
containing a random number:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
.RB "$" " while true; do echo $RANDOM > /dev/udp/127.0.0.1/1234; "
Packit 7cfc04
.B      "      sleep 0.25; done"
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
These datagrams are read by the example application, which
Packit 7cfc04
can give the following output:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
.RB "$" " ./a.out"
Packit 7cfc04
5 messages received
Packit 7cfc04
1 11782
Packit 7cfc04
2 11345
Packit 7cfc04
3 304
Packit 7cfc04
4 13514
Packit 7cfc04
5 28421
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.SS Program source
Packit 7cfc04
\&
Packit 7cfc04
.EX
Packit 7cfc04
#define _GNU_SOURCE
Packit 7cfc04
#include <netinet/ip.h>
Packit 7cfc04
#include <stdio.h>
Packit 7cfc04
#include <stdlib.h>
Packit 7cfc04
#include <string.h>
Packit 7cfc04
#include <sys/socket.h>
Packit 7cfc04
Packit 7cfc04
int
Packit 7cfc04
main(void)
Packit 7cfc04
{
Packit 7cfc04
#define VLEN 10
Packit 7cfc04
#define BUFSIZE 200
Packit 7cfc04
#define TIMEOUT 1
Packit 7cfc04
    int sockfd, retval, i;
Packit 7cfc04
    struct sockaddr_in addr;
Packit 7cfc04
    struct mmsghdr msgs[VLEN];
Packit 7cfc04
    struct iovec iovecs[VLEN];
Packit 7cfc04
    char bufs[VLEN][BUFSIZE+1];
Packit 7cfc04
    struct timespec timeout;
Packit 7cfc04
Packit 7cfc04
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
Packit 7cfc04
    if (sockfd == \-1) {
Packit 7cfc04
        perror("socket()");
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    addr.sin_family = AF_INET;
Packit 7cfc04
    addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
Packit 7cfc04
    addr.sin_port = htons(1234);
Packit 7cfc04
    if (bind(sockfd, (struct sockaddr *) &addr, sizeof(addr)) == \-1) {
Packit 7cfc04
        perror("bind()");
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    memset(msgs, 0, sizeof(msgs));
Packit 7cfc04
    for (i = 0; i < VLEN; i++) {
Packit 7cfc04
        iovecs[i].iov_base         = bufs[i];
Packit 7cfc04
        iovecs[i].iov_len          = BUFSIZE;
Packit 7cfc04
        msgs[i].msg_hdr.msg_iov    = &iovecs[i];
Packit 7cfc04
        msgs[i].msg_hdr.msg_iovlen = 1;
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    timeout.tv_sec = TIMEOUT;
Packit 7cfc04
    timeout.tv_nsec = 0;
Packit 7cfc04
Packit 7cfc04
    retval = recvmmsg(sockfd, msgs, VLEN, 0, &timeout);
Packit 7cfc04
    if (retval == \-1) {
Packit 7cfc04
        perror("recvmmsg()");
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    printf("%d messages received\\n", retval);
Packit 7cfc04
    for (i = 0; i < retval; i++) {
Packit 7cfc04
        bufs[i][msgs[i].msg_len] = 0;
Packit 7cfc04
        printf("%d %s", i+1, bufs[i]);
Packit 7cfc04
    }
Packit 7cfc04
    exit(EXIT_SUCCESS);
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR clock_gettime (2),
Packit 7cfc04
.BR recvmsg (2),
Packit 7cfc04
.BR sendmmsg (2),
Packit 7cfc04
.BR sendmsg (2),
Packit 7cfc04
.BR socket (2),
Packit 7cfc04
.BR socket (7)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.