Skip to content

Commit f26efb6

Browse files
jwhitedraggi
authored andcommitted
conn: set SO_{SND,RCV}BUF to 7MB on the Bind UDP socket
The conn.Bind UDP sockets' send and receive buffers are now being sized to 7MB, whereas they were previously inheriting the system defaults. The system defaults are considerably small and can result in dropped packets on high speed links. By increasing the size of these buffers we are able to achieve higher throughput in the aforementioned case. The iperf3 results below demonstrate the effect of this commit between two Linux computers with 32-core Xeon Platinum CPUs @ 2.9Ghz. There is roughly ~125us of round trip latency between them. The first result is from commit 792b49c which uses the system defaults, e.g. net.core.{r,w}mem_max = 212992. The TCP retransmits are correlated with buffer full drops on both sides. Starting Test: protocol: TCP, 1 streams, 131072 byte blocks [ ID] Interval Transfer Bitrate Retr Cwnd [ 5] 0.00-10.00 sec 4.74 GBytes 4.08 Gbits/sec 2742 285 KBytes - - - - - - - - - - - - - - - - - - - - - - - - - Test Complete. Summary Results: [ ID] Interval Transfer Bitrate Retr [ 5] 0.00-10.00 sec 4.74 GBytes 4.08 Gbits/sec 2742 sender [ 5] 0.00-10.04 sec 4.74 GBytes 4.06 Gbits/sec receiver The second result is after increasing SO_{SND,RCV}BUF to 7MB, i.e. applying this commit. Starting Test: protocol: TCP, 1 streams, 131072 byte blocks [ ID] Interval Transfer Bitrate Retr Cwnd [ 5] 0.00-10.00 sec 6.14 GBytes 5.27 Gbits/sec 0 3.15 MBytes - - - - - - - - - - - - - - - - - - - - - - - - - Test Complete. Summary Results: [ ID] Interval Transfer Bitrate Retr [ 5] 0.00-10.00 sec 6.14 GBytes 5.27 Gbits/sec 0 sender [ 5] 0.00-10.04 sec 6.14 GBytes 5.25 Gbits/sec receiver The specific value of 7MB is chosen as it is the max supported by a default configuration of macOS. A value greater than 7MB may further benefit throughput for environments with higher network latency and lower CPU clocks, but will also increase latency under load (bufferbloat). Some platforms will silently clamp the value to other maximums. On Linux, we use SO_{SND,RCV}BUFFORCE in case 7MB is beyond net.core.{r,w}mem_max. Co-authored-by: James Tucker <[email protected]> Signed-off-by: James Tucker <[email protected]> Signed-off-by: Jordan Whited <[email protected]> Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent f67c862 commit f26efb6

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

conn/controlfns.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ import (
1010
"syscall"
1111
)
1212

13+
// UDP socket read/write buffer size (7MB). The value of 7MB is chosen as it is
14+
// the max supported by a default configuration of macOS. Some platforms will
15+
// silently clamp the value to other maximums, such as linux clamping to
16+
// net.core.{r,w}mem_max (see _linux.go for additional implementation that works
17+
// around this limitation)
18+
const socketBufferSize = 7 << 20
19+
1320
// controlFn is the callback function signature from net.ListenConfig.Control.
1421
// It is used to apply platform specific configuration to the socket prior to
1522
// bind.

conn/controlfns_linux.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ import (
1515
func init() {
1616
controlFns = append(controlFns,
1717

18+
// Attempt to set the socket buffer size beyond net.core.{r,w}mem_max by
19+
// using SO_*BUFFORCE. This requires CAP_NET_ADMIN, and is allowed here to
20+
// fail silently - the result of failure is lower performance on very fast
21+
// links or high latency links.
22+
func(network, address string, c syscall.RawConn) error {
23+
return c.Control(func(fd uintptr) {
24+
// Set up to *mem_max
25+
_ = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_RCVBUF, socketBufferSize)
26+
_ = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_SNDBUF, socketBufferSize)
27+
// Set beyond *mem_max if CAP_NET_ADMIN
28+
_ = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_RCVBUFFORCE, socketBufferSize)
29+
_ = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_SNDBUFFORCE, socketBufferSize)
30+
})
31+
},
32+
1833
// Enable receiving of the packet information (IP_PKTINFO for IPv4,
1934
// IPV6_PKTINFO for IPv6) that is used to implement sticky socket support.
2035
func(network, address string, c syscall.RawConn) error {

conn/controlfns_unix.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ import (
1515

1616
func init() {
1717
controlFns = append(controlFns,
18+
func(network, address string, c syscall.RawConn) error {
19+
return c.Control(func(fd uintptr) {
20+
_ = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_RCVBUF, socketBufferSize)
21+
_ = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_SNDBUF, socketBufferSize)
22+
})
23+
},
24+
1825
func(network, address string, c syscall.RawConn) error {
1926
var err error
2027
if network == "udp6" {

conn/controlfns_windows.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* SPDX-License-Identifier: MIT
2+
*
3+
* Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved.
4+
*/
5+
6+
package conn
7+
8+
import (
9+
"syscall"
10+
11+
"golang.org/x/sys/windows"
12+
)
13+
14+
func init() {
15+
controlFns = append(controlFns,
16+
func(network, address string, c syscall.RawConn) error {
17+
return c.Control(func(fd uintptr) {
18+
_ = windows.SetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET, windows.SO_RCVBUF, socketBufferSize)
19+
_ = windows.SetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET, windows.SO_SNDBUF, socketBufferSize)
20+
})
21+
},
22+
)
23+
}

0 commit comments

Comments
 (0)