Blame man7/socket.7

Packit 7cfc04
'\" t
Packit 7cfc04
.\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
Packit 7cfc04
.\" and copyright (c) 1999 Matthew Wilcox.
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM_ONE_PARA)
Packit 7cfc04
.\" Permission is granted to distribute possibly modified copies
Packit 7cfc04
.\" of this page provided the header is included verbatim,
Packit 7cfc04
.\" and in case of nontrivial modification author and date
Packit 7cfc04
.\" of the modification is added to the header.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" 2002-10-30, Michael Kerrisk, <mtk.manpages@gmail.com>
Packit 7cfc04
.\"	Added description of SO_ACCEPTCONN
Packit 7cfc04
.\" 2004-05-20, aeb, added SO_RCVTIMEO/SO_SNDTIMEO text.
Packit 7cfc04
.\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"     Added notes on capability requirements
Packit 7cfc04
.\"	A few small grammar fixes
Packit 7cfc04
.\" 2010-06-13 Jan Engelhardt <jengelh@medozas.de>
Packit 7cfc04
.\"	Documented SO_DOMAIN and SO_PROTOCOL.
Packit 7cfc04
.\"
Packit 7cfc04
.\" FIXME
Packit 7cfc04
.\" The following are not yet documented:
Packit 7cfc04
.\"
Packit 7cfc04
.\" SO_PEERNAME (2.4?)
Packit 7cfc04
.\"	get only
Packit 7cfc04
.\"	Seems to do something similar to getpeername(), but then
Packit 7cfc04
.\"	why is it necessary / how does it differ?
Packit 7cfc04
.\"
Packit 7cfc04
.\" SO_TIMESTAMPNS (2.6.22)
Packit 7cfc04
.\"	Documentation/networking/timestamping.txt
Packit 7cfc04
.\"	commit 92f37fd2ee805aa77925c1e64fd56088b46094fc
Packit 7cfc04
.\"	Author: Eric Dumazet <dada1@cosmosbay.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" SO_TIMESTAMPING (2.6.30)
Packit 7cfc04
.\"	Documentation/networking/timestamping.txt
Packit 7cfc04
.\"	commit cb9eff097831007afb30d64373f29d99825d0068
Packit 7cfc04
.\"	Author: Patrick Ohly <patrick.ohly@intel.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\"  SO_WIFI_STATUS (3.3)
Packit 7cfc04
.\"	commit 6e3e939f3b1bf8534b32ad09ff199d88800835a0
Packit 7cfc04
.\"	Author: Johannes Berg <johannes.berg@intel.com>
Packit 7cfc04
.\"	Also: SCM_WIFI_STATUS
Packit 7cfc04
.\"
Packit 7cfc04
.\" SO_NOFCS (3.4)
Packit 7cfc04
.\"	commit 3bdc0eba0b8b47797f4a76e377dd8360f317450f
Packit 7cfc04
.\"	Author: Ben Greear <greearb@candelatech.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\"  SO_GET_FILTER (3.8)
Packit 7cfc04
.\"	commit a8fc92778080c845eaadc369a0ecf5699a03bef0
Packit 7cfc04
.\"	Author: Pavel Emelyanov <xemul@parallels.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" SO_SELECT_ERR_QUEUE (3.10)
Packit 7cfc04
.\"	commit 7d4c04fc170087119727119074e72445f2bb192b
Packit 7cfc04
.\"	Author: Keller, Jacob E <jacob.e.keller@intel.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" SO_MAX_PACING_RATE (3.13)
Packit 7cfc04
.\"	commit 62748f32d501f5d3712a7c372bbb92abc7c62bc7
Packit 7cfc04
.\"	Author: Eric Dumazet <edumazet@google.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" SO_BPF_EXTENSIONS (3.14)
Packit 7cfc04
.\"	commit ea02f9411d9faa3553ed09ce0ec9f00ceae9885e
Packit 7cfc04
.\"	Author: Michal Sekletar <msekleta@redhat.com>
Packit 7cfc04
.\"
Packit 7cfc04
.TH SOCKET 7 2018-02-02 Linux "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
socket \- Linux socket interface
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.B #include <sys/socket.h>
Packit 7cfc04
.PP
Packit 7cfc04
.IB sockfd " = socket(int " socket_family ", int " socket_type ", int " protocol );
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
This manual page describes the Linux networking socket layer user
Packit 7cfc04
interface.
Packit 7cfc04
The BSD compatible sockets
Packit 7cfc04
are the uniform interface
Packit 7cfc04
between the user process and the network protocol stacks in the kernel.
Packit 7cfc04
The protocol modules are grouped into
Packit 7cfc04
.I protocol families
Packit 7cfc04
such as
Packit 7cfc04
.BR AF_INET ", " AF_IPX ", and " AF_PACKET ,
Packit 7cfc04
and
Packit 7cfc04
.I socket types
Packit 7cfc04
such as
Packit 7cfc04
.B SOCK_STREAM
Packit 7cfc04
or
Packit 7cfc04
.BR SOCK_DGRAM .
Packit 7cfc04
See
Packit 7cfc04
.BR socket (2)
Packit 7cfc04
for more information on families and types.
Packit 7cfc04
.SS Socket-layer functions
Packit 7cfc04
These functions are used by the user process to send or receive packets
Packit 7cfc04
and to do other socket operations.
Packit 7cfc04
For more information see their respective manual pages.
Packit 7cfc04
.PP
Packit 7cfc04
.BR socket (2)
Packit 7cfc04
creates a socket,
Packit 7cfc04
.BR connect (2)
Packit 7cfc04
connects a socket to a remote socket address,
Packit 7cfc04
the
Packit 7cfc04
.BR bind (2)
Packit 7cfc04
function binds a socket to a local socket address,
Packit 7cfc04
.BR listen (2)
Packit 7cfc04
tells the socket that new connections shall be accepted, and
Packit 7cfc04
.BR accept (2)
Packit 7cfc04
is used to get a new socket with a new incoming connection.
Packit 7cfc04
.BR socketpair (2)
Packit 7cfc04
returns two connected anonymous sockets (implemented only for a few
Packit 7cfc04
local families like
Packit 7cfc04
.BR AF_UNIX )
Packit 7cfc04
.PP
Packit 7cfc04
.BR send (2),
Packit 7cfc04
.BR sendto (2),
Packit 7cfc04
and
Packit 7cfc04
.BR sendmsg (2)
Packit 7cfc04
send data over a socket, and
Packit 7cfc04
.BR recv (2),
Packit 7cfc04
.BR recvfrom (2),
Packit 7cfc04
.BR recvmsg (2)
Packit 7cfc04
receive data from a socket.
Packit 7cfc04
.BR poll (2)
Packit 7cfc04
and
Packit 7cfc04
.BR select (2)
Packit 7cfc04
wait for arriving data or a readiness to send data.
Packit 7cfc04
In addition, the standard I/O operations like
Packit 7cfc04
.BR write (2),
Packit 7cfc04
.BR writev (2),
Packit 7cfc04
.BR sendfile (2),
Packit 7cfc04
.BR read (2),
Packit 7cfc04
and
Packit 7cfc04
.BR readv (2)
Packit 7cfc04
can be used to read and write data.
Packit 7cfc04
.PP
Packit 7cfc04
.BR getsockname (2)
Packit 7cfc04
returns the local socket address and
Packit 7cfc04
.BR getpeername (2)
Packit 7cfc04
returns the remote socket address.
Packit 7cfc04
.BR getsockopt (2)
Packit 7cfc04
and
Packit 7cfc04
.BR setsockopt (2)
Packit 7cfc04
are used to set or get socket layer or protocol options.
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
can be used to set or read some other options.
Packit 7cfc04
.PP
Packit 7cfc04
.BR close (2)
Packit 7cfc04
is used to close a socket.
Packit 7cfc04
.BR shutdown (2)
Packit 7cfc04
closes parts of a full-duplex socket connection.
Packit 7cfc04
.PP
Packit 7cfc04
Seeking, or calling
Packit 7cfc04
.BR pread (2)
Packit 7cfc04
or
Packit 7cfc04
.BR pwrite (2)
Packit 7cfc04
with a nonzero position is not supported on sockets.
Packit 7cfc04
.PP
Packit 7cfc04
It is possible to do nonblocking I/O on sockets by setting the
Packit 7cfc04
.B O_NONBLOCK
Packit 7cfc04
flag on a socket file descriptor using
Packit 7cfc04
.BR fcntl (2).
Packit 7cfc04
Then all operations that would block will (usually)
Packit 7cfc04
return with
Packit 7cfc04
.B EAGAIN
Packit 7cfc04
(operation should be retried later);
Packit 7cfc04
.BR connect (2)
Packit 7cfc04
will return
Packit 7cfc04
.B EINPROGRESS
Packit 7cfc04
error.
Packit 7cfc04
The user can then wait for various events via
Packit 7cfc04
.BR poll (2)
Packit 7cfc04
or
Packit 7cfc04
.BR select (2).
Packit 7cfc04
.TS
Packit 7cfc04
tab(:) allbox;
Packit 7cfc04
c s s
Packit 7cfc04
l l l.
Packit 7cfc04
I/O events
Packit 7cfc04
Event:Poll flag:Occurrence
Packit 7cfc04
Read:POLLIN:T{
Packit 7cfc04
New data arrived.
Packit 7cfc04
T}
Packit 7cfc04
Read:POLLIN:T{
Packit 7cfc04
A connection setup has been completed
Packit 7cfc04
(for connection-oriented sockets)
Packit 7cfc04
T}
Packit 7cfc04
Read:POLLHUP:T{
Packit 7cfc04
A disconnection request has been initiated by the other end.
Packit 7cfc04
T}
Packit 7cfc04
Read:POLLHUP:T{
Packit 7cfc04
A connection is broken (only for connection-oriented protocols).
Packit 7cfc04
When the socket is written
Packit 7cfc04
.B SIGPIPE
Packit 7cfc04
is also sent.
Packit 7cfc04
T}
Packit 7cfc04
Write:POLLOUT:T{
Packit 7cfc04
Socket has enough send buffer space for writing new data.
Packit 7cfc04
T}
Packit 7cfc04
Read/Write:T{
Packit 7cfc04
POLLIN |
Packit 7cfc04
.br
Packit 7cfc04
POLLOUT
Packit 7cfc04
T}:T{
Packit 7cfc04
An outgoing
Packit 7cfc04
.BR connect (2)
Packit 7cfc04
finished.
Packit 7cfc04
T}
Packit 7cfc04
Read/Write:POLLERR:An asynchronous error occurred.
Packit 7cfc04
Read/Write:POLLHUP:The other end has shut down one direction.
Packit 7cfc04
Exception:POLLPRI:T{
Packit 7cfc04
Urgent data arrived.
Packit 7cfc04
.B SIGURG
Packit 7cfc04
is sent then.
Packit 7cfc04
T}
Packit 7cfc04
.\" FIXME . The following is not true currently:
Packit 7cfc04
.\" It is no I/O event when the connection
Packit 7cfc04
.\" is broken from the local end using
Packit 7cfc04
.\" .BR shutdown (2)
Packit 7cfc04
.\" or
Packit 7cfc04
.\" .BR close (2).
Packit 7cfc04
.TE
Packit 7cfc04
.PP
Packit 7cfc04
An alternative to
Packit 7cfc04
.BR poll (2)
Packit 7cfc04
and
Packit 7cfc04
.BR select (2)
Packit 7cfc04
is to let the kernel inform the application about events
Packit 7cfc04
via a
Packit 7cfc04
.B SIGIO
Packit 7cfc04
signal.
Packit 7cfc04
For that the
Packit 7cfc04
.B O_ASYNC
Packit 7cfc04
flag must be set on a socket file descriptor via
Packit 7cfc04
.BR fcntl (2)
Packit 7cfc04
and a valid signal handler for
Packit 7cfc04
.B SIGIO
Packit 7cfc04
must be installed via
Packit 7cfc04
.BR sigaction (2).
Packit 7cfc04
See the
Packit 7cfc04
.I Signals
Packit 7cfc04
discussion below.
Packit 7cfc04
.SS Socket address structures
Packit 7cfc04
Each socket domain has its own format for socket addresses,
Packit 7cfc04
with a domain-specific address structure.
Packit 7cfc04
Each of these structures begins with an
Packit 7cfc04
integer "family" field (typed as
Packit 7cfc04
.IR sa_family_t )
Packit 7cfc04
that indicates the type of the address structure.
Packit 7cfc04
This allows
Packit 7cfc04
the various system calls (e.g.,
Packit 7cfc04
.BR connect (2),
Packit 7cfc04
.BR bind (2),
Packit 7cfc04
.BR accept (2),
Packit 7cfc04
.BR getsockname (2),
Packit 7cfc04
.BR getpeername (2)),
Packit 7cfc04
which are generic to all socket domains,
Packit 7cfc04
to determine the domain of a particular socket address.
Packit 7cfc04
.PP
Packit 7cfc04
To allow any type of socket address to be passed to
Packit 7cfc04
interfaces in the sockets API,
Packit 7cfc04
the type
Packit 7cfc04
.IR "struct sockaddr"
Packit 7cfc04
is defined.
Packit 7cfc04
The purpose of this type is purely to allow casting of
Packit 7cfc04
domain-specific socket address types to a "generic" type,
Packit 7cfc04
so as to avoid compiler warnings about type mismatches in
Packit 7cfc04
calls to the sockets API.
Packit 7cfc04
.PP
Packit 7cfc04
In addition, the sockets API provides the data type
Packit 7cfc04
.IR "struct sockaddr_storage".
Packit 7cfc04
This type
Packit 7cfc04
is suitable to accommodate all supported domain-specific socket
Packit 7cfc04
address structures; it is large enough and is aligned properly.
Packit 7cfc04
(In particular, it is large enough to hold
Packit 7cfc04
IPv6 socket addresses.)
Packit 7cfc04
The structure includes the following field, which can be used to identify
Packit 7cfc04
the type of socket address actually stored in the structure:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
    sa_family_t ss_family;
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I sockaddr_storage
Packit 7cfc04
structure is useful in programs that must handle socket addresses
Packit 7cfc04
in a generic way
Packit 7cfc04
(e.g., programs that must deal with both IPv4 and IPv6 socket addresses).
Packit 7cfc04
.SS Socket options
Packit 7cfc04
The socket options listed below can be set by using
Packit 7cfc04
.BR setsockopt (2)
Packit 7cfc04
and read with
Packit 7cfc04
.BR getsockopt (2)
Packit 7cfc04
with the socket level set to
Packit 7cfc04
.B SOL_SOCKET
Packit 7cfc04
for all sockets.
Packit 7cfc04
Unless otherwise noted,
Packit 7cfc04
.I optval
Packit 7cfc04
is a pointer to an
Packit 7cfc04
.IR int .
Packit 7cfc04
.\" FIXME .
Packit 7cfc04
.\" In the list below, the text used to describe argument types
Packit 7cfc04
.\" for each socket option should be more consistent
Packit 7cfc04
.\"
Packit 7cfc04
.\" SO_ACCEPTCONN is in POSIX.1-2001, and its origin is explained in
Packit 7cfc04
.\" W R Stevens, UNPv1
Packit 7cfc04
.TP
Packit 7cfc04
.B SO_ACCEPTCONN
Packit 7cfc04
Returns a value indicating whether or not this socket has been marked
Packit 7cfc04
to accept connections with
Packit 7cfc04
.BR listen (2).
Packit 7cfc04
The value 0 indicates that this is not a listening socket,
Packit 7cfc04
the value 1 indicates that this is a listening socket.
Packit 7cfc04
This socket option is read-only.
Packit 7cfc04
.TP
Packit 7cfc04
.BR SO_ATTACH_FILTER " (since Linux 2.2), " SO_ATTACH_BPF " (since Linux 3.19)"
Packit 7cfc04
Attach a classic BPF
Packit 7cfc04
.RB ( SO_ATTACH_FILTER )
Packit 7cfc04
or an extended BPF
Packit 7cfc04
.RB ( SO_ATTACH_BPF )
Packit 7cfc04
program to the socket for use as a filter of incoming packets.
Packit 7cfc04
A packet will be dropped if the filter program returns zero.
Packit 7cfc04
If the filter program returns a
Packit 7cfc04
nonzero value which is less than the packet's data length,
Packit 7cfc04
the packet will be truncated to the length returned.
Packit 7cfc04
If the value returned by the filter is greater than or equal to the
Packit 7cfc04
packet's data length, the packet is allowed to proceed unmodified.
Packit 7cfc04
.IP
Packit 7cfc04
The argument for
Packit 7cfc04
.BR SO_ATTACH_FILTER
Packit 7cfc04
is a
Packit 7cfc04
.I sock_fprog
Packit 7cfc04
structure, defined in
Packit 7cfc04
.IR <linux/filter.h> :
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct sock_fprog {
Packit 7cfc04
    unsigned short      len;
Packit 7cfc04
    struct sock_filter *filter;
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
The argument for
Packit 7cfc04
.BR SO_ATTACH_BPF
Packit 7cfc04
is a file descriptor returned by the
Packit 7cfc04
.BR bpf (2)
Packit 7cfc04
system call and must refer to a program of type
Packit 7cfc04
.BR BPF_PROG_TYPE_SOCKET_FILTER.
Packit 7cfc04
.IP
Packit 7cfc04
These options may be set multiple times for a given socket,
Packit 7cfc04
each time replacing the previous filter program.
Packit 7cfc04
The classic and extended versions may be called on the same socket,
Packit 7cfc04
but the previous filter will always be replaced such that a socket
Packit 7cfc04
never has more than one filter defined.
Packit 7cfc04
.IP
Packit 7cfc04
Both classic and extended BPF are explained in the kernel source file
Packit 7cfc04
.I Documentation/networking/filter.txt
Packit 7cfc04
.TP
Packit 7cfc04
.BR SO_ATTACH_REUSEPORT_CBPF ", " SO_ATTACH_REUSEPORT_EBPF
Packit 7cfc04
For use with the
Packit 7cfc04
.BR SO_REUSEPORT
Packit 7cfc04
option, these options allow the user to set a classic BPF
Packit 7cfc04
.RB ( SO_ATTACH_REUSEPORT_CBPF )
Packit 7cfc04
or an extended BPF
Packit 7cfc04
.RB ( SO_ATTACH_REUSEPORT_EBPF )
Packit 7cfc04
program which defines how packets are assigned to
Packit 7cfc04
the sockets in the reuseport group (that is, all sockets which have
Packit 7cfc04
.BR SO_REUSEPORT
Packit 7cfc04
set and are using the same local address to receive packets).
Packit 7cfc04
.IP
Packit 7cfc04
The BPF program must return an index between 0 and N\-1 representing
Packit 7cfc04
the socket which should receive the packet
Packit 7cfc04
(where N is the number of sockets in the group).
Packit 7cfc04
If the BPF program returns an invalid index,
Packit 7cfc04
socket selection will fall back to the plain
Packit 7cfc04
.BR SO_REUSEPORT
Packit 7cfc04
mechanism.
Packit 7cfc04
.IP
Packit 7cfc04
Sockets are numbered in the order in which they are added to the group
Packit 7cfc04
(that is, the order of
Packit 7cfc04
.BR bind (2)
Packit 7cfc04
calls for UDP sockets or the order of
Packit 7cfc04
.BR listen (2)
Packit 7cfc04
calls for TCP sockets).
Packit 7cfc04
New sockets added to a reuseport group will inherit the BPF program.
Packit 7cfc04
When a socket is removed from a reuseport group (via
Packit 7cfc04
.BR close (2)),
Packit 7cfc04
the last socket in the group will be moved into the closed socket's
Packit 7cfc04
position.
Packit 7cfc04
.IP
Packit 7cfc04
These options may be set repeatedly at any time on any socket in the group
Packit 7cfc04
to replace the current BPF program used by all sockets in the group.
Packit 7cfc04
.IP
Packit 7cfc04
.BR SO_ATTACH_REUSEPORT_CBPF
Packit 7cfc04
takes the same argument type as
Packit 7cfc04
.BR SO_ATTACH_FILTER
Packit 7cfc04
and
Packit 7cfc04
.BR SO_ATTACH_REUSEPORT_EBPF
Packit 7cfc04
takes the same argument type as
Packit 7cfc04
.BR SO_ATTACH_BPF.
Packit 7cfc04
.IP
Packit 7cfc04
UDP support for this feature is available since Linux 4.5;
Packit 7cfc04
TCP support is available since Linux 4.6.
Packit 7cfc04
.TP
Packit 7cfc04
.B SO_BINDTODEVICE
Packit 7cfc04
Bind this socket to a particular device like \(lqeth0\(rq,
Packit 7cfc04
as specified in the passed interface name.
Packit 7cfc04
If the
Packit 7cfc04
name is an empty string or the option length is zero, the socket device
Packit 7cfc04
binding is removed.
Packit 7cfc04
The passed option is a variable-length null-terminated
Packit 7cfc04
interface name string with the maximum size of
Packit 7cfc04
.BR IFNAMSIZ .
Packit 7cfc04
If a socket is bound to an interface,
Packit 7cfc04
only packets received from that particular interface are processed by the
Packit 7cfc04
socket.
Packit 7cfc04
Note that this works only for some socket types, particularly
Packit 7cfc04
.B AF_INET
Packit 7cfc04
sockets.
Packit 7cfc04
It is not supported for packet sockets (use normal
Packit 7cfc04
.BR bind (2)
Packit 7cfc04
there).
Packit 7cfc04
.IP
Packit 7cfc04
Before Linux 3.8,
Packit 7cfc04
this socket option could be set, but could not retrieved with
Packit 7cfc04
.BR getsockopt (2).
Packit 7cfc04
Since Linux 3.8, it is readable.
Packit 7cfc04
The
Packit 7cfc04
.I optlen
Packit 7cfc04
argument should contain the buffer size available
Packit 7cfc04
to receive the device name and is recommended to be
Packit 7cfc04
.BR IFNAMSIZ
Packit 7cfc04
bytes.
Packit 7cfc04
The real device name length is reported back in the
Packit 7cfc04
.I optlen
Packit 7cfc04
argument.
Packit 7cfc04
.TP
Packit 7cfc04
.B SO_BROADCAST
Packit 7cfc04
Set or get the broadcast flag.
Packit 7cfc04
When enabled, datagram sockets are allowed to send
Packit 7cfc04
packets to a broadcast address.
Packit 7cfc04
This option has no effect on stream-oriented sockets.
Packit 7cfc04
.TP
Packit 7cfc04
.B SO_BSDCOMPAT
Packit 7cfc04
Enable BSD bug-to-bug compatibility.
Packit 7cfc04
This is used by the UDP protocol module in Linux 2.0 and 2.2.
Packit 7cfc04
If enabled, ICMP errors received for a UDP socket will not be passed
Packit 7cfc04
to the user program.
Packit 7cfc04
In later kernel versions, support for this option has been phased out:
Packit 7cfc04
Linux 2.4 silently ignores it, and Linux 2.6 generates a kernel warning
Packit 7cfc04
(printk()) if a program uses this option.
Packit 7cfc04
Linux 2.0 also enabled BSD bug-to-bug compatibility
Packit 7cfc04
options (random header changing, skipping of the broadcast flag) for raw
Packit 7cfc04
sockets with this option, but that was removed in Linux 2.2.
Packit 7cfc04
.TP
Packit 7cfc04
.B SO_DEBUG
Packit 7cfc04
Enable socket debugging.
Packit 7cfc04
Allowed only for processes with the
Packit 7cfc04
.B CAP_NET_ADMIN
Packit 7cfc04
capability or an effective user ID of 0.
Packit 7cfc04
.TP
Packit 7cfc04
.BR SO_DETACH_FILTER " (since Linux 2.2), " SO_DETACH_BPF " (since Linux 3.19)"
Packit 7cfc04
These two options, which are synonyms,
Packit 7cfc04
may be used to remove the classic or extended BPF
Packit 7cfc04
program attached to a socket with either
Packit 7cfc04
.BR SO_ATTACH_FILTER
Packit 7cfc04
or
Packit 7cfc04
.BR SO_ATTACH_BPF .
Packit 7cfc04
The option value is ignored.
Packit 7cfc04
.TP
Packit 7cfc04
.BR SO_DOMAIN " (since Linux 2.6.32)"
Packit 7cfc04
Retrieves the socket domain as an integer, returning a value such as
Packit 7cfc04
.BR AF_INET6 .
Packit 7cfc04
See
Packit 7cfc04
.BR socket (2)
Packit 7cfc04
for details.
Packit 7cfc04
This socket option is read-only.
Packit 7cfc04
.TP
Packit 7cfc04
.B SO_ERROR
Packit 7cfc04
Get and clear the pending socket error.
Packit 7cfc04
This socket option is read-only.
Packit 7cfc04
Expects an integer.
Packit 7cfc04
.TP
Packit 7cfc04
.B SO_DONTROUTE
Packit 7cfc04
Don't send via a gateway, send only to directly connected hosts.
Packit 7cfc04
The same effect can be achieved by setting the
Packit 7cfc04
.B MSG_DONTROUTE
Packit 7cfc04
flag on a socket
Packit 7cfc04
.BR send (2)
Packit 7cfc04
operation.
Packit 7cfc04
Expects an integer boolean flag.
Packit 7cfc04
.TP
Packit 7cfc04
.BR SO_INCOMING_CPU " (gettable since Linux 3.19, settable since Linux 4.4)"
Packit 7cfc04
.\" getsockopt 2c8c56e15df3d4c2af3d656e44feb18789f75837
Packit 7cfc04
.\" setsockopt 70da268b569d32a9fddeea85dc18043de9d89f89
Packit 7cfc04
Sets or gets the CPU affinity of a socket.
Packit 7cfc04
Expects an integer flag.
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
int cpu = 1;
Packit 7cfc04
socklen_t len = sizeof(cpu);
Packit 7cfc04
setsockopt(fd, SOL_SOCKET, SO_INCOMING_CPU, &cpu, &len;;
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
Because all of the packets for a single stream
Packit 7cfc04
(i.e., all packets for the same 4-tuple)
Packit 7cfc04
arrive on the single RX queue that is associated with a particular CPU,
Packit 7cfc04
the typical use case is to employ one listening process per RX queue,
Packit 7cfc04
with the incoming flow being handled by a listener
Packit 7cfc04
on the same CPU that is handling the RX queue.
Packit 7cfc04
This provides optimal NUMA behavior and keeps CPU caches hot.
Packit 7cfc04
.\"
Packit 7cfc04
.\" From an email conversation with Eric Dumazet:
Packit 7cfc04
.\" >> Note that setting the option is not supported if SO_REUSEPORT is used.
Packit 7cfc04
.\" >
Packit 7cfc04
.\" > Please define "not supported". Does this yield an API diagnostic?
Packit 7cfc04
.\" > If so, what is it?
Packit 7cfc04
.\" >
Packit 7cfc04
.\" >> Socket will be selected from an array, either by a hash or BPF program
Packit 7cfc04
.\" >> that has no access to this information.
Packit 7cfc04
.\" >
Packit 7cfc04
.\" > Sorry -- I'm lost here. How does this comment relate to the proposed
Packit 7cfc04
.\" > man page text above?
Packit 7cfc04
.\"
Packit 7cfc04
.\" Simply that :
Packit 7cfc04
.\"
Packit 7cfc04
.\" If an application uses both SO_INCOMING_CPU and SO_REUSEPORT, then
Packit 7cfc04
.\" SO_REUSEPORT logic, selecting the socket to receive the packet, ignores
Packit 7cfc04
.\" SO_INCOMING_CPU setting.
Packit 7cfc04
.TP
Packit 7cfc04
.B SO_KEEPALIVE
Packit 7cfc04
Enable sending of keep-alive messages on connection-oriented sockets.
Packit 7cfc04
Expects an integer boolean flag.
Packit 7cfc04
.TP
Packit 7cfc04
.B SO_LINGER
Packit 7cfc04
Sets or gets the
Packit 7cfc04
.B SO_LINGER
Packit 7cfc04
option.
Packit 7cfc04
The argument is a
Packit 7cfc04
.I linger
Packit 7cfc04
structure.
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct linger {
Packit 7cfc04
    int l_onoff;    /* linger active */
Packit 7cfc04
    int l_linger;   /* how many seconds to linger for */
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
When enabled, a
Packit 7cfc04
.BR close (2)
Packit 7cfc04
or
Packit 7cfc04
.BR shutdown (2)
Packit 7cfc04
will not return until all queued messages for the socket have been
Packit 7cfc04
successfully sent or the linger timeout has been reached.
Packit 7cfc04
Otherwise,
Packit 7cfc04
the call returns immediately and the closing is done in the background.
Packit 7cfc04
When the socket is closed as part of
Packit 7cfc04
.BR exit (2),
Packit 7cfc04
it always lingers in the background.
Packit 7cfc04
.TP
Packit 7cfc04
.B SO_LOCK_FILTER
Packit 7cfc04
.\" commit d59577b6ffd313d0ab3be39cb1ab47e29bdc9182
Packit 7cfc04
When set, this option will prevent
Packit 7cfc04
changing the filters associated with the socket.
Packit 7cfc04
These filters include any set using the socket options
Packit 7cfc04
.BR SO_ATTACH_FILTER,
Packit 7cfc04
.BR SO_ATTACH_BPF,
Packit 7cfc04
.BR SO_ATTACH_REUSEPORT_CBPF
Packit 7cfc04
and
Packit 7cfc04
.BR SO_ATTACH_REUSEPORT_EPBF .
Packit 7cfc04
.IP
Packit 7cfc04
The typical use case is for a privileged process to set up a raw socket
Packit 7cfc04
(an operation that requires the
Packit 7cfc04
.BR CAP_NET_RAW
Packit 7cfc04
capability), apply a restrictive filter, set the
Packit 7cfc04
.BR SO_LOCK_FILTER
Packit 7cfc04
option,
Packit 7cfc04
and then either drop its privileges or pass the socket file descriptor
Packit 7cfc04
to an unprivileged process via a UNIX domain socket.
Packit 7cfc04
.IP
Packit 7cfc04
Once the
Packit 7cfc04
.BR SO_LOCK_FILTER
Packit 7cfc04
option has been enabled, attempts to change or remove the filter
Packit 7cfc04
attached to a socket, or to disable the
Packit 7cfc04
.BR SO_LOCK_FILTER
Packit 7cfc04
option will fail with the error
Packit 7cfc04
.BR EPERM .
Packit 7cfc04
.TP
Packit 7cfc04
.BR SO_MARK " (since Linux 2.6.25)"
Packit 7cfc04
.\" commit 4a19ec5800fc3bb64e2d87c4d9fdd9e636086fe0
Packit 7cfc04
.\" and    914a9ab386a288d0f22252fc268ecbc048cdcbd5
Packit 7cfc04
Set the mark for each packet sent through this socket
Packit 7cfc04
(similar to the netfilter MARK target but socket-based).
Packit 7cfc04
Changing the mark can be used for mark-based
Packit 7cfc04
routing without netfilter or for packet filtering.
Packit 7cfc04
Setting this option requires the
Packit 7cfc04
.B CAP_NET_ADMIN
Packit 7cfc04
capability.
Packit 7cfc04
.TP
Packit 7cfc04
.B SO_OOBINLINE
Packit 7cfc04
If this option is enabled,
Packit 7cfc04
out-of-band data is directly placed into the receive data stream.
Packit 7cfc04
Otherwise, out-of-band data is passed only when the
Packit 7cfc04
.B MSG_OOB
Packit 7cfc04
flag is set during receiving.
Packit 7cfc04
.\" don't document it because it can do too much harm.
Packit 7cfc04
.\".B SO_NO_CHECK
Packit 7cfc04
.\"     The kernel has support for the SO_NO_CHECK socket
Packit 7cfc04
.\"     option (boolean: 0 == default, calculate checksum on xmit,
Packit 7cfc04
.\"     1 == do not calculate checksum on xmit).
Packit 7cfc04
.\" Additional note from Andi Kleen on SO_NO_CHECK (2010-08-30)
Packit 7cfc04
.\"     On Linux UDP checksums are essentially free and there's no reason
Packit 7cfc04
.\"     to turn them off and it would disable another safety line.
Packit 7cfc04
.\"     That is why I didn't document the option.
Packit 7cfc04
.TP
Packit 7cfc04
.B SO_PASSCRED
Packit 7cfc04
Enable or disable the receiving of the
Packit 7cfc04
.B SCM_CREDENTIALS
Packit 7cfc04
control message.
Packit 7cfc04
For more information see
Packit 7cfc04
.BR unix (7).
Packit 7cfc04
.\" FIXME Document SO_PASSSEC, added in 2.6.18; there is some info
Packit 7cfc04
.\" in the 2.6.18 ChangeLog
Packit 7cfc04
.TP
Packit 7cfc04
.BR SO_PEEK_OFF " (since Linux 3.4)"
Packit 7cfc04
.\" commit ef64a54f6e558155b4f149bb10666b9e914b6c54
Packit 7cfc04
This option, which is currently supported only for
Packit 7cfc04
.BR unix (7)
Packit 7cfc04
sockets, sets the value of the "peek offset" for the
Packit 7cfc04
.BR recv (2)
Packit 7cfc04
system call when used with
Packit 7cfc04
.BR MSG_PEEK
Packit 7cfc04
flag.
Packit 7cfc04
.IP
Packit 7cfc04
When this option is set to a negative value
Packit 7cfc04
(it is set to \-1 for all new sockets),
Packit 7cfc04
traditional behavior is provided:
Packit 7cfc04
.BR recv (2)
Packit 7cfc04
with the
Packit 7cfc04
.BR MSG_PEEK
Packit 7cfc04
flag will peek data from the front of the queue.
Packit 7cfc04
.IP
Packit 7cfc04
When the option is set to a value greater than or equal to zero,
Packit 7cfc04
then the next peek at data queued in the socket will occur at
Packit 7cfc04
the byte offset specified by the option value.
Packit 7cfc04
At the same time, the "peek offset" will be
Packit 7cfc04
incremented by the number of bytes that were peeked from the queue,
Packit 7cfc04
so that a subsequent peek will return the next data in the queue.
Packit 7cfc04
.IP
Packit 7cfc04
If data is removed from the front of the queue via a call to
Packit 7cfc04
.BR recv (2)
Packit 7cfc04
(or similar) without the
Packit 7cfc04
.BR MSG_PEEK
Packit 7cfc04
flag, the "peek offset" will be decreased by the number of bytes removed.
Packit 7cfc04
In other words, receiving data without the
Packit 7cfc04
.B MSG_PEEK
Packit 7cfc04
flag will cause the "peek offset" to be adjusted to maintain
Packit 7cfc04
the correct relative position in the queued data,
Packit 7cfc04
so that a subsequent peek will retrieve the data that would have been
Packit 7cfc04
retrieved had the data not been removed.
Packit 7cfc04
.IP
Packit 7cfc04
For datagram sockets, if the "peek offset" points to the middle of a packet,
Packit 7cfc04
the data returned will be marked with the
Packit 7cfc04
.BR MSG_TRUNC
Packit 7cfc04
flag.
Packit 7cfc04
.IP
Packit 7cfc04
The following example serves to illustrate the use of
Packit 7cfc04
.BR SO_PEEK_OFF .
Packit 7cfc04
Suppose a stream socket has the following queued input data:
Packit 7cfc04
.IP
Packit 7cfc04
    aabbccddeeff
Packit 7cfc04
.IP
Packit 7cfc04
The following sequence of
Packit 7cfc04
.BR recv (2)
Packit 7cfc04
calls would have the effect noted in the comments:
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
int ov = 4;                  // Set peek offset to 4
Packit 7cfc04
setsockopt(fd, SOL_SOCKET, SO_PEEK_OFF, &ov, sizeof(ov));
Packit 7cfc04
Packit 7cfc04
recv(fd, buf, 2, MSG_PEEK);  // Peeks "cc"; offset set to 6
Packit 7cfc04
recv(fd, buf, 2, MSG_PEEK);  // Peeks "dd"; offset set to 8
Packit 7cfc04
recv(fd, buf, 2, 0);         // Reads "aa"; offset set to 6
Packit 7cfc04
recv(fd, buf, 2, MSG_PEEK);  // Peeks "ee"; offset set to 8
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.TP
Packit 7cfc04
.B SO_PEERCRED
Packit 7cfc04
Return the credentials of the foreign process connected to this socket.
Packit 7cfc04
This is possible only for connected
Packit 7cfc04
.B AF_UNIX
Packit 7cfc04
stream sockets and
Packit 7cfc04
.B AF_UNIX
Packit 7cfc04
stream and datagram socket pairs created using
Packit 7cfc04
.BR socketpair (2);
Packit 7cfc04
see
Packit 7cfc04
.BR unix (7).
Packit 7cfc04
The returned credentials are those that were in effect at the time
Packit 7cfc04
of the call to
Packit 7cfc04
.BR connect (2)
Packit 7cfc04
or
Packit 7cfc04
.BR socketpair (2).
Packit 7cfc04
The argument is a
Packit 7cfc04
.I ucred
Packit 7cfc04
structure; define the
Packit 7cfc04
.B _GNU_SOURCE
Packit 7cfc04
feature test macro to obtain the definition of that structure from
Packit 7cfc04
.IR <sys/socket.h> .
Packit 7cfc04
This socket option is read-only.
Packit 7cfc04
.TP
Packit 7cfc04
.B SO_PRIORITY
Packit 7cfc04
Set the protocol-defined priority for all packets to be sent on
Packit 7cfc04
this socket.
Packit 7cfc04
Linux uses this value to order the networking queues:
Packit 7cfc04
packets with a higher priority may be processed first depending
Packit 7cfc04
on the selected device queueing discipline.
Packit 7cfc04
.\" For
Packit 7cfc04
.\" .BR ip (7),
Packit 7cfc04
.\" this also sets the IP type-of-service (TOS) field for outgoing packets.
Packit 7cfc04
Setting a priority outside the range 0 to 6 requires the
Packit 7cfc04
.B CAP_NET_ADMIN
Packit 7cfc04
capability.
Packit 7cfc04
.TP
Packit 7cfc04
.BR SO_PROTOCOL " (since Linux 2.6.32)"
Packit 7cfc04
Retrieves the socket protocol as an integer, returning a value such as
Packit 7cfc04
.BR IPPROTO_SCTP .
Packit 7cfc04
See
Packit 7cfc04
.BR socket (2)
Packit 7cfc04
for details.
Packit 7cfc04
This socket option is read-only.
Packit 7cfc04
.TP
Packit 7cfc04
.B SO_RCVBUF
Packit 7cfc04
Sets or gets the maximum socket receive buffer in bytes.
Packit 7cfc04
The kernel doubles this value (to allow space for bookkeeping overhead)
Packit 7cfc04
when it is set using
Packit 7cfc04
.\" Most (all?) other implementations do not do this -- MTK, Dec 05
Packit 7cfc04
.BR setsockopt (2),
Packit 7cfc04
and this doubled value is returned by
Packit 7cfc04
.BR getsockopt (2).
Packit 7cfc04
.\" The following thread on LMKL is quite informative:
Packit 7cfc04
.\" getsockopt/setsockopt with SO_RCVBUF and SO_SNDBUF "non-standard" behavior
Packit 7cfc04
.\" 17 July 2012
Packit 7cfc04
.\" http://thread.gmane.org/gmane.linux.kernel/1328935
Packit 7cfc04
The default value is set by the
Packit 7cfc04
.I /proc/sys/net/core/rmem_default
Packit 7cfc04
file, and the maximum allowed value is set by the
Packit 7cfc04
.I /proc/sys/net/core/rmem_max
Packit 7cfc04
file.
Packit 7cfc04
The minimum (doubled) value for this option is 256.
Packit 7cfc04
.TP
Packit 7cfc04
.BR SO_RCVBUFFORCE " (since Linux 2.6.14)"
Packit 7cfc04
Using this socket option, a privileged
Packit 7cfc04
.RB ( CAP_NET_ADMIN )
Packit 7cfc04
process can perform the same task as
Packit 7cfc04
.BR SO_RCVBUF ,
Packit 7cfc04
but the
Packit 7cfc04
.I rmem_max
Packit 7cfc04
limit can be overridden.
Packit 7cfc04
.TP
Packit 7cfc04
.BR SO_RCVLOWAT " and " SO_SNDLOWAT
Packit 7cfc04
Specify the minimum number of bytes in the buffer until the socket layer
Packit 7cfc04
will pass the data to the protocol
Packit 7cfc04
.RB ( SO_SNDLOWAT )
Packit 7cfc04
or the user on receiving
Packit 7cfc04
.RB ( SO_RCVLOWAT ).
Packit 7cfc04
These two values are initialized to 1.
Packit 7cfc04
.B SO_SNDLOWAT
Packit 7cfc04
is not changeable on Linux
Packit 7cfc04
.RB ( setsockopt (2)
Packit 7cfc04
fails with the error
Packit 7cfc04
.BR ENOPROTOOPT ).
Packit 7cfc04
.B SO_RCVLOWAT
Packit 7cfc04
is changeable
Packit 7cfc04
only since Linux 2.4.
Packit 7cfc04
The
Packit 7cfc04
.BR select (2)
Packit 7cfc04
and
Packit 7cfc04
.BR poll (2)
Packit 7cfc04
system calls currently do not respect the
Packit 7cfc04
.B SO_RCVLOWAT
Packit 7cfc04
setting on Linux,
Packit 7cfc04
and mark a socket readable when even a single byte of data is available.
Packit 7cfc04
A subsequent read from the socket will block until
Packit 7cfc04
.B SO_RCVLOWAT
Packit 7cfc04
bytes are available.
Packit 7cfc04
.\" See http://marc.theaimsgroup.com/?l=linux-kernel&m=111049368106984&w=2
Packit 7cfc04
.\" Tested on kernel 2.6.14 -- mtk, 30 Nov 05
Packit 7cfc04
.TP
Packit 7cfc04
.BR SO_RCVTIMEO " and " SO_SNDTIMEO
Packit 7cfc04
.\" Not implemented in 2.0.
Packit 7cfc04
.\" Implemented in 2.1.11 for getsockopt: always return a zero struct.
Packit 7cfc04
.\" Implemented in 2.3.41 for setsockopt, and actually used.
Packit 7cfc04
Specify the receiving or sending timeouts until reporting an error.
Packit 7cfc04
The argument is a
Packit 7cfc04
.IR "struct timeval" .
Packit 7cfc04
If an input or output function blocks for this period of time, and
Packit 7cfc04
data has been sent or received, the return value of that function
Packit 7cfc04
will be the amount of data transferred; if no data has been transferred
Packit 7cfc04
and the timeout has been reached, then \-1 is returned with
Packit 7cfc04
.I errno
Packit 7cfc04
set to
Packit 7cfc04
.BR EAGAIN
Packit 7cfc04
or
Packit 7cfc04
.BR EWOULDBLOCK ,
Packit 7cfc04
.\" in fact to EAGAIN
Packit 7cfc04
or
Packit 7cfc04
.B EINPROGRESS
Packit 7cfc04
(for
Packit 7cfc04
.BR connect (2))
Packit 7cfc04
just as if the socket was specified to be nonblocking.
Packit 7cfc04
If the timeout is set to zero (the default),
Packit 7cfc04
then the operation will never timeout.
Packit 7cfc04
Timeouts only have effect for system calls that perform socket I/O (e.g.,
Packit 7cfc04
.BR read (2),
Packit 7cfc04
.BR recvmsg (2),
Packit 7cfc04
.BR send (2),
Packit 7cfc04
.BR sendmsg (2));
Packit 7cfc04
timeouts have no effect for
Packit 7cfc04
.BR select (2),
Packit 7cfc04
.BR poll (2),
Packit 7cfc04
.BR epoll_wait (2),
Packit 7cfc04
and so on.
Packit 7cfc04
.TP
Packit 7cfc04
.B SO_REUSEADDR
Packit 7cfc04
.\"	commit c617f398edd4db2b8567a28e899a88f8f574798d
Packit 7cfc04
.\"	https://lwn.net/Articles/542629/
Packit 7cfc04
Indicates that the rules used in validating addresses supplied in a
Packit 7cfc04
.BR bind (2)
Packit 7cfc04
call should allow reuse of local addresses.
Packit 7cfc04
For
Packit 7cfc04
.B AF_INET
Packit 7cfc04
sockets this
Packit 7cfc04
means that a socket may bind, except when there
Packit 7cfc04
is an active listening socket bound to the address.
Packit 7cfc04
When the listening socket is bound to
Packit 7cfc04
.B INADDR_ANY
Packit 7cfc04
with a specific port then it is not possible
Packit 7cfc04
to bind to this port for any local address.
Packit 7cfc04
Argument is an integer boolean flag.
Packit 7cfc04
.TP
Packit 7cfc04
.BR SO_REUSEPORT " (since Linux 3.9)"
Packit 7cfc04
Permits multiple
Packit 7cfc04
.B AF_INET
Packit 7cfc04
or
Packit 7cfc04
.B AF_INET6
Packit 7cfc04
sockets to be bound to an identical socket address.
Packit 7cfc04
This option must be set on each socket (including the first socket)
Packit 7cfc04
prior to calling
Packit 7cfc04
.BR bind (2)
Packit 7cfc04
on the socket.
Packit 7cfc04
To prevent port hijacking,
Packit 7cfc04
all of the processes binding to the same address must have the same
Packit 7cfc04
effective UID.
Packit 7cfc04
This option can be employed with both TCP and UDP sockets.
Packit 7cfc04
.IP
Packit 7cfc04
For TCP sockets, this option allows
Packit 7cfc04
.BR accept (2)
Packit 7cfc04
load distribution in a multi-threaded server to be improved by
Packit 7cfc04
using a distinct listener socket for each thread.
Packit 7cfc04
This provides improved load distribution as compared
Packit 7cfc04
to traditional techniques such using a single
Packit 7cfc04
.BR accept (2)ing
Packit 7cfc04
thread that distributes connections,
Packit 7cfc04
or having multiple threads that compete to
Packit 7cfc04
.BR accept (2)
Packit 7cfc04
from the same socket.
Packit 7cfc04
.IP
Packit 7cfc04
For UDP sockets,
Packit 7cfc04
the use of this option can provide better distribution
Packit 7cfc04
of incoming datagrams to multiple processes (or threads) as compared
Packit 7cfc04
to the traditional technique of having multiple processes
Packit 7cfc04
compete to receive datagrams on the same socket.
Packit 7cfc04
.TP
Packit 7cfc04
.BR SO_RXQ_OVFL " (since Linux 2.6.33)"
Packit 7cfc04
.\" commit 3b885787ea4112eaa80945999ea0901bf742707f
Packit 7cfc04
Indicates that an unsigned 32-bit value ancillary message (cmsg)
Packit 7cfc04
should be attached to received skbs indicating
Packit 7cfc04
the number of packets dropped by the socket since its creation.
Packit 7cfc04
.TP
Packit 7cfc04
.B SO_SNDBUF
Packit 7cfc04
Sets or gets the maximum socket send buffer in bytes.
Packit 7cfc04
The kernel doubles this value (to allow space for bookkeeping overhead)
Packit 7cfc04
when it is set using
Packit 7cfc04
.\" Most (all?) other implementations do not do this -- MTK, Dec 05
Packit 7cfc04
.\" See also the comment to SO_RCVBUF (17 Jul 2012 LKML mail)
Packit 7cfc04
.BR setsockopt (2),
Packit 7cfc04
and this doubled value is returned by
Packit 7cfc04
.BR getsockopt (2).
Packit 7cfc04
The default value is set by the
Packit 7cfc04
.I /proc/sys/net/core/wmem_default
Packit 7cfc04
file and the maximum allowed value is set by the
Packit 7cfc04
.I /proc/sys/net/core/wmem_max
Packit 7cfc04
file.
Packit 7cfc04
The minimum (doubled) value for this option is 2048.
Packit 7cfc04
.TP
Packit 7cfc04
.BR SO_SNDBUFFORCE " (since Linux 2.6.14)"
Packit 7cfc04
Using this socket option, a privileged
Packit 7cfc04
.RB ( CAP_NET_ADMIN )
Packit 7cfc04
process can perform the same task as
Packit 7cfc04
.BR SO_SNDBUF ,
Packit 7cfc04
but the
Packit 7cfc04
.I wmem_max
Packit 7cfc04
limit can be overridden.
Packit 7cfc04
.TP
Packit 7cfc04
.B SO_TIMESTAMP
Packit 7cfc04
Enable or disable the receiving of the
Packit 7cfc04
.B SO_TIMESTAMP
Packit 7cfc04
control message.
Packit 7cfc04
The timestamp control message is sent with level
Packit 7cfc04
.B SOL_SOCKET
Packit 7cfc04
and the
Packit 7cfc04
.I cmsg_data
Packit 7cfc04
field is a
Packit 7cfc04
.I "struct timeval"
Packit 7cfc04
indicating the
Packit 7cfc04
reception time of the last packet passed to the user in this call.
Packit 7cfc04
See
Packit 7cfc04
.BR cmsg (3)
Packit 7cfc04
for details on control messages.
Packit 7cfc04
.TP
Packit 7cfc04
.B SO_TYPE
Packit 7cfc04
Gets the socket type as an integer (e.g.,
Packit 7cfc04
.BR SOCK_STREAM ).
Packit 7cfc04
This socket option is read-only.
Packit 7cfc04
.TP
Packit 7cfc04
.BR SO_BUSY_POLL " (since Linux 3.11)"
Packit 7cfc04
Sets the approximate time in microseconds to busy poll on a blocking receive
Packit 7cfc04
when there is no data.
Packit 7cfc04
Increasing this value requires
Packit 7cfc04
.BR CAP_NET_ADMIN .
Packit 7cfc04
The default for this option is controlled by the
Packit 7cfc04
.I /proc/sys/net/core/busy_read
Packit 7cfc04
file.
Packit 7cfc04
.IP
Packit 7cfc04
The value in the
Packit 7cfc04
.I /proc/sys/net/core/busy_poll
Packit 7cfc04
file determines how long
Packit 7cfc04
.BR select (2)
Packit 7cfc04
and
Packit 7cfc04
.BR poll (2)
Packit 7cfc04
will busy poll when they operate on sockets with
Packit 7cfc04
.BR SO_BUSY_POLL
Packit 7cfc04
set and no events to report are found.
Packit 7cfc04
.IP
Packit 7cfc04
In both cases,
Packit 7cfc04
busy polling will only be done when the socket last received data
Packit 7cfc04
from a network device that supports this option.
Packit 7cfc04
.IP
Packit 7cfc04
While busy polling may improve latency of some applications,
Packit 7cfc04
care must be taken when using it since this will increase
Packit 7cfc04
both CPU utilization and power usage.
Packit 7cfc04
.SS Signals
Packit 7cfc04
When writing onto a connection-oriented socket that has been shut down
Packit 7cfc04
(by the local or the remote end)
Packit 7cfc04
.B SIGPIPE
Packit 7cfc04
is sent to the writing process and
Packit 7cfc04
.B EPIPE
Packit 7cfc04
is returned.
Packit 7cfc04
The signal is not sent when the write call
Packit 7cfc04
specified the
Packit 7cfc04
.B MSG_NOSIGNAL
Packit 7cfc04
flag.
Packit 7cfc04
.PP
Packit 7cfc04
When requested with the
Packit 7cfc04
.B FIOSETOWN
Packit 7cfc04
.BR fcntl (2)
Packit 7cfc04
or
Packit 7cfc04
.B SIOCSPGRP
Packit 7cfc04
.BR ioctl (2),
Packit 7cfc04
.B SIGIO
Packit 7cfc04
is sent when an I/O event occurs.
Packit 7cfc04
It is possible to use
Packit 7cfc04
.BR poll (2)
Packit 7cfc04
or
Packit 7cfc04
.BR select (2)
Packit 7cfc04
in the signal handler to find out which socket the event occurred on.
Packit 7cfc04
An alternative (in Linux 2.2) is to set a real-time signal using the
Packit 7cfc04
.B F_SETSIG
Packit 7cfc04
.BR fcntl (2);
Packit 7cfc04
the handler of the real time signal will be called with
Packit 7cfc04
the file descriptor in the
Packit 7cfc04
.I si_fd
Packit 7cfc04
field of its
Packit 7cfc04
.IR siginfo_t .
Packit 7cfc04
See
Packit 7cfc04
.BR fcntl (2)
Packit 7cfc04
for more information.
Packit 7cfc04
.PP
Packit 7cfc04
Under some circumstances (e.g., multiple processes accessing a
Packit 7cfc04
single socket), the condition that caused the
Packit 7cfc04
.B SIGIO
Packit 7cfc04
may have already disappeared when the process reacts to the signal.
Packit 7cfc04
If this happens, the process should wait again because Linux
Packit 7cfc04
will resend the signal later.
Packit 7cfc04
.\" .SS Ancillary messages
Packit 7cfc04
.SS /proc interfaces
Packit 7cfc04
The core socket networking parameters can be accessed
Packit 7cfc04
via files in the directory
Packit 7cfc04
.IR /proc/sys/net/core/ .
Packit 7cfc04
.TP
Packit 7cfc04
.I rmem_default
Packit 7cfc04
contains the default setting in bytes of the socket receive buffer.
Packit 7cfc04
.TP
Packit 7cfc04
.I rmem_max
Packit 7cfc04
contains the maximum socket receive buffer size in bytes which a user may
Packit 7cfc04
set by using the
Packit 7cfc04
.B SO_RCVBUF
Packit 7cfc04
socket option.
Packit 7cfc04
.TP
Packit 7cfc04
.I wmem_default
Packit 7cfc04
contains the default setting in bytes of the socket send buffer.
Packit 7cfc04
.TP
Packit 7cfc04
.I wmem_max
Packit 7cfc04
contains the maximum socket send buffer size in bytes which a user may
Packit 7cfc04
set by using the
Packit 7cfc04
.B SO_SNDBUF
Packit 7cfc04
socket option.
Packit 7cfc04
.TP
Packit 7cfc04
.IR message_cost " and " message_burst
Packit 7cfc04
configure the token bucket filter used to load limit warning messages
Packit 7cfc04
caused by external network events.
Packit 7cfc04
.TP
Packit 7cfc04
.I netdev_max_backlog
Packit 7cfc04
Maximum number of packets in the global input queue.
Packit 7cfc04
.TP
Packit 7cfc04
.I optmem_max
Packit 7cfc04
Maximum length of ancillary data and user control data like the iovecs
Packit 7cfc04
per socket.
Packit 7cfc04
.\" netdev_fastroute is not documented because it is experimental
Packit 7cfc04
.SS Ioctls
Packit 7cfc04
These operations can be accessed using
Packit 7cfc04
.BR ioctl (2):
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
.IB error " = ioctl(" ip_socket ", " ioctl_type ", " &value_result ");"
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.TP
Packit 7cfc04
.B SIOCGSTAMP
Packit 7cfc04
Return a
Packit 7cfc04
.I struct timeval
Packit 7cfc04
with the receive timestamp of the last packet passed to the user.
Packit 7cfc04
This is useful for accurate round trip time measurements.
Packit 7cfc04
See
Packit 7cfc04
.BR setitimer (2)
Packit 7cfc04
for a description of
Packit 7cfc04
.IR "struct timeval" .
Packit 7cfc04
.\"
Packit 7cfc04
This ioctl should be used only if the socket option
Packit 7cfc04
.B SO_TIMESTAMP
Packit 7cfc04
is not set on the socket.
Packit 7cfc04
Otherwise, it returns the timestamp of the
Packit 7cfc04
last packet that was received while
Packit 7cfc04
.B SO_TIMESTAMP
Packit 7cfc04
was not set, or it fails if no such packet has been received,
Packit 7cfc04
(i.e.,
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
returns \-1 with
Packit 7cfc04
.I errno
Packit 7cfc04
set to
Packit 7cfc04
.BR ENOENT ).
Packit 7cfc04
.TP
Packit 7cfc04
.B SIOCSPGRP
Packit 7cfc04
Set the process or process group that is to receive
Packit 7cfc04
.B SIGIO
Packit 7cfc04
or
Packit 7cfc04
.B SIGURG
Packit 7cfc04
signals when I/O becomes possible or urgent data is available.
Packit 7cfc04
The argument is a pointer to a
Packit 7cfc04
.IR pid_t .
Packit 7cfc04
For further details, see the description of
Packit 7cfc04
.BR F_SETOWN
Packit 7cfc04
in
Packit 7cfc04
.BR fcntl (2).
Packit 7cfc04
.TP
Packit 7cfc04
.B FIOASYNC
Packit 7cfc04
Change the
Packit 7cfc04
.B O_ASYNC
Packit 7cfc04
flag to enable or disable asynchronous I/O mode of the socket.
Packit 7cfc04
Asynchronous I/O mode means that the
Packit 7cfc04
.B SIGIO
Packit 7cfc04
signal or the signal set with
Packit 7cfc04
.B F_SETSIG
Packit 7cfc04
is raised when a new I/O event occurs.
Packit 7cfc04
.IP
Packit 7cfc04
Argument is an integer boolean flag.
Packit 7cfc04
(This operation is synonymous with the use of
Packit 7cfc04
.BR fcntl (2)
Packit 7cfc04
to set the
Packit 7cfc04
.B O_ASYNC
Packit 7cfc04
flag.)
Packit 7cfc04
.\"
Packit 7cfc04
.TP
Packit 7cfc04
.B SIOCGPGRP
Packit 7cfc04
Get the current process or process group that receives
Packit 7cfc04
.B SIGIO
Packit 7cfc04
or
Packit 7cfc04
.B SIGURG
Packit 7cfc04
signals,
Packit 7cfc04
or 0
Packit 7cfc04
when none is set.
Packit 7cfc04
.PP
Packit 7cfc04
Valid
Packit 7cfc04
.BR fcntl (2)
Packit 7cfc04
operations:
Packit 7cfc04
.TP
Packit 7cfc04
.B FIOGETOWN
Packit 7cfc04
The same as the
Packit 7cfc04
.B SIOCGPGRP
Packit 7cfc04
.BR ioctl (2).
Packit 7cfc04
.TP
Packit 7cfc04
.B FIOSETOWN
Packit 7cfc04
The same as the
Packit 7cfc04
.B SIOCSPGRP
Packit 7cfc04
.BR ioctl (2).
Packit 7cfc04
.SH VERSIONS
Packit 7cfc04
.B SO_BINDTODEVICE
Packit 7cfc04
was introduced in Linux 2.0.30.
Packit 7cfc04
.B SO_PASSCRED
Packit 7cfc04
is new in Linux 2.2.
Packit 7cfc04
The
Packit 7cfc04
.I /proc
Packit 7cfc04
interfaces were introduced in Linux 2.2.
Packit 7cfc04
.B SO_RCVTIMEO
Packit 7cfc04
and
Packit 7cfc04
.B SO_SNDTIMEO
Packit 7cfc04
are supported since Linux 2.3.41.
Packit 7cfc04
Earlier, timeouts were fixed to
Packit 7cfc04
a protocol-specific setting, and could not be read or written.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
Linux assumes that half of the send/receive buffer is used for internal
Packit 7cfc04
kernel structures; thus the values in the corresponding
Packit 7cfc04
.I /proc
Packit 7cfc04
files are twice what can be observed on the wire.
Packit 7cfc04
.PP
Packit 7cfc04
Linux will allow port reuse only with the
Packit 7cfc04
.B SO_REUSEADDR
Packit 7cfc04
option
Packit 7cfc04
when this option was set both in the previous program that performed a
Packit 7cfc04
.BR bind (2)
Packit 7cfc04
to the port and in the program that wants to reuse the port.
Packit 7cfc04
This differs from some implementations (e.g., FreeBSD)
Packit 7cfc04
where only the later program needs to set the
Packit 7cfc04
.B SO_REUSEADDR
Packit 7cfc04
option.
Packit 7cfc04
Typically this difference is invisible, since, for example, a server
Packit 7cfc04
program is designed to always set this option.
Packit 7cfc04
.\" .SH AUTHORS
Packit 7cfc04
.\" This man page was written by Andi Kleen.
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR wireshark (1),
Packit 7cfc04
.BR bpf (2),
Packit 7cfc04
.BR connect (2),
Packit 7cfc04
.BR getsockopt (2),
Packit 7cfc04
.BR setsockopt (2),
Packit 7cfc04
.BR socket (2),
Packit 7cfc04
.BR pcap (3),
Packit 7cfc04
.BR capabilities (7),
Packit 7cfc04
.BR ddp (7),
Packit 7cfc04
.BR ip (7),
Packit 7cfc04
.BR packet (7),
Packit 7cfc04
.BR tcp (7),
Packit 7cfc04
.BR udp (7),
Packit 7cfc04
.BR unix (7),
Packit 7cfc04
.BR tcpdump (8)
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/.