xref: /aosp_15_r20/external/cronet/net/socket/socket_options.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_SOCKET_SOCKET_OPTIONS_H_
6 #define NET_SOCKET_SOCKET_OPTIONS_H_
7 
8 #include <stdint.h>
9 
10 #include "net/base/net_export.h"
11 #include "net/socket/socket_descriptor.h"
12 
13 namespace net {
14 
15 // This function enables/disables buffering in the kernel. By default, on Linux,
16 // TCP sockets will wait up to 200ms for more data to complete a packet before
17 // transmitting. After calling this function, the kernel will not wait. See
18 // TCP_NODELAY in `man 7 tcp`.
19 //
20 // For Windows:
21 //
22 // The Nagle implementation on Windows is governed by RFC 896.  The idea
23 // behind Nagle is to reduce small packets on the network.  When Nagle is
24 // enabled, if a partial packet has been sent, the TCP stack will disallow
25 // further *partial* packets until an ACK has been received from the other
26 // side.  Good applications should always strive to send as much data as
27 // possible and avoid partial-packet sends.  However, in most real world
28 // applications, there are edge cases where this does not happen, and two
29 // partial packets may be sent back to back.  For a browser, it is NEVER
30 // a benefit to delay for an RTT before the second packet is sent.
31 //
32 // As a practical example in Chromium today, consider the case of a small
33 // POST.  I have verified this:
34 //     Client writes 649 bytes of header  (partial packet #1)
35 //     Client writes 50 bytes of POST data (partial packet #2)
36 // In the above example, with Nagle, a RTT delay is inserted between these
37 // two sends due to nagle.  RTTs can easily be 100ms or more.  The best
38 // fix is to make sure that for POSTing data, we write as much data as
39 // possible and minimize partial packets.  We will fix that.  But disabling
40 // Nagle also ensure we don't run into this delay in other edge cases.
41 // See also:
42 //    http://technet.microsoft.com/en-us/library/bb726981.aspx
43 //
44 // SetTCPNoDelay() sets the TCP_NODELAY option. Use |no_delay| to enable or
45 // disable it. On error returns a net error code, on success returns OK.
46 int SetTCPNoDelay(SocketDescriptor fd, bool no_delay);
47 
48 // SetReuseAddr() sets the SO_REUSEADDR socket option. Use |reuse| to enable or
49 // disable it. On error returns a net error code, on success returns OK.
50 int SetReuseAddr(SocketDescriptor fd, bool reuse);
51 
52 // SetSocketReceiveBufferSize() sets the SO_RCVBUF socket option. On error
53 // returns a net error code, on success returns OK.
54 int SetSocketReceiveBufferSize(SocketDescriptor fd, int32_t size);
55 
56 // SetSocketSendBufferSize() sets the SO_SNDBUF socket option. On error
57 // returns a net error code, on success returns OK.
58 int SetSocketSendBufferSize(SocketDescriptor fd, int32_t size);
59 
60 // SetIPv6Only() sets the IPV6_V6ONLY socket option. On error
61 // returns a net error code, on success returns OK.
62 int SetIPv6Only(SocketDescriptor fd, bool ipv6_only);
63 
64 }  // namespace net
65 
66 #endif  // NET_SOCKET_SOCKET_OPTIONS_H_
67