mirror of
https://github.com/kubenetworks/kubevpn.git
synced 2025-10-06 15:57:01 +08:00
hotfix: use 64k buffer to read tun device packet for windows tun device mtu 65535 (#404)
This commit is contained in:
@@ -128,9 +128,9 @@ func init() {
|
|||||||
var Debug bool
|
var Debug bool
|
||||||
|
|
||||||
var (
|
var (
|
||||||
SmallBufferSize = 2 * 1024 // 2KB small buffer
|
SmallBufferSize = 8 * 1024 // 8KB small buffer
|
||||||
MediumBufferSize = 8 * 1024 // 8KB medium buffer
|
MediumBufferSize = 32 * 1024 // 32KB medium buffer
|
||||||
LargeBufferSize = 32 * 1024 // 32KB large buffer
|
LargeBufferSize = 64 * 1024 // 64KB large buffer
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@@ -73,8 +73,8 @@ func TCPForwarder(s *stack.Stack, ctx context.Context) func(stack.TransportEndpo
|
|||||||
|
|
||||||
func WriteProxyInfo(conn net.Conn, id stack.TransportEndpointID) error {
|
func WriteProxyInfo(conn net.Conn, id stack.TransportEndpointID) error {
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
i := config.MPool.Get().([]byte)[:]
|
i := config.LPool.Get().([]byte)[:]
|
||||||
defer config.MPool.Put(i[:])
|
defer config.LPool.Put(i[:])
|
||||||
binary.BigEndian.PutUint16(i, id.LocalPort)
|
binary.BigEndian.PutUint16(i, id.LocalPort)
|
||||||
b.Write(i)
|
b.Write(i)
|
||||||
binary.BigEndian.PutUint16(i, id.RemotePort)
|
binary.BigEndian.PutUint16(i, id.RemotePort)
|
||||||
|
@@ -50,16 +50,16 @@ func (h *gvisorTCPHandler) readFromTCPConnWriteToEndpoint(ctx context.Context, c
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := config.SPool.Get().([]byte)[:]
|
buf := config.LPool.Get().([]byte)[:]
|
||||||
read, err := tcpConn.Read(buf[:])
|
read, err := tcpConn.Read(buf[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("[TUN] Failed to read from tcp conn: %v", err)
|
log.Errorf("[TUN] Failed to read from tcp conn: %v", err)
|
||||||
config.SPool.Put(buf[:])
|
config.LPool.Put(buf[:])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if read == 0 {
|
if read == 0 {
|
||||||
log.Warnf("[TUN] Read from tcp conn length is %d", read)
|
log.Warnf("[TUN] Read from tcp conn length is %d", read)
|
||||||
config.SPool.Put(buf[:])
|
config.LPool.Put(buf[:])
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// Try to determine network protocol number, default zero.
|
// 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])
|
ipHeader, err := ipv4.ParseHeader(buf[:read])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to parse IPv4 header: %v", err)
|
log.Errorf("Failed to parse IPv4 header: %v", err)
|
||||||
config.SPool.Put(buf[:])
|
config.LPool.Put(buf[:])
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
ipProtocol = ipHeader.Protocol
|
ipProtocol = ipHeader.Protocol
|
||||||
@@ -84,7 +84,7 @@ func (h *gvisorTCPHandler) readFromTCPConnWriteToEndpoint(ctx context.Context, c
|
|||||||
ipHeader, err := ipv6.ParseHeader(buf[:read])
|
ipHeader, err := ipv6.ParseHeader(buf[:read])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to parse IPv6 header: %s", err.Error())
|
log.Errorf("Failed to parse IPv6 header: %s", err.Error())
|
||||||
config.SPool.Put(buf[:])
|
config.LPool.Put(buf[:])
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
ipProtocol = ipHeader.NextHeader
|
ipProtocol = ipHeader.NextHeader
|
||||||
@@ -92,7 +92,7 @@ func (h *gvisorTCPHandler) readFromTCPConnWriteToEndpoint(ctx context.Context, c
|
|||||||
dst = ipHeader.Dst
|
dst = ipHeader.Dst
|
||||||
} else {
|
} else {
|
||||||
log.Debugf("[TUN-GVISOR] Unknown packet")
|
log.Debugf("[TUN-GVISOR] Unknown packet")
|
||||||
config.SPool.Put(buf[:])
|
config.LPool.Put(buf[:])
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,7 +111,7 @@ func (h *gvisorTCPHandler) readFromTCPConnWriteToEndpoint(ctx context.Context, c
|
|||||||
ReserveHeaderBytes: 0,
|
ReserveHeaderBytes: 0,
|
||||||
Payload: buffer.MakeWithData(buf[:read]),
|
Payload: buffer.MakeWithData(buf[:read]),
|
||||||
})
|
})
|
||||||
config.SPool.Put(buf[:])
|
config.LPool.Put(buf[:])
|
||||||
sniffer.LogPacket("[gVISOR] ", sniffer.DirectionRecv, protocol, pkt)
|
sniffer.LogPacket("[gVISOR] ", sniffer.DirectionRecv, protocol, pkt)
|
||||||
endpoint.InjectInbound(protocol, pkt)
|
endpoint.InjectInbound(protocol, pkt)
|
||||||
pkt.DecRef()
|
pkt.DecRef()
|
||||||
|
@@ -78,11 +78,11 @@ func (h *fakeUdpHandler) Handle(ctx context.Context, tcpConn net.Conn) {
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := config.SPool.Get().([]byte)[:]
|
buf := config.LPool.Get().([]byte)[:]
|
||||||
dgram, err := readDatagramPacketServer(tcpConn, buf[:])
|
dgram, err := readDatagramPacketServer(tcpConn, buf[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("[TCP] %s -> %s : %v", tcpConn.RemoteAddr(), tcpConn.LocalAddr(), err)
|
log.Errorf("[TCP] %s -> %s : %v", tcpConn.RemoteAddr(), tcpConn.LocalAddr(), err)
|
||||||
config.SPool.Put(buf[:])
|
config.LPool.Put(buf[:])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,7 +90,7 @@ func (h *fakeUdpHandler) Handle(ctx context.Context, tcpConn net.Conn) {
|
|||||||
src, _, err = util.ParseIP(dgram.Data[:dgram.DataLength])
|
src, _, err = util.ParseIP(dgram.Data[:dgram.DataLength])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("[TCP] Unknown packet")
|
log.Errorf("[TCP] Unknown packet")
|
||||||
config.SPool.Put(buf[:])
|
config.LPool.Put(buf[:])
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
value, loaded := h.routeMapTCP.LoadOrStore(src.String(), tcpConn)
|
value, loaded := h.routeMapTCP.LoadOrStore(src.String(), tcpConn)
|
||||||
|
@@ -96,24 +96,24 @@ type Device struct {
|
|||||||
|
|
||||||
func (d *Device) readFromTun() {
|
func (d *Device) readFromTun() {
|
||||||
for {
|
for {
|
||||||
buf := config.SPool.Get().([]byte)[:]
|
buf := config.LPool.Get().([]byte)[:]
|
||||||
n, err := d.tun.Read(buf[:])
|
n, err := d.tun.Read(buf[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
config.SPool.Put(buf[:])
|
config.LPool.Put(buf[:])
|
||||||
log.Errorf("[TUN] Failed to read from tun: %v", err)
|
log.Errorf("[TUN] Failed to read from tun: %v", err)
|
||||||
util.SafeWrite(d.chExit, err)
|
util.SafeWrite(d.chExit, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
log.Errorf("[TUN] Read packet length 0")
|
log.Errorf("[TUN] Read packet length 0")
|
||||||
config.SPool.Put(buf[:])
|
config.LPool.Put(buf[:])
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
src, dst, err := util.ParseIP(buf[:n])
|
src, dst, err := util.ParseIP(buf[:n])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("[TUN] Unknown packet")
|
log.Errorf("[TUN] Unknown packet")
|
||||||
config.SPool.Put(buf[:])
|
config.LPool.Put(buf[:])
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +130,7 @@ func (d *Device) readFromTun() {
|
|||||||
func (d *Device) writeToTun() {
|
func (d *Device) writeToTun() {
|
||||||
for e := range d.tunOutbound {
|
for e := range d.tunOutbound {
|
||||||
_, err := d.tun.Write(e.data[:e.length])
|
_, err := d.tun.Write(e.data[:e.length])
|
||||||
config.SPool.Put(e.data[:])
|
config.LPool.Put(e.data[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.SafeWrite(d.chExit, err)
|
util.SafeWrite(d.chExit, err)
|
||||||
return
|
return
|
||||||
@@ -278,17 +278,17 @@ func (p *Peer) sendErr(err error) {
|
|||||||
|
|
||||||
func (p *Peer) readFromConn() {
|
func (p *Peer) readFromConn() {
|
||||||
for {
|
for {
|
||||||
buf := config.SPool.Get().([]byte)[:]
|
buf := config.LPool.Get().([]byte)[:]
|
||||||
n, from, err := p.conn.ReadFrom(buf[:])
|
n, from, err := p.conn.ReadFrom(buf[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
config.SPool.Put(buf[:])
|
config.LPool.Put(buf[:])
|
||||||
p.sendErr(err)
|
p.sendErr(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
src, dst, err := util.ParseIP(buf[:n])
|
src, dst, err := util.ParseIP(buf[:n])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
config.SPool.Put(buf[:])
|
config.LPool.Put(buf[:])
|
||||||
log.Errorf("[TUN] Unknown packet: %v", err)
|
log.Errorf("[TUN] Unknown packet: %v", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -316,7 +316,7 @@ func (p *Peer) readFromTCPConn() {
|
|||||||
src, dst, err := util.ParseIP(packet.Data)
|
src, dst, err := util.ParseIP(packet.Data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("[TUN] Unknown packet")
|
log.Errorf("[TUN] Unknown packet")
|
||||||
config.SPool.Put(packet.Data[:])
|
config.LPool.Put(packet.Data[:])
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
u := &udpElem{
|
u := &udpElem{
|
||||||
@@ -335,7 +335,7 @@ func (p *Peer) routePeer() {
|
|||||||
if routeToAddr := p.routeMapUDP.RouteTo(e.dst); routeToAddr != nil {
|
if routeToAddr := p.routeMapUDP.RouteTo(e.dst); routeToAddr != nil {
|
||||||
log.Debugf("[UDP] Find UDP route to dst: %s -> %s", e.dst, routeToAddr)
|
log.Debugf("[UDP] Find UDP route to dst: %s -> %s", e.dst, routeToAddr)
|
||||||
_, err := p.conn.WriteTo(e.data[:e.length], routeToAddr)
|
_, err := p.conn.WriteTo(e.data[:e.length], routeToAddr)
|
||||||
config.SPool.Put(e.data[:])
|
config.LPool.Put(e.data[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.sendErr(err)
|
p.sendErr(err)
|
||||||
return
|
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())
|
log.Debugf("[TCP] Find TCP route to dst: %s -> %s", e.dst.String(), conn.(net.Conn).RemoteAddr())
|
||||||
dgram := newDatagramPacket(e.data[:e.length])
|
dgram := newDatagramPacket(e.data[:e.length])
|
||||||
err := dgram.Write(conn.(net.Conn))
|
err := dgram.Write(conn.(net.Conn))
|
||||||
config.SPool.Put(e.data[:])
|
config.LPool.Put(e.data[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("[TCP] udp-tun %s <- %s : %s", conn.(net.Conn).RemoteAddr(), dgram.Addr(), err)
|
log.Errorf("[TCP] udp-tun %s <- %s : %s", conn.(net.Conn).RemoteAddr(), dgram.Addr(), err)
|
||||||
p.sendErr(err)
|
p.sendErr(err)
|
||||||
@@ -367,7 +367,7 @@ func (p *Peer) routeTUN() {
|
|||||||
if addr := p.routeMapUDP.RouteTo(e.dst); addr != nil {
|
if addr := p.routeMapUDP.RouteTo(e.dst); addr != nil {
|
||||||
log.Debugf("[TUN] Find UDP route to dst: %s -> %s", e.dst, addr)
|
log.Debugf("[TUN] Find UDP route to dst: %s -> %s", e.dst, addr)
|
||||||
_, err := p.conn.WriteTo(e.data[:e.length], addr)
|
_, err := p.conn.WriteTo(e.data[:e.length], addr)
|
||||||
config.SPool.Put(e.data[:])
|
config.LPool.Put(e.data[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("[TUN] Failed wirte to route dst: %s -> %s", e.dst, addr)
|
log.Debugf("[TUN] Failed wirte to route dst: %s -> %s", e.dst, addr)
|
||||||
p.sendErr(err)
|
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())
|
log.Debugf("[TUN] Find TCP route to dst: %s -> %s", e.dst.String(), conn.(net.Conn).RemoteAddr())
|
||||||
dgram := newDatagramPacket(e.data[:e.length])
|
dgram := newDatagramPacket(e.data[:e.length])
|
||||||
err := dgram.Write(conn.(net.Conn))
|
err := dgram.Write(conn.(net.Conn))
|
||||||
config.SPool.Put(e.data[:])
|
config.LPool.Put(e.data[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("[TUN] Failed to write TCP %s <- %s : %s", conn.(net.Conn).RemoteAddr(), dgram.Addr(), err)
|
log.Errorf("[TUN] Failed to write TCP %s <- %s : %s", conn.(net.Conn).RemoteAddr(), dgram.Addr(), err)
|
||||||
p.sendErr(err)
|
p.sendErr(err)
|
||||||
@@ -385,7 +385,7 @@ func (p *Peer) routeTUN() {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Errorf("[TUN] No route for src: %s -> dst: %s, drop it", e.src, e.dst)
|
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[:])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -87,7 +87,7 @@ func transportTunClient(ctx context.Context, tunInbound <-chan *DataElem, tunOut
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
_, err := packetConn.WriteTo(e.data[:e.length], remoteAddr)
|
_, err := packetConn.WriteTo(e.data[:e.length], remoteAddr)
|
||||||
config.SPool.Put(e.data[:])
|
config.LPool.Put(e.data[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.SafeWrite(errChan, errors.Wrap(err, fmt.Sprintf("failed to write packet to remote %s", remoteAddr)))
|
util.SafeWrite(errChan, errors.Wrap(err, fmt.Sprintf("failed to write packet to remote %s", remoteAddr)))
|
||||||
return
|
return
|
||||||
@@ -97,10 +97,10 @@ func transportTunClient(ctx context.Context, tunInbound <-chan *DataElem, tunOut
|
|||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
buf := config.SPool.Get().([]byte)[:]
|
buf := config.LPool.Get().([]byte)[:]
|
||||||
n, _, err := packetConn.ReadFrom(buf[:])
|
n, _, err := packetConn.ReadFrom(buf[:])
|
||||||
if err != nil {
|
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)))
|
util.SafeWrite(errChan, errors.Wrap(err, fmt.Sprintf("failed to read packet from remote %s", remoteAddr)))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -146,15 +146,15 @@ func (d *ClientDevice) SetTunInboundHandler(handler func(tunInbound <-chan *Data
|
|||||||
|
|
||||||
func (d *ClientDevice) readFromTun() {
|
func (d *ClientDevice) readFromTun() {
|
||||||
for {
|
for {
|
||||||
buf := config.SPool.Get().([]byte)[:]
|
buf := config.LPool.Get().([]byte)[:]
|
||||||
n, err := d.tun.Read(buf[:])
|
n, err := d.tun.Read(buf[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.SafeWrite(d.chExit, err)
|
util.SafeWrite(d.chExit, err)
|
||||||
config.SPool.Put(buf[:])
|
config.LPool.Put(buf[:])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
config.SPool.Put(buf[:])
|
config.LPool.Put(buf[:])
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,7 +163,7 @@ func (d *ClientDevice) readFromTun() {
|
|||||||
src, dst, err = util.ParseIP(buf[:n])
|
src, dst, err = util.ParseIP(buf[:n])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("[TUN-GVISOR] Unknown packet: %v", err)
|
log.Debugf("[TUN-GVISOR] Unknown packet: %v", err)
|
||||||
config.SPool.Put(buf[:])
|
config.LPool.Put(buf[:])
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
log.Tracef("[TUN-RAW] SRC: %s, DST: %s, Length: %d", src.String(), dst, n)
|
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() {
|
func (d *ClientDevice) writeToTun() {
|
||||||
for e := range d.tunOutbound {
|
for e := range d.tunOutbound {
|
||||||
_, err := d.tun.Write(e.data[:e.length])
|
_, err := d.tun.Write(e.data[:e.length])
|
||||||
config.SPool.Put(e.data[:])
|
config.LPool.Put(e.data[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.SafeWrite(d.chExit, err)
|
util.SafeWrite(d.chExit, err)
|
||||||
return
|
return
|
||||||
|
@@ -61,8 +61,8 @@ func readDatagramPacketServer(r io.Reader, b []byte) (*datagramPacket, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (addr *datagramPacket) Write(w io.Writer) error {
|
func (addr *datagramPacket) Write(w io.Writer) error {
|
||||||
buf := config.MPool.Get().([]byte)[:]
|
buf := config.LPool.Get().([]byte)[:]
|
||||||
defer config.MPool.Put(buf[:])
|
defer config.LPool.Put(buf[:])
|
||||||
binary.BigEndian.PutUint16(buf[:2], uint16(len(addr.Data)))
|
binary.BigEndian.PutUint16(buf[:2], uint16(len(addr.Data)))
|
||||||
n := copy(buf[2:], addr.Data)
|
n := copy(buf[2:], addr.Data)
|
||||||
_, err := w.Write(buf[:n+2])
|
_, err := w.Write(buf[:n+2])
|
||||||
|
@@ -84,8 +84,8 @@ type tunConn struct {
|
|||||||
|
|
||||||
func (c *tunConn) Read(b []byte) (n int, err error) {
|
func (c *tunConn) Read(b []byte) (n int, err error) {
|
||||||
offset := device.MessageTransportHeaderSize
|
offset := device.MessageTransportHeaderSize
|
||||||
buf := config.MPool.Get().([]byte)[:]
|
buf := config.LPool.Get().([]byte)[:]
|
||||||
defer config.MPool.Put(buf[:])
|
defer config.LPool.Put(buf[:])
|
||||||
|
|
||||||
var size int
|
var size int
|
||||||
size, err = c.ifce.Read(buf[:], offset)
|
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 {
|
if len(b) < device.MessageTransportHeaderSize {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
buf := config.MPool.Get().([]byte)[:]
|
buf := config.LPool.Get().([]byte)[:]
|
||||||
defer config.MPool.Put(buf[:])
|
defer config.LPool.Put(buf[:])
|
||||||
|
|
||||||
copy(buf[device.MessageTransportOffsetContent:], b)
|
copy(buf[device.MessageTransportOffsetContent:], b)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user