Skip to content

Commit

Permalink
📡 linux: use Go's listener backlog as TFO backlog
Browse files Browse the repository at this point in the history
Previously we hardcoded Linux's default net.core.somaxconn (4096) as the default TFO backlog. Turns out it's quite easy to use linkname to retrieve Go's listener backlog, so we changed to do this in this commit, as well as fixing some related comments.
  • Loading branch information
database64128 committed Mar 14, 2024
1 parent 7e12621 commit 12287b1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
3 changes: 2 additions & 1 deletion sockopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ func SetTFOListener(fd uintptr) error {
}

// SetTFOListenerWithBacklog enables TCP Fast Open on the listener with the given backlog.
// If the platform does not support custom backlog, the specified backlog is ignored.
// If the backlog is 0, Go std's listen(2) backlog is used.
// If the platform does not support custom backlog values, the given backlog is ignored.
func SetTFOListenerWithBacklog(fd uintptr, backlog int) error {
return setTFOListenerWithBacklog(fd, backlog) // sockopt_linux.go, sockopt_listen_generic.go, sockopt_stub.go
}
Expand Down
18 changes: 15 additions & 3 deletions sockopt_linux.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
package tfo

import "golang.org/x/sys/unix"
import (
_ "unsafe"

"golang.org/x/sys/unix"
)

// TCPFastopenQueueLength is the maximum number of total pending TFO connection requests,
// see https://datatracker.ietf.org/doc/html/rfc7413#section-5.1 for why this limit exists.
// The current value aligns with Go std's listen(2) backlog (4096, as of the current version).
// The current value is the default net.core.somaxconn on Linux.
//
// Deprecated: This constant is no longer used in this module and will be removed in v3.
const TCPFastopenQueueLength = 4096

func setTFOListener(fd uintptr) error {
return setTFOListenerWithBacklog(fd, defaultBacklog)
return setTFOListenerWithBacklog(fd, 0)
}

func setTFOListenerWithBacklog(fd uintptr, backlog int) error {
if backlog == 0 {
backlog = listenerBacklog()
}
return setTFO(int(fd), backlog)
}

// listenerBacklog is linked from src/net/net.go
//
//go:linkname listenerBacklog net.listenerBacklog
func listenerBacklog() int

func setTFODialer(fd uintptr) error {
return unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_FASTOPEN_CONNECT, 1)
}
2 changes: 1 addition & 1 deletion tfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type ListenConfig struct {
net.ListenConfig

// Backlog specifies the maximum number of pending TFO connections on supported platforms.
// If the value is 0, Go std's listen(2) backlog (4096, as of the current version) is used.
// If the value is 0, Go std's listen(2) backlog is used.
// If the value is negative, TFO is disabled.
Backlog int

Expand Down
7 changes: 0 additions & 7 deletions tfo_listen_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,9 @@ import (
"syscall"
)

// defaultBacklog is Go std's listen(2) backlog.
// We use this as the default TFO backlog.
const defaultBacklog = 4096

func (lc *ListenConfig) listenTFO(ctx context.Context, network, address string) (net.Listener, error) {
ctrlFn := lc.Control
backlog := lc.Backlog
if backlog == 0 {
backlog = defaultBacklog
}
llc := *lc
llc.Control = func(network, address string, c syscall.RawConn) (err error) {
if ctrlFn != nil {
Expand Down

0 comments on commit 12287b1

Please sign in to comment.