Browse Source

Chore: separate send/recv buffer

pull/133/head
xjasonlyu 3 years ago
parent
commit
9f239d146b
  1. 26
      core/option/option.go

26
core/option/option.go

@ -47,14 +47,18 @@ const (
tcpRecovery = tcpip.TCPRACKLossDetection tcpRecovery = tcpip.TCPRACKLossDetection
// tcpMinBufferSize is the smallest size of a send/recv buffer. // tcpMinBufferSize is the smallest size of a send/recv buffer.
tcpMinBufferSize = tcp.MinBufferSize // 4 KiB tcpMinBufferSize = tcp.MinBufferSize
// tcpMaxBufferSize is the maximum permitted size of a send/recv buffer. // tcpMaxBufferSize is the maximum permitted size of a send/recv buffer.
tcpMaxBufferSize = tcp.MaxBufferSize // 4 MiB tcpMaxBufferSize = tcp.MaxBufferSize
// tcpDefaultBufferSize is the default size of the send/recv buffer for // tcpDefaultReceiveBufferSize is the default size of the receive buffer
// for a transport endpoint.
tcpDefaultReceiveBufferSize = stack.DefaultBufferSize
// tcpDefaultBufferSize is the default size of the send buffer for
// a transport endpoint. // a transport endpoint.
tcpDefaultBufferSize = 212 << 10 // 212 KiB tcpDefaultSendBufferSize = stack.DefaultBufferSize
) )
type Option func(*stack.Stack) error type Option func(*stack.Stack) error
@ -74,7 +78,8 @@ func WithDefault() Option {
// in too large buffers. // in too large buffers.
// //
// Ref: https://github.com/cloudflare/slirpnetstack/blob/master/stack.go // Ref: https://github.com/cloudflare/slirpnetstack/blob/master/stack.go
WithTCPBufferSizeRange(tcpMinBufferSize, tcpDefaultBufferSize, tcpMaxBufferSize), WithTCPReceiveBufferSizeRange(tcpMinBufferSize, tcpDefaultReceiveBufferSize, tcpMaxBufferSize),
WithTCPSendBufferSizeRange(tcpMinBufferSize, tcpDefaultSendBufferSize, tcpMaxBufferSize),
WithTCPCongestionControl(tcpCongestionControlAlgorithm), WithTCPCongestionControl(tcpCongestionControlAlgorithm),
WithTCPDelay(tcpDelayEnabled), WithTCPDelay(tcpDelayEnabled),
@ -154,13 +159,20 @@ func WithICMPLimit(limit rate.Limit) Option {
} }
} }
// WithTCPBufferSizeRange sets the receive and send buffer size range for TCP. // WithTCPReceiveBufferSizeRange sets the receive buffer size range for TCP.
func WithTCPBufferSizeRange(a, b, c int) Option { func WithTCPReceiveBufferSizeRange(a, b, c int) Option {
return func(s *stack.Stack) error { return func(s *stack.Stack) error {
rcvOpt := tcpip.TCPReceiveBufferSizeRangeOption{Min: a, Default: b, Max: c} rcvOpt := tcpip.TCPReceiveBufferSizeRangeOption{Min: a, Default: b, Max: c}
if err := s.SetTransportProtocolOption(tcp.ProtocolNumber, &rcvOpt); err != nil { if err := s.SetTransportProtocolOption(tcp.ProtocolNumber, &rcvOpt); err != nil {
return fmt.Errorf("set TCP receive buffer size range: %s", err) return fmt.Errorf("set TCP receive buffer size range: %s", err)
} }
return nil
}
}
// WithTCPSendBufferSizeRange sets the send buffer size range for TCP.
func WithTCPSendBufferSizeRange(a, b, c int) Option {
return func(s *stack.Stack) error {
sndOpt := tcpip.TCPSendBufferSizeRangeOption{Min: a, Default: b, Max: c} sndOpt := tcpip.TCPSendBufferSizeRangeOption{Min: a, Default: b, Max: c}
if err := s.SetTransportProtocolOption(tcp.ProtocolNumber, &sndOpt); err != nil { if err := s.SetTransportProtocolOption(tcp.ProtocolNumber, &sndOpt); err != nil {
return fmt.Errorf("set TCP send buffer size range: %s", err) return fmt.Errorf("set TCP send buffer size range: %s", err)

Loading…
Cancel
Save