diff --git a/pkg/config/config.go b/pkg/config/config.go index cf792191..e379d034 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -128,9 +128,9 @@ func init() { var Debug bool var ( - SmallBufferSize = 2 * 1024 // 2KB small buffer - MediumBufferSize = 8 * 1024 // 8KB medium buffer - LargeBufferSize = 32 * 1024 // 32KB large buffer + SmallBufferSize = 8 * 1024 // 8KB small buffer + MediumBufferSize = 32 * 1024 // 32KB medium buffer + LargeBufferSize = 64 * 1024 // 64KB large buffer ) var ( diff --git a/pkg/core/gvisortcpforwarder.go b/pkg/core/gvisortcpforwarder.go index 1b4ca8ce..6efbb2cf 100644 --- a/pkg/core/gvisortcpforwarder.go +++ b/pkg/core/gvisortcpforwarder.go @@ -73,8 +73,8 @@ func TCPForwarder(s *stack.Stack, ctx context.Context) func(stack.TransportEndpo func WriteProxyInfo(conn net.Conn, id stack.TransportEndpointID) error { var b bytes.Buffer - i := config.MPool.Get().([]byte)[:] - defer config.MPool.Put(i[:]) + i := config.LPool.Get().([]byte)[:] + defer config.LPool.Put(i[:]) binary.BigEndian.PutUint16(i, id.LocalPort) b.Write(i) binary.BigEndian.PutUint16(i, id.RemotePort) diff --git a/pkg/core/gvisortunendpoint.go b/pkg/core/gvisortunendpoint.go index 3c0bf908..3b0b5ce9 100755 --- a/pkg/core/gvisortunendpoint.go +++ b/pkg/core/gvisortunendpoint.go @@ -50,16 +50,16 @@ func (h *gvisorTCPHandler) readFromTCPConnWriteToEndpoint(ctx context.Context, c default: } - buf := config.SPool.Get().([]byte)[:] + buf := config.LPool.Get().([]byte)[:] read, err := tcpConn.Read(buf[:]) if err != nil { log.Errorf("[TUN] Failed to read from tcp conn: %v", err) - config.SPool.Put(buf[:]) + config.LPool.Put(buf[:]) return } if read == 0 { log.Warnf("[TUN] Read from tcp conn length is %d", read) - config.SPool.Put(buf[:]) + config.LPool.Put(buf[:]) continue } // Try to determine network protocol number, default zero. @@ -73,7 +73,7 @@ func (h *gvisorTCPHandler) readFromTCPConnWriteToEndpoint(ctx context.Context, c ipHeader, err := ipv4.ParseHeader(buf[:read]) if err != nil { log.Errorf("Failed to parse IPv4 header: %v", err) - config.SPool.Put(buf[:]) + config.LPool.Put(buf[:]) continue } ipProtocol = ipHeader.Protocol @@ -84,7 +84,7 @@ func (h *gvisorTCPHandler) readFromTCPConnWriteToEndpoint(ctx context.Context, c ipHeader, err := ipv6.ParseHeader(buf[:read]) if err != nil { log.Errorf("Failed to parse IPv6 header: %s", err.Error()) - config.SPool.Put(buf[:]) + config.LPool.Put(buf[:]) continue } ipProtocol = ipHeader.NextHeader @@ -92,7 +92,7 @@ func (h *gvisorTCPHandler) readFromTCPConnWriteToEndpoint(ctx context.Context, c dst = ipHeader.Dst } else { log.Debugf("[TUN-GVISOR] Unknown packet") - config.SPool.Put(buf[:]) + config.LPool.Put(buf[:]) continue } @@ -111,7 +111,7 @@ func (h *gvisorTCPHandler) readFromTCPConnWriteToEndpoint(ctx context.Context, c ReserveHeaderBytes: 0, Payload: buffer.MakeWithData(buf[:read]), }) - config.SPool.Put(buf[:]) + config.LPool.Put(buf[:]) sniffer.LogPacket("[gVISOR] ", sniffer.DirectionRecv, protocol, pkt) endpoint.InjectInbound(protocol, pkt) pkt.DecRef() diff --git a/pkg/core/tcphandler.go b/pkg/core/tcphandler.go index b4532269..5df5ee70 100644 --- a/pkg/core/tcphandler.go +++ b/pkg/core/tcphandler.go @@ -78,11 +78,11 @@ func (h *fakeUdpHandler) Handle(ctx context.Context, tcpConn net.Conn) { default: } - buf := config.SPool.Get().([]byte)[:] + buf := config.LPool.Get().([]byte)[:] dgram, err := readDatagramPacketServer(tcpConn, buf[:]) if err != nil { log.Errorf("[TCP] %s -> %s : %v", tcpConn.RemoteAddr(), tcpConn.LocalAddr(), err) - config.SPool.Put(buf[:]) + config.LPool.Put(buf[:]) return } @@ -90,7 +90,7 @@ func (h *fakeUdpHandler) Handle(ctx context.Context, tcpConn net.Conn) { src, _, err = util.ParseIP(dgram.Data[:dgram.DataLength]) if err != nil { log.Errorf("[TCP] Unknown packet") - config.SPool.Put(buf[:]) + config.LPool.Put(buf[:]) continue } value, loaded := h.routeMapTCP.LoadOrStore(src.String(), tcpConn) diff --git a/pkg/core/tunhandler.go b/pkg/core/tunhandler.go index 5c14a17e..b99f7c64 100644 --- a/pkg/core/tunhandler.go +++ b/pkg/core/tunhandler.go @@ -96,24 +96,24 @@ type Device struct { func (d *Device) readFromTun() { for { - buf := config.SPool.Get().([]byte)[:] + buf := config.LPool.Get().([]byte)[:] n, err := d.tun.Read(buf[:]) if err != nil { - config.SPool.Put(buf[:]) + config.LPool.Put(buf[:]) log.Errorf("[TUN] Failed to read from tun: %v", err) util.SafeWrite(d.chExit, err) return } if n == 0 { log.Errorf("[TUN] Read packet length 0") - config.SPool.Put(buf[:]) + config.LPool.Put(buf[:]) continue } src, dst, err := util.ParseIP(buf[:n]) if err != nil { log.Errorf("[TUN] Unknown packet") - config.SPool.Put(buf[:]) + config.LPool.Put(buf[:]) continue } @@ -130,7 +130,7 @@ func (d *Device) readFromTun() { func (d *Device) writeToTun() { for e := range d.tunOutbound { _, err := d.tun.Write(e.data[:e.length]) - config.SPool.Put(e.data[:]) + config.LPool.Put(e.data[:]) if err != nil { util.SafeWrite(d.chExit, err) return @@ -278,17 +278,17 @@ func (p *Peer) sendErr(err error) { func (p *Peer) readFromConn() { for { - buf := config.SPool.Get().([]byte)[:] + buf := config.LPool.Get().([]byte)[:] n, from, err := p.conn.ReadFrom(buf[:]) if err != nil { - config.SPool.Put(buf[:]) + config.LPool.Put(buf[:]) p.sendErr(err) return } src, dst, err := util.ParseIP(buf[:n]) if err != nil { - config.SPool.Put(buf[:]) + config.LPool.Put(buf[:]) log.Errorf("[TUN] Unknown packet: %v", err) continue } @@ -316,7 +316,7 @@ func (p *Peer) readFromTCPConn() { src, dst, err := util.ParseIP(packet.Data) if err != nil { log.Errorf("[TUN] Unknown packet") - config.SPool.Put(packet.Data[:]) + config.LPool.Put(packet.Data[:]) continue } u := &udpElem{ @@ -335,7 +335,7 @@ func (p *Peer) routePeer() { if routeToAddr := p.routeMapUDP.RouteTo(e.dst); routeToAddr != nil { log.Debugf("[UDP] Find UDP route to dst: %s -> %s", e.dst, routeToAddr) _, err := p.conn.WriteTo(e.data[:e.length], routeToAddr) - config.SPool.Put(e.data[:]) + config.LPool.Put(e.data[:]) if err != nil { p.sendErr(err) return @@ -344,7 +344,7 @@ func (p *Peer) routePeer() { log.Debugf("[TCP] Find TCP route to dst: %s -> %s", e.dst.String(), conn.(net.Conn).RemoteAddr()) dgram := newDatagramPacket(e.data[:e.length]) err := dgram.Write(conn.(net.Conn)) - config.SPool.Put(e.data[:]) + config.LPool.Put(e.data[:]) if err != nil { log.Errorf("[TCP] udp-tun %s <- %s : %s", conn.(net.Conn).RemoteAddr(), dgram.Addr(), err) p.sendErr(err) @@ -367,7 +367,7 @@ func (p *Peer) routeTUN() { if addr := p.routeMapUDP.RouteTo(e.dst); addr != nil { log.Debugf("[TUN] Find UDP route to dst: %s -> %s", e.dst, addr) _, err := p.conn.WriteTo(e.data[:e.length], addr) - config.SPool.Put(e.data[:]) + config.LPool.Put(e.data[:]) if err != nil { log.Debugf("[TUN] Failed wirte to route dst: %s -> %s", e.dst, addr) p.sendErr(err) @@ -377,7 +377,7 @@ func (p *Peer) routeTUN() { log.Debugf("[TUN] Find TCP route to dst: %s -> %s", e.dst.String(), conn.(net.Conn).RemoteAddr()) dgram := newDatagramPacket(e.data[:e.length]) err := dgram.Write(conn.(net.Conn)) - config.SPool.Put(e.data[:]) + config.LPool.Put(e.data[:]) if err != nil { log.Errorf("[TUN] Failed to write TCP %s <- %s : %s", conn.(net.Conn).RemoteAddr(), dgram.Addr(), err) p.sendErr(err) @@ -385,7 +385,7 @@ func (p *Peer) routeTUN() { } } else { log.Errorf("[TUN] No route for src: %s -> dst: %s, drop it", e.src, e.dst) - config.SPool.Put(e.data[:]) + config.LPool.Put(e.data[:]) } } } diff --git a/pkg/core/tunhandlerclient.go b/pkg/core/tunhandlerclient.go index ab5cdfa2..bcfb71f5 100644 --- a/pkg/core/tunhandlerclient.go +++ b/pkg/core/tunhandlerclient.go @@ -87,7 +87,7 @@ func transportTunClient(ctx context.Context, tunInbound <-chan *DataElem, tunOut continue } _, err := packetConn.WriteTo(e.data[:e.length], remoteAddr) - config.SPool.Put(e.data[:]) + config.LPool.Put(e.data[:]) if err != nil { util.SafeWrite(errChan, errors.Wrap(err, fmt.Sprintf("failed to write packet to remote %s", remoteAddr))) return @@ -97,10 +97,10 @@ func transportTunClient(ctx context.Context, tunInbound <-chan *DataElem, tunOut go func() { for { - buf := config.SPool.Get().([]byte)[:] + buf := config.LPool.Get().([]byte)[:] n, _, err := packetConn.ReadFrom(buf[:]) if err != nil { - config.SPool.Put(buf[:]) + config.LPool.Put(buf[:]) util.SafeWrite(errChan, errors.Wrap(err, fmt.Sprintf("failed to read packet from remote %s", remoteAddr))) return } @@ -146,15 +146,15 @@ func (d *ClientDevice) SetTunInboundHandler(handler func(tunInbound <-chan *Data func (d *ClientDevice) readFromTun() { for { - buf := config.SPool.Get().([]byte)[:] + buf := config.LPool.Get().([]byte)[:] n, err := d.tun.Read(buf[:]) if err != nil { util.SafeWrite(d.chExit, err) - config.SPool.Put(buf[:]) + config.LPool.Put(buf[:]) return } if n == 0 { - config.SPool.Put(buf[:]) + config.LPool.Put(buf[:]) continue } @@ -163,7 +163,7 @@ func (d *ClientDevice) readFromTun() { src, dst, err = util.ParseIP(buf[:n]) if err != nil { log.Debugf("[TUN-GVISOR] Unknown packet: %v", err) - config.SPool.Put(buf[:]) + config.LPool.Put(buf[:]) continue } log.Tracef("[TUN-RAW] SRC: %s, DST: %s, Length: %d", src.String(), dst, n) @@ -174,7 +174,7 @@ func (d *ClientDevice) readFromTun() { func (d *ClientDevice) writeToTun() { for e := range d.tunOutbound { _, err := d.tun.Write(e.data[:e.length]) - config.SPool.Put(e.data[:]) + config.LPool.Put(e.data[:]) if err != nil { util.SafeWrite(d.chExit, err) return diff --git a/pkg/core/udpovertcp.go b/pkg/core/udpovertcp.go index 9bb7dab4..60859543 100644 --- a/pkg/core/udpovertcp.go +++ b/pkg/core/udpovertcp.go @@ -61,8 +61,8 @@ func readDatagramPacketServer(r io.Reader, b []byte) (*datagramPacket, error) { } func (addr *datagramPacket) Write(w io.Writer) error { - buf := config.MPool.Get().([]byte)[:] - defer config.MPool.Put(buf[:]) + buf := config.LPool.Get().([]byte)[:] + defer config.LPool.Put(buf[:]) binary.BigEndian.PutUint16(buf[:2], uint16(len(addr.Data))) n := copy(buf[2:], addr.Data) _, err := w.Write(buf[:n+2]) diff --git a/pkg/tun/tun.go b/pkg/tun/tun.go index a43313cb..e507b56c 100644 --- a/pkg/tun/tun.go +++ b/pkg/tun/tun.go @@ -84,8 +84,8 @@ type tunConn struct { func (c *tunConn) Read(b []byte) (n int, err error) { offset := device.MessageTransportHeaderSize - buf := config.MPool.Get().([]byte)[:] - defer config.MPool.Put(buf[:]) + buf := config.LPool.Get().([]byte)[:] + defer config.LPool.Put(buf[:]) var size int size, err = c.ifce.Read(buf[:], offset) @@ -102,8 +102,8 @@ func (c *tunConn) Write(b []byte) (n int, err error) { if len(b) < device.MessageTransportHeaderSize { return 0, err } - buf := config.MPool.Get().([]byte)[:] - defer config.MPool.Put(buf[:]) + buf := config.LPool.Get().([]byte)[:] + defer config.LPool.Put(buf[:]) copy(buf[device.MessageTransportOffsetContent:], b)