Vectorise reads from IPv6 RWC

This commit is contained in:
Neil Alexander 2024-07-17 23:30:38 +01:00
parent 0fb5d76e30
commit c38544014c
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
3 changed files with 64 additions and 11 deletions

View file

@ -1,9 +1,5 @@
package tun
import (
"net"
)
const TUN_OFFSET_BYTES = 80 // sizeof(virtio_net_hdr)
const TUN_MAX_VECTOR = 16
@ -16,7 +12,7 @@ func (tun *TunAdapter) idealBatchSize() int {
func (tun *TunAdapter) read() {
vs := tun.idealBatchSize()
bufs := make(net.Buffers, vs)
bufs := make([][]byte, vs)
sizes := make([]int, vs)
for i := range bufs {
bufs[i] = make([]byte, TUN_OFFSET_BYTES+65535)
@ -36,13 +32,14 @@ func (tun *TunAdapter) read() {
}
func (tun *TunAdapter) write() {
vs := 1 // One at a time for now... eventually use tun.idealBatchSize()
bufs := make(net.Buffers, vs)
vs := tun.idealBatchSize()
bufs := make([][]byte, vs)
sizes := make([]int, vs)
for i := range bufs {
bufs[i] = make([]byte, TUN_OFFSET_BYTES+65535)
}
for {
n, err := tun.rwc.Read(bufs[0][TUN_OFFSET_BYTES : TUN_OFFSET_BYTES+65535])
n, err := tun.rwc.ReadMany(bufs, sizes, TUN_OFFSET_BYTES)
if err != nil {
tun.log.Errorln("Exiting TUN writer due to core read error:", err)
return
@ -50,8 +47,7 @@ func (tun *TunAdapter) write() {
if !tun.isEnabled {
continue // Nothing to do, the tun isn't enabled
}
bufs[0] = bufs[0][:TUN_OFFSET_BYTES+n]
if _, err = tun.iface.Write(bufs, TUN_OFFSET_BYTES); err != nil {
if _, err = tun.iface.Write(bufs[:n], TUN_OFFSET_BYTES); err != nil {
tun.Act(nil, func() {
if !tun.isOpen {
tun.log.Errorln("TUN iface write error:", err)