Contain vectorisation changes to the TUN package

This commit is contained in:
Neil Alexander 2024-07-18 22:57:28 +01:00
parent c38544014c
commit 115d8a3b9d
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
3 changed files with 38 additions and 67 deletions

View file

@ -10,6 +10,7 @@ import (
"fmt"
"io"
"net"
"sync"
"time"
"github.com/Arceliar/phony"
@ -24,7 +25,6 @@ type MTU uint16
type ReadWriteCloser interface {
io.ReadWriteCloser
ReadMany([][]byte, []int, int) (int, error) // Vectorised reads
Address() address.Address
Subnet() address.Subnet
MaxMTU() uint64
@ -50,6 +50,7 @@ type TunAdapter struct {
name InterfaceName
mtu InterfaceMTU
}
ch chan []byte
}
// Gets the maximum supported MTU for the platform based on the defaults in
@ -161,6 +162,8 @@ func (tun *TunAdapter) _start() error {
tun.rwc.SetMTU(tun.MTU())
tun.isOpen = true
tun.isEnabled = true
tun.ch = make(chan []byte, tun.idealBatchSize())
go tun.queue()
go tun.read()
go tun.write()
return nil
@ -194,3 +197,12 @@ func (tun *TunAdapter) _stop() error {
}
return nil
}
const bufPoolSize = TUN_OFFSET_BYTES + 65535
var bufPool = sync.Pool{
New: func() any {
b := [bufPoolSize]byte{}
return b[:]
},
}