Skip to content

Commit

Permalink
Feat: custom TCP Fast Open queue length (#1293)
Browse files Browse the repository at this point in the history
* Feat: custom TCP Fast Open queue length

* Feat: change default TFO queue length to 4096
  • Loading branch information
AkinoKaede committed Sep 26, 2021
1 parent 0054bff commit b25a9e5
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 29 deletions.
8 changes: 8 additions & 0 deletions infra/conf/transport_internet.go
Expand Up @@ -387,6 +387,7 @@ func (p TransportProtocol) Build() (string, error) {
type SocketConfig struct {
Mark uint32 `json:"mark"`
TFO *bool `json:"tcpFastOpen"`
TFOQueueLength uint32 `json:"tcpFastOpenQueueLength"`
TProxy string `json:"tproxy"`
AcceptProxyProtocol bool `json:"acceptProxyProtocol"`
TCPKeepAliveInterval int32 `json:"tcpKeepAliveInterval"`
Expand All @@ -402,6 +403,12 @@ func (c *SocketConfig) Build() (*internet.SocketConfig, error) {
tfoSettings = internet.SocketConfig_Disable
}
}

tfoQueueLength := c.TFOQueueLength
if tfoQueueLength == 0 {
tfoQueueLength = 4096
}

var tproxy internet.SocketConfig_TProxyMode
switch strings.ToLower(c.TProxy) {
case "tproxy":
Expand All @@ -415,6 +422,7 @@ func (c *SocketConfig) Build() (*internet.SocketConfig, error) {
return &internet.SocketConfig{
Mark: c.Mark,
Tfo: tfoSettings,
TfoQueueLength: tfoQueueLength,
Tproxy: tproxy,
AcceptProxyProtocol: c.AcceptProxyProtocol,
TcpKeepAliveInterval: c.TCPKeepAliveInterval,
Expand Down
8 changes: 5 additions & 3 deletions infra/conf/transport_test.go
Expand Up @@ -35,12 +35,14 @@ func TestSocketConfig(t *testing.T) {
{
Input: `{
"mark": 1,
"tcpFastOpen": true
"tcpFastOpen": true,
"tcpFastOpenQueueLength": 1024
}`,
Parser: createParser(),
Output: &internet.SocketConfig{
Mark: 1,
Tfo: internet.SocketConfig_Enable,
Mark: 1,
Tfo: internet.SocketConfig_Enable,
TfoQueueLength: 1024,
},
},
})
Expand Down
58 changes: 34 additions & 24 deletions transport/internet/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions transport/internet/config.proto
Expand Up @@ -93,4 +93,6 @@ message SocketConfig {
bool accept_proxy_protocol = 7;

int32 tcp_keep_alive_interval = 8;

uint32 tfo_queue_length = 9;
}
4 changes: 2 additions & 2 deletions transport/internet/sockopt_linux.go
Expand Up @@ -84,8 +84,8 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
if isTCPSocket(network) {
switch config.Tfo {
case SocketConfig_Enable:
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN, 1); err != nil {
return newError("failed to set TCP_FASTOPEN=1").Base(err)
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN, int(config.TfoQueueLength)); err != nil {
return newError("failed to set TCP_FASTOPEN=", config.TfoQueueLength).Base(err)
}
case SocketConfig_Disable:
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN, 0); err != nil {
Expand Down

0 comments on commit b25a9e5

Please sign in to comment.