diff --git a/common/pool/pool.go b/common/pool/pool.go index e6354f11..bee4887f 100644 --- a/common/pool/pool.go +++ b/common/pool/pool.go @@ -5,6 +5,11 @@ const ( // but the maximum packet size of vmess/shadowsocks is about 16 KiB // so define a buffer of 20 KiB to reduce the memory of each TCP relay RelayBufferSize = 20 * 1024 + + // RelayBufferSize uses 20KiB, but due to the allocator it will actually + // request 32Kib. Most UDPs are smaller than the MTU, and the TUN's MTU + // set to 9000, so the UDP Buffer size set to 16Kib + UDPBufferSize = 16 * 1024 ) func Get(size int) []byte { diff --git a/listener/socks/udp.go b/listener/socks/udp.go index a2d21508..8bc439fb 100644 --- a/listener/socks/udp.go +++ b/listener/socks/udp.go @@ -49,7 +49,7 @@ func NewUDP(addr string, in chan<- *inbound.PacketAdapter) (*UDPListener, error) } go func() { for { - buf := pool.Get(pool.RelayBufferSize) + buf := pool.Get(pool.UDPBufferSize) n, remoteAddr, err := l.ReadFrom(buf) if err != nil { pool.Put(buf) diff --git a/listener/tproxy/udp.go b/listener/tproxy/udp.go index f3d8dbb3..c7e6d99e 100644 --- a/listener/tproxy/udp.go +++ b/listener/tproxy/udp.go @@ -57,7 +57,7 @@ func NewUDP(addr string, in chan<- *inbound.PacketAdapter) (*UDPListener, error) go func() { oob := make([]byte, 1024) for { - buf := pool.Get(pool.RelayBufferSize) + buf := pool.Get(pool.UDPBufferSize) n, oobn, _, lAddr, err := c.ReadMsgUDP(buf, oob) if err != nil { pool.Put(buf) diff --git a/tunnel/connection.go b/tunnel/connection.go index 8c3b4cb6..45de46d7 100644 --- a/tunnel/connection.go +++ b/tunnel/connection.go @@ -39,7 +39,7 @@ func handleUDPToRemote(packet C.UDPPacket, pc C.PacketConn, metadata *C.Metadata } func handleUDPToLocal(packet C.UDPPacket, pc net.PacketConn, key string, fAddr net.Addr) { - buf := pool.Get(pool.RelayBufferSize) + buf := pool.Get(pool.UDPBufferSize) defer pool.Put(buf) defer natTable.Delete(key) defer pc.Close()