Skip to content

Commit

Permalink
Add tcpMaxSeg to sockopt (XTLS#2002)
Browse files Browse the repository at this point in the history
  • Loading branch information
xqzr authored Apr 30, 2023
1 parent 18e5b09 commit dd81ad5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
2 changes: 2 additions & 0 deletions infra/conf/transport_internet.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ type SocketConfig struct {
TCPKeepAliveIdle int32 `json:"tcpKeepAliveIdle"`
TCPCongestion string `json:"tcpCongestion"`
TCPWindowClamp int32 `json:"tcpWindowClamp"`
TCPMaxSeg int32 `json:"tcpMaxSeg"`
TCPUserTimeout int32 `json:"tcpUserTimeout"`
V6only bool `json:"v6only"`
Interface string `json:"interface"`
Expand Down Expand Up @@ -670,6 +671,7 @@ func (c *SocketConfig) Build() (*internet.SocketConfig, error) {
TcpKeepAliveIdle: c.TCPKeepAliveIdle,
TcpCongestion: c.TCPCongestion,
TcpWindowClamp: c.TCPWindowClamp,
TcpMaxSeg: c.TCPMaxSeg,
TcpUserTimeout: c.TCPUserTimeout,
V6Only: c.V6only,
Interface: c.Interface,
Expand Down
14 changes: 12 additions & 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,6 @@ message SocketConfig {
int32 tcp_window_clamp = 15;

int32 tcp_user_timeout = 16;

int32 tcp_max_seg = 17;
}
13 changes: 13 additions & 0 deletions transport/internet/sockopt_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf
return newError("failed to set TCP_USER_TIMEOUT", err)
}
}

if config.TcpMaxSeg > 0 {
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_MAXSEG, int(config.TcpMaxSeg)); err != nil {
return newError("failed to set TCP_MAXSEG", err)
}
}

}

if config.Tproxy.IsEnabled() {
Expand Down Expand Up @@ -156,6 +163,12 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
return newError("failed to set TCP_USER_TIMEOUT", err)
}
}

if config.TcpMaxSeg > 0 {
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_MAXSEG, int(config.TcpMaxSeg)); err != nil {
return newError("failed to set TCP_MAXSEG", err)
}
}
}

if config.Tproxy.IsEnabled() {
Expand Down

0 comments on commit dd81ad5

Please sign in to comment.