From dbd949307e75bbd72d86e53aa57b74b20daab04d Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Sat, 4 Mar 2023 15:25:46 +0100 Subject: conn: inch BatchSize toward being non-dynamic There's not really a use at the moment for making this configurable, and once bind_windows.go behaves like bind_std.go, we'll be able to use constants everywhere. So begin that simplification now. Signed-off-by: Jason A. Donenfeld --- conn/bind_std.go | 18 ++++++------------ conn/bind_windows.go | 2 ++ conn/conn.go | 2 +- device/queueconstants_android.go | 2 +- device/queueconstants_default.go | 2 +- tun/tcp_offload_linux.go | 6 +++--- tun/tcp_offload_linux_test.go | 4 ++-- tun/tun_linux.go | 6 +++--- 8 files changed, 19 insertions(+), 23 deletions(-) diff --git a/conn/bind_std.go b/conn/bind_std.go index a164f56..b9da4c3 100644 --- a/conn/bind_std.go +++ b/conn/bind_std.go @@ -31,21 +31,13 @@ type StdNetBind struct { blackhole6 bool ipv4PC *ipv4.PacketConn ipv6PC *ipv6.PacketConn - batchSize int udpAddrPool sync.Pool ipv4MsgsPool sync.Pool ipv6MsgsPool sync.Pool } -func NewStdNetBind() Bind { return NewStdNetBindBatch(DefaultBatchSize) } - -func NewStdNetBindBatch(maxBatchSize int) Bind { - if maxBatchSize == 0 { - maxBatchSize = DefaultBatchSize - } +func NewStdNetBind() Bind { return &StdNetBind{ - batchSize: maxBatchSize, - udpAddrPool: sync.Pool{ New: func() any { return &net.UDPAddr{ @@ -56,7 +48,7 @@ func NewStdNetBindBatch(maxBatchSize int) Bind { ipv4MsgsPool: sync.Pool{ New: func() any { - msgs := make([]ipv4.Message, maxBatchSize) + msgs := make([]ipv4.Message, IdealBatchSize) for i := range msgs { msgs[i].Buffers = make(net.Buffers, 1) msgs[i].OOB = make([]byte, srcControlSize) @@ -67,7 +59,7 @@ func NewStdNetBindBatch(maxBatchSize int) Bind { ipv6MsgsPool: sync.Pool{ New: func() any { - msgs := make([]ipv6.Message, maxBatchSize) + msgs := make([]ipv6.Message, IdealBatchSize) for i := range msgs { msgs[i].Buffers = make(net.Buffers, 1) msgs[i].OOB = make([]byte, srcControlSize) @@ -240,8 +232,10 @@ func (s *StdNetBind) receiveIPv6(buffs [][]byte, sizes []int, eps []Endpoint) (n return numMsgs, nil } +// TODO: When all Binds handle IdealBatchSize, remove this dynamic function and +// rename the IdealBatchSize constant to BatchSize. func (s *StdNetBind) BatchSize() int { - return s.batchSize + return IdealBatchSize } func (s *StdNetBind) Close() error { diff --git a/conn/bind_windows.go b/conn/bind_windows.go index 5a0b8c2..e44cc7b 100644 --- a/conn/bind_windows.go +++ b/conn/bind_windows.go @@ -321,6 +321,8 @@ func (bind *WinRingBind) Close() error { return nil } +// TODO: When all Binds handle IdealBatchSize, remove this dynamic function and +// rename the IdealBatchSize constant to BatchSize. func (bind *WinRingBind) BatchSize() int { // TODO: implement batching in and out of the ring return 1 diff --git a/conn/conn.go b/conn/conn.go index 9cbd0af..a9c70b5 100644 --- a/conn/conn.go +++ b/conn/conn.go @@ -16,7 +16,7 @@ import ( ) const ( - DefaultBatchSize = 128 // maximum number of packets handled per read and write + IdealBatchSize = 128 // maximum number of packets handled per read and write ) // A ReceiveFunc receives at least one packet from the network and writes them diff --git a/device/queueconstants_android.go b/device/queueconstants_android.go index 1158387..3d80ead 100644 --- a/device/queueconstants_android.go +++ b/device/queueconstants_android.go @@ -10,7 +10,7 @@ import "golang.zx2c4.com/wireguard/conn" /* Reduce memory consumption for Android */ const ( - QueueStagedSize = conn.DefaultBatchSize + QueueStagedSize = conn.IdealBatchSize QueueOutboundSize = 1024 QueueInboundSize = 1024 QueueHandshakeSize = 1024 diff --git a/device/queueconstants_default.go b/device/queueconstants_default.go index 7ed70a1..ea763d0 100644 --- a/device/queueconstants_default.go +++ b/device/queueconstants_default.go @@ -10,7 +10,7 @@ package device import "golang.zx2c4.com/wireguard/conn" const ( - QueueStagedSize = conn.DefaultBatchSize + QueueStagedSize = conn.IdealBatchSize QueueOutboundSize = 1024 QueueInboundSize = 1024 QueueHandshakeSize = 1024 diff --git a/tun/tcp_offload_linux.go b/tun/tcp_offload_linux.go index f3ffa75..a040e6f 100644 --- a/tun/tcp_offload_linux.go +++ b/tun/tcp_offload_linux.go @@ -72,11 +72,11 @@ type tcpGROTable struct { func newTCPGROTable() *tcpGROTable { t := &tcpGROTable{ - itemsByFlow: make(map[flowKey][]tcpGROItem, conn.DefaultBatchSize), - itemsPool: make([][]tcpGROItem, conn.DefaultBatchSize), + itemsByFlow: make(map[flowKey][]tcpGROItem, conn.IdealBatchSize), + itemsPool: make([][]tcpGROItem, conn.IdealBatchSize), } for i := range t.itemsPool { - t.itemsPool[i] = make([]tcpGROItem, 0, conn.DefaultBatchSize) + t.itemsPool[i] = make([]tcpGROItem, 0, conn.IdealBatchSize) } return t } diff --git a/tun/tcp_offload_linux_test.go b/tun/tcp_offload_linux_test.go index 7fa0777..11f9e53 100644 --- a/tun/tcp_offload_linux_test.go +++ b/tun/tcp_offload_linux_test.go @@ -125,8 +125,8 @@ func Test_handleVirtioRead(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - out := make([][]byte, conn.DefaultBatchSize) - sizes := make([]int, conn.DefaultBatchSize) + out := make([][]byte, conn.IdealBatchSize) + sizes := make([]int, conn.IdealBatchSize) for i := range out { out[i] = make([]byte, 65535) } diff --git a/tun/tun_linux.go b/tun/tun_linux.go index d56e3c1..88ed979 100644 --- a/tun/tun_linux.go +++ b/tun/tun_linux.go @@ -524,7 +524,7 @@ func (tun *NativeTun) initFromFlags(name string) error { return } tun.vnetHdr = true - tun.batchSize = conn.DefaultBatchSize + tun.batchSize = conn.IdealBatchSize } else { tun.batchSize = 1 } @@ -577,7 +577,7 @@ func CreateTUNFromFile(file *os.File, mtu int) (Device, error) { statusListenersShutdown: make(chan struct{}), tcp4GROTable: newTCPGROTable(), tcp6GROTable: newTCPGROTable(), - toWrite: make([]int, 0, conn.DefaultBatchSize), + toWrite: make([]int, 0, conn.IdealBatchSize), } name, err := tun.Name() @@ -633,7 +633,7 @@ func CreateUnmonitoredTUNFromFD(fd int) (Device, string, error) { errors: make(chan error, 5), tcp4GROTable: newTCPGROTable(), tcp6GROTable: newTCPGROTable(), - toWrite: make([]int, 0, conn.DefaultBatchSize), + toWrite: make([]int, 0, conn.IdealBatchSize), } name, err := tun.Name() if err != nil { -- cgit v1.2.3