hotfix: use 64k buffer to read tun device packet for windows tun device mtu 65535 (#404)

This commit is contained in:
naison
2024-12-17 11:46:09 +08:00
committed by GitHub
parent 51166477c2
commit 68d550a80d
8 changed files with 43 additions and 43 deletions

View File

@@ -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 (

View File

@@ -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)

View File

@@ -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()

View File

@@ -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)

View File

@@ -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[:])
}
}
}

View File

@@ -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

View File

@@ -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])

View File

@@ -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)