wintun: Move exchange buffer in separate struct on heap

This allows buffer alignment and keeps it together with its meta-data.

Furthermore, the write buffer has been reduced - as long as we flush
after _every_ write, we don't need a 1MiB write buffer.

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2019-02-20 11:41:37 +01:00
parent 4863089120
commit 6581cfb885

View file

@ -18,7 +18,8 @@ const (
packetSizeMax uint32 = 0xeffc // Maximum packet size: 4 + packetSizeMax == 0xf000 packetSizeMax uint32 = 0xeffc // Maximum packet size: 4 + packetSizeMax == 0xf000
packetExchangeMax uint32 = 256 // Number of packets that may be written at a time packetExchangeMax uint32 = 256 // Number of packets that may be written at a time
packetExchangeAlignment uint32 = 16 // Number of bytes packets are aligned to in exchange buffers packetExchangeAlignment uint32 = 16 // Number of bytes packets are aligned to in exchange buffers
packetExchangeSize uint32 = 0x100000 // Exchange buffer size (defaults to 1MiB) packetExchangeSizeRead uint32 = 0x100000 // Read exchange buffer size (defaults to 1MiB)
packetExchangeSizeWrite uint32 = 0x10000 // Write exchange buffer size (defaults to 64kiB)
) )
const ( const (
@ -28,18 +29,26 @@ const (
signalMax signalMax
) )
type exchgBufRead struct {
data [packetExchangeSizeRead]byte
offset uint32
avail uint32
}
type exchgBufWrite struct {
data [packetExchangeSizeWrite]byte
offset uint32
packetNum uint32
}
type nativeTun struct { type nativeTun struct {
wt *wintun.Wintun wt *wintun.Wintun
tunName string tunName string
signalName *uint16 signalName *uint16
tunFile *os.File tunFile *os.File
wrBuff [packetExchangeSize]byte rdBuff *exchgBufRead
rdBuff [packetExchangeSize]byte wrBuff *exchgBufWrite
signals [signalMax]windows.Handle signals [signalMax]windows.Handle
wrOffset uint32
wrPacketNum uint32
rdOffset uint32
rdAvailabe uint32
events chan TUNEvent events chan TUNEvent
errors chan error errors chan error
} }
@ -88,6 +97,8 @@ func CreateTUN(ifname string) (TUNDevice, error) {
wt: wt, wt: wt,
tunName: wt.DataFileName(), tunName: wt.DataFileName(),
signalName: signalNameUTF16, signalName: signalNameUTF16,
rdBuff: &exchgBufRead{},
wrBuff: &exchgBufWrite{},
events: make(chan TUNEvent, 10), events: make(chan TUNEvent, 10),
errors: make(chan error, 1), errors: make(chan error, 1),
} }
@ -203,19 +214,19 @@ func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
} }
for { for {
if tun.rdOffset+4 <= tun.rdAvailabe { if tun.rdBuff.offset+4 <= tun.rdBuff.avail {
// Get packet from the queue. // Get packet from the exchange buffer.
size := *(*uint32)(unsafe.Pointer(&tun.rdBuff[tun.rdOffset])) size := *(*uint32)(unsafe.Pointer(&tun.rdBuff.data[tun.rdBuff.offset]))
pSize := packetAlign(4 + size) pSize := packetAlign(4 + size)
if packetSizeMax < size || tun.rdAvailabe < tun.rdOffset+pSize { if packetSizeMax < size || tun.rdBuff.avail < tun.rdBuff.offset+pSize {
// Invalid packet size. // Invalid packet size.
tun.rdAvailabe = 0 tun.rdBuff.avail = 0
continue continue
} }
// Copy data. // Copy data.
copy(buff[offset:], (*(*[packetSizeMax]byte)(unsafe.Pointer(&tun.rdBuff[tun.rdOffset+4])))[:size]) copy(buff[offset:], (*(*[packetSizeMax]byte)(unsafe.Pointer(&tun.rdBuff.data[tun.rdBuff.offset+4])))[:size])
tun.rdOffset += pSize tun.rdBuff.offset += pSize
return int(size), nil return int(size), nil
} }
@ -249,16 +260,16 @@ func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
} }
// Fill queue. // Fill queue.
n, err := tun.tunFile.Read(tun.rdBuff[:]) n, err := tun.tunFile.Read(tun.rdBuff.data[:])
if err != nil { if err != nil {
// TUN interface stopped, returned incomplete data, etc. // TUN interface stopped, returned incomplete data, etc.
// Retry. // Retry.
tun.rdAvailabe = 0 tun.rdBuff.avail = 0
tun.closeTUN() tun.closeTUN()
continue continue
} }
tun.rdOffset = 0 tun.rdBuff.offset = 0
tun.rdAvailabe = uint32(n) tun.rdBuff.avail = uint32(n)
} }
} }
@ -266,9 +277,9 @@ func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
func (tun *nativeTun) flush() error { func (tun *nativeTun) flush() error {
// Flush write buffer. // Flush write buffer.
_, err := tun.tunFile.Write(tun.wrBuff[:tun.wrOffset]) _, err := tun.tunFile.Write(tun.wrBuff.data[:tun.wrBuff.offset])
tun.wrPacketNum = 0 tun.wrBuff.packetNum = 0
tun.wrOffset = 0 tun.wrBuff.offset = 0
if err != nil { if err != nil {
return err return err
} }
@ -286,7 +297,7 @@ func (tun *nativeTun) putTunPacket(buff []byte) error {
} }
pSize := packetAlign(4 + size) pSize := packetAlign(4 + size)
if tun.wrPacketNum >= packetExchangeMax || tun.wrOffset+pSize >= packetExchangeSize { if tun.wrBuff.packetNum >= packetExchangeMax || tun.wrBuff.offset+pSize >= packetExchangeSizeWrite {
// Exchange buffer is full -> flush first. // Exchange buffer is full -> flush first.
err := tun.flush() err := tun.flush()
if err != nil { if err != nil {
@ -295,11 +306,11 @@ func (tun *nativeTun) putTunPacket(buff []byte) error {
} }
// Write packet to the exchange buffer. // Write packet to the exchange buffer.
*(*uint32)(unsafe.Pointer(&tun.wrBuff[tun.wrOffset])) = size *(*uint32)(unsafe.Pointer(&tun.wrBuff.data[tun.wrBuff.offset])) = size
copy((*(*[packetSizeMax]byte)(unsafe.Pointer(&tun.wrBuff[tun.wrOffset+4])))[:size], buff) copy((*(*[packetSizeMax]byte)(unsafe.Pointer(&tun.wrBuff.data[tun.wrBuff.offset+4])))[:size], buff)
tun.wrPacketNum++ tun.wrBuff.packetNum++
tun.wrOffset += pSize tun.wrBuff.offset += pSize
return nil return nil
} }