-
Notifications
You must be signed in to change notification settings - Fork 402
/
fastopen_linux.go
63 lines (55 loc) · 1.9 KB
/
fastopen_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright (C) 2023 Storj Labs, Inc.
// See LICENSE for copying information.
package server
import (
"fmt"
"os"
"strconv"
"strings"
"sync"
"syscall"
"go.uber.org/zap"
)
const tcpFastOpen = 0x17
const tcpFastOpenSysctlPath = "/proc/sys/net/ipv4/tcp_fastopen"
func setTCPFastOpen(fd uintptr, queue int) error {
return syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, tcpFastOpen, queue)
}
var tryInitFastOpenOnce sync.Once
var initFastOpenPossiblyEnabled bool
// tryInitFastOpen returns true if fastopen support is possibly enabled.
func tryInitFastOpen(log *zap.Logger) bool {
tryInitFastOpenOnce.Do(func() {
data, err := os.ReadFile(tcpFastOpenSysctlPath)
if err != nil {
log.Sugar().Infof("kernel support for tcp fast open unknown")
initFastOpenPossiblyEnabled = true
return
}
fastOpenFlags, err := strconv.Atoi(strings.TrimSpace(string(data)))
if err != nil {
log.Sugar().Infof("kernel support for tcp fast open unparsable")
initFastOpenPossiblyEnabled = true
return
}
if fastOpenFlags&0x2 != 0 {
log.Sugar().Infof("existing kernel support for server-side tcp fast open detected")
initFastOpenPossiblyEnabled = true
return
}
err = os.WriteFile(tcpFastOpenSysctlPath, []byte(fmt.Sprint(fastOpenFlags|0x2)), 0o644)
if err != nil {
log.Sugar().Infof("kernel support for server-side tcp fast open remains disabled.")
// really, it's just the secondmost least significant bit that needs to
// be flipped, but maybe this isn't the place to explain that. 0x3 will
// enable standard fast open with standard cookies for both clients and
// servers, so it's probably the right advice.
log.Sugar().Infof("enable with: sysctl -w net.ipv4.tcp_fastopen=3")
initFastOpenPossiblyEnabled = false
return
}
log.Sugar().Infof("kernel support for server-side tcp fast open enabled.")
initFastOpenPossiblyEnabled = true
})
return initFastOpenPossiblyEnabled
}