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 authored and xiaokangwang committed Sep 27, 2021
1 parent 7d78683 commit be4dd56
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 27 deletions.
8 changes: 8 additions & 0 deletions infra/conf/cfgcommon/socketcfg/socket.go
Expand Up @@ -11,6 +11,7 @@ type SocketConfig struct {
TProxy string `json:"tproxy"`
AcceptProxyProtocol bool `json:"acceptProxyProtocol"`
TCPKeepAliveInterval int32 `json:"tcpKeepAliveInterval"`
TFOQueueLength uint32 `json:"tcpFastOpenQueueLength"`
}

// Build implements Buildable.
Expand All @@ -23,6 +24,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 @@ -36,6 +43,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/v4/transport_test.go
Expand Up @@ -37,12 +37,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
55 changes: 33 additions & 22 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 be4dd56

Please sign in to comment.