1udp(7)                 Miscellaneous Information Manual                 udp(7)
2
3
4

NAME

6       udp - User Datagram Protocol for IPv4
7

SYNOPSIS

9       #include <sys/socket.h>
10       #include <netinet/in.h>
11       #include <netinet/udp.h>
12
13       udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
14

DESCRIPTION

16       This  is  an  implementation of the User Datagram Protocol described in
17       RFC 768.  It implements a connectionless,  unreliable  datagram  packet
18       service.   Packets  may  be reordered or duplicated before they arrive.
19       UDP generates and checks checksums to catch transmission errors.
20
21       When a UDP socket is created, its local and remote  addresses  are  un‐
22       specified.   Datagrams  can  be  sent  immediately  using  sendto(2) or
23       sendmsg(2) with a valid destination address as an argument.  When  con‐
24       nect(2) is called on the socket, the default destination address is set
25       and datagrams can now be sent using send(2) or write(2) without  speci‐
26       fying  a  destination  address.   It is still possible to send to other
27       destinations by passing an address to sendto(2) or sendmsg(2).  In  or‐
28       der  to  receive  packets,  the  socket can be bound to a local address
29       first by using bind(2).  Otherwise, the socket layer will automatically
30       assign   a   free   local   port   out   of   the   range   defined  by
31       /proc/sys/net/ipv4/ip_local_port_range  and  bind  the  socket  to  IN‐
32       ADDR_ANY.
33
34       All  receive  operations  return  only  one packet.  When the packet is
35       smaller than the passed buffer, only that much data is  returned;  when
36       it  is  bigger,  the packet is truncated and the MSG_TRUNC flag is set.
37       MSG_WAITALL is not supported.
38
39       IP options may be sent or received using the socket  options  described
40       in  ip(7).   They are processed by the kernel only when the appropriate
41       /proc parameter is enabled (but still passed to the user even  when  it
42       is turned off).  See ip(7).
43
44       When  the MSG_DONTROUTE flag is set on sending, the destination address
45       must refer to a local interface address and the packet is sent only  to
46       that interface.
47
48       By default, Linux UDP does path MTU (Maximum Transmission Unit) discov‐
49       ery.  This means the kernel will keep track of the MTU  to  a  specific
50       target  IP  address and return EMSGSIZE when a UDP packet write exceeds
51       it.  When this happens, the  application  should  decrease  the  packet
52       size.   Path MTU discovery can be also turned off using the IP_MTU_DIS‐
53       COVER socket option or the /proc/sys/net/ipv4/ip_no_pmtu_disc file; see
54       ip(7)  for  details.   When  turned off, UDP will fragment outgoing UDP
55       packets that exceed the interface MTU.  However, disabling  it  is  not
56       recommended for performance and reliability reasons.
57
58   Address format
59       UDP uses the IPv4 sockaddr_in address format described in ip(7).
60
61   Error handling
62       All  fatal  errors  will  be passed to the user as an error return even
63       when the socket is not connected.  This  includes  asynchronous  errors
64       received  from the network.  You may get an error for an earlier packet
65       that was sent on the same socket.   This  behavior  differs  from  many
66       other BSD socket implementations which don't pass any errors unless the
67       socket is connected.  Linux's behavior is mandated by RFC 1122.
68
69       For compatibility with legacy code, in Linux 2.0 and 2.2 it was  possi‐
70       ble  to set the SO_BSDCOMPAT SOL_SOCKET option to receive remote errors
71       only when the socket has been connected (except for  EPROTO  and  EMSG‐
72       SIZE).   Locally  generated errors are always passed.  Support for this
73       socket option was removed in later kernels; see socket(7)  for  further
74       information.
75
76       When  the  IP_RECVERR  option  is enabled, all errors are stored in the
77       socket error queue, and can be received by recvmsg(2) with the  MSG_ER‐
78       RQUEUE flag set.
79
80   /proc interfaces
81       System-wide  UDP parameter settings can be accessed by files in the di‐
82       rectory /proc/sys/net/ipv4/.
83
84       udp_mem (since Linux 2.6.25)
85              This is a vector of three integers governing the number of pages
86              allowed for queueing by all UDP sockets.
87
88              min    Below this number of pages, UDP is not bothered about its
89                     memory appetite.  When the amount of memory allocated  by
90                     UDP  exceeds  this  number, UDP starts to moderate memory
91                     usage.
92
93              pressure
94                     This value was introduced to follow the format of tcp_mem
95                     (see tcp(7)).
96
97              max    Number of pages allowed for queueing by all UDP sockets.
98
99              Defaults  values  for  these  three items are calculated at boot
100              time from the amount of available memory.
101
102       udp_rmem_min (integer; default value: PAGE_SIZE; since Linux 2.6.25)
103              Minimal size, in bytes, of receive buffers used by  UDP  sockets
104              in  moderation.  Each UDP socket is able to use the size for re‐
105              ceiving data, even if total pages of UDP sockets exceed  udp_mem
106              pressure.
107
108       udp_wmem_min (integer; default value: PAGE_SIZE; since Linux 2.6.25)
109              Minimal  size,  in  bytes, of send buffer used by UDP sockets in
110              moderation.  Each UDP socket is able to use the size for sending
111              data,  even  if  total pages of UDP sockets exceed udp_mem pres‐
112              sure.
113
114   Socket options
115       To set or get a UDP socket option, call getsockopt(2) to read  or  set‐
116       sockopt(2)  to  write  the option with the option level argument set to
117       IPPROTO_UDP.  Unless otherwise noted, optval is a pointer to an int.
118
119       Following is a list of UDP-specific socket  options.   For  details  of
120       some other socket options that are also applicable for UDP sockets, see
121       socket(7).
122
123       UDP_CORK (since Linux 2.5.44)
124              If this option is enabled, then all data output on  this  socket
125              is  accumulated  into a single datagram that is transmitted when
126              the option is disabled.  This option should not be used in  code
127              intended to be portable.
128
129       UDP_SEGMENT (since Linux 4.18)
130              Enables  UDP segmentation offload.  Segmentation offload reduces
131              send(2) cost by transferring multiple datagrams worth of data as
132              a  single  large  packet  through the kernel transmit path, even
133              when that exceeds MTU.  As late as possible, the large packet is
134              split  by segment size into a series of datagrams.  This segmen‐
135              tation offload step is deferred to hardware if  supported,  else
136              performed  in  software.  This option takes a value in the range
137              [0, USHRT_MAX] that sets the segment size: the size of  datagram
138              payload,  excluding  the  UDP  header.  The segment size must be
139              chosen such that at most 64 datagrams are sent in a single  call
140              and  that  the  datagrams  after  segmentation meet the same MTU
141              rules that apply to datagrams sent without this option.  Segmen‐
142              tation  offload  depends on checksum offload, as datagram check‐
143              sums are computed after segmentation.  The option  may  also  be
144              set  for individual sendmsg(2) calls by passing it as a cmsg(3).
145              A value of zero disables the feature.  This option should not be
146              used in code intended to be portable.
147
148       UDP_GRO (since Linux 5.0)
149              Enables UDP receive offload.  If enabled, the socket may receive
150              multiple datagrams worth of data as a single large  buffer,  to‐
151              gether  with a cmsg(3) that holds the segment size.  This option
152              is the inverse of segmentation offload.  It reduces receive cost
153              by  handling  multiple datagrams worth of data as a single large
154              packet in the kernel receive path, even when that  exceeds  MTU.
155              This option should not be used in code intended to be portable.
156
157   Ioctls
158       These ioctls can be accessed using ioctl(2).  The correct syntax is:
159
160              int value;
161              error = ioctl(udp_socket, ioctl_type, &value);
162
163       FIONREAD (SIOCINQ)
164              Gets  a  pointer to an integer as argument.  Returns the size of
165              the next pending datagram in the integer in bytes, or 0 when  no
166              datagram  is pending.  Warning: Using FIONREAD, it is impossible
167              to distinguish the case where no datagram is  pending  from  the
168              case  where  the  next  pending  datagram contains zero bytes of
169              data.  It is safer to use select(2),  poll(2),  or  epoll(7)  to
170              distinguish these cases.
171
172       TIOCOUTQ (SIOCOUTQ)
173              Returns  the number of data bytes in the local send queue.  Sup‐
174              ported only with Linux 2.4 and above.
175
176       In addition, all ioctls documented in  ip(7)  and  socket(7)  are  sup‐
177       ported.
178

ERRORS

180       All  errors documented for socket(7) or ip(7) may be returned by a send
181       or receive on a UDP socket.
182
183       ECONNREFUSED
184              No receiver was associated with the destination  address.   This
185              might be caused by a previous packet sent over the socket.
186

VERSIONS

188       IP_RECVERR is a new feature in Linux 2.2.
189

SEE ALSO

191       ip(7), raw(7), socket(7), udplite(7)
192
193       The kernel source file Documentation/networking/ip-sysctl.txt.
194
195       RFC 768 for the User Datagram Protocol.
196       RFC 1122 for the host requirements.
197       RFC 1191 for a description of path MTU discovery.
198
199
200
201Linux man-pages 6.05              2023-07-15                            udp(7)
Impressum